Skip to content

Commit

Permalink
fix: Fix load of empty configuration .yaml file failing (#20289)
Browse files Browse the repository at this point in the history
* Always return an object when loading a YAML file, even when it is empty or containing the `null` value.

Fixes #20283

* Add unit test for having multiple device files, with one is empty. This test breaks without the proposed.
  • Loading branch information
rhuss committed Dec 20, 2023
1 parent af8c2d8 commit b67888f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/util/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import equals from 'fast-deep-equal/es6';

function read(file: string): KeyValue {
try {
return yaml.load(fs.readFileSync(file, 'utf8'));
const result = yaml.load(fs.readFileSync(file, 'utf8'));
return result as KeyValue ?? {};
} catch (error) {
if (error.name === 'YAMLException') {
error.file = file;
Expand Down
30 changes: 18 additions & 12 deletions test/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,22 +335,15 @@ describe('Settings', () => {
expect(read(devicesFile)).toStrictEqual(expected);
});

it('Should add devices for first file when using 2 separates file', () => {
function extractFromMultipleDeviceConfigs(contentDevices2) {
const contentConfiguration = {
devices: ['devices.yaml', 'devices2.yaml']
devices: ['devices.yaml', 'devices2.yaml'],
};

const contentDevices = {
'0x12345678': {
friendly_name: '0x12345678',
retain: false,
},
};

const contentDevices2 = {
'0x87654321': {
friendly_name: '0x87654321',
retain: false,
retain: false,
},
};

Expand All @@ -365,15 +358,28 @@ describe('Settings', () => {
const expected = {
'0x12345678': {
friendly_name: '0x12345678',
retain: false,
retain: false,
},
'0x1234': {
'0x1234': {
friendly_name: '0x1234',
},
};

expect(read(devicesFile)).toStrictEqual(expected);
expect(read(devicesFile2)).toStrictEqual(contentDevices2);
}

it('Should add devices for first file when using 2 separates file', () => {
extractFromMultipleDeviceConfigs({
'0x87654321': {
friendly_name: '0x87654321',
retain: false,
},
});
});

it('Should add devices for first file when using 2 separates file and the second file is empty', () => {
extractFromMultipleDeviceConfigs(null)
});

it('Should add devices to a separate file if devices.yaml doesnt exist', () => {
Expand Down

0 comments on commit b67888f

Please sign in to comment.