Skip to content

Commit

Permalink
Don’t write to configuration.yaml when it didn’t change. #2071
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 3, 2019
1 parent e7bf5ee commit 732a97b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ function write() {
// Read settings to check if we have to split devices/groups into separate file.
const actual = yaml.read(file);
if (typeof actual.devices === 'string') {
yaml.write(data.joinPath(actual.devices), settings.devices);
yaml.writeIfChanged(data.joinPath(actual.devices), settings.devices);
toWrite.devices = actual.devices;
}

if (typeof actual.groups === 'string') {
yaml.write(data.joinPath(actual.groups), settings.groups);
yaml.writeIfChanged(data.joinPath(actual.groups), settings.groups);
toWrite.groups = actual.groups;
}

yaml.write(file, toWrite);
yaml.writeIfChanged(file, toWrite);

_settings = read();
_settingsWithDefaults = objectAssignDeep.noMutate(defaults, get());
Expand Down
10 changes: 7 additions & 3 deletions lib/util/yaml.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const yaml = require('js-yaml');
const fs = require('fs');
const equals = require('fast-deep-equal');

function read(file) {
try {
Expand All @@ -25,8 +26,11 @@ function readIfExists(file) {
return fs.existsSync(file) ? read(file) : null;
}

function write(file, content) {
fs.writeFileSync(file, yaml.safeDump(content));
function writeIfChanged(file, content) {
const before = readIfExists(file);
if (!equals(before, content)) {
fs.writeFileSync(file, yaml.safeDump(content));
}
}

module.exports = {read, readIfExists, write};
module.exports = {read, readIfExists, writeIfChanged};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"ajv": "*",
"debounce": "*",
"fast-deep-equal": "*",
"git-last-commit": "*",
"js-yaml": "*",
"mkdir-recursive": "*",
Expand Down
7 changes: 7 additions & 0 deletions test/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,11 @@ describe('Controller', () => {
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(0);
});

it('Should start when state is corrupted', async () => {
fs.writeFileSync(path.join(data.mockDir, 'state.json'), 'corrupted');
await controller.start();
await flushPromises();
expect(controller.state.state).toStrictEqual({});
});
});
11 changes: 11 additions & 0 deletions test/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,15 @@ describe('Settings', () => {
settings.changeFriendlyName('myname1', 'myname');
}).toThrowError(`friendly_name 'myname' is already in use`);
});

it('Shouldnt write to configuration.yaml when there are no changes in it', () => {
const contentConfiguration = {devices: 'devices.yaml'};
const contentDevices = {};
write(configurationFile, contentConfiguration);
const before = fs.statSync(configurationFile).mtimeMs;
write(devicesFile, contentDevices);
settings.addDevice('0x1234');
const after = fs.statSync(configurationFile).mtimeMs;
expect(before).toBe(after);
});
});
4 changes: 2 additions & 2 deletions test/stub/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ function writeDefaultConfiguration() {
}
};

yaml.write(path.join(mockDir, 'configuration.yaml'), config);
yaml.writeIfChanged(path.join(mockDir, 'configuration.yaml'), config);
}

function writeEmptyState() {
yaml.write(path.join(mockDir, 'state.json'), JSON.stringify({}));
fs.writeFileSync(path.join(mockDir, 'state.json'), JSON.stringify({}));
}

function writeDefaultState() {
Expand Down

0 comments on commit 732a97b

Please sign in to comment.