Use path.resolve instead of path.join in data.joinPath (#14953)

This allows specifying a path outside of the data directory using an
absolute path (e.g. /path/to/devices.yaml) or a path relative to the data
directory (e.g. devices.yaml, foo/devices.yaml, ../../devices.yaml, ...).
This change is backward compatible because file names and nested paths are
just a special case of a relative path.

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
Jakub Jirutka 2022-11-13 09:24:47 +01:00 committed by GitHub
parent 489d1de756
commit 4bc0c3ed6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 1 deletions

View File

@ -14,7 +14,7 @@ function load(): void {
load();
function joinPath(file: string): string {
return path.join(dataPath, file);
return path.resolve(dataPath, file);
}
function getPath(): string {

View File

@ -19,6 +19,7 @@ describe('Data', () => {
const actual = data.getPath();
expect(actual).toBe(expected);
expect(data.joinPath('test')).toStrictEqual(path.join(expected, 'test'));
expect(data.joinPath('/test')).toStrictEqual(path.resolve(expected, '/test'));
delete process.env.ZIGBEE2MQTT_DATA;
data.testingOnlyReload();
});