Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return an object when loading a YAML file #20289

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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