Skip to content

Commit

Permalink
Tests for ensuring preferences.json before write. Closes gh-821
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Aug 19, 2016
1 parent 67e38ca commit a53a076
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ Preferences.write = function(key, value) {
.then(contents => {
contents = contents || {};
contents[key] = value;
fs.ensureFile(preferencesJson, function(err){
fs.ensureFile(preferencesJson, error => {
if (error) {
log.error('Error writing preference', key, value);
reject(error);
} else {
fs.writeFile(preferencesJson, JSON.stringify(contents), function(error) {
fs.writeFile(preferencesJson, JSON.stringify(contents), error => {
if (error) {
log.error('Error writing preference', key, value);
reject(error);
Expand All @@ -45,7 +45,7 @@ Preferences.write = function(key, value) {
}
});
}
})
});
})
.catch(error => {
reject(error);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ exports['Preferences.write'] = {
this.exists = this.sandbox.stub(fs, 'exists', (file, handler) => {
handler(true);
});
this.ensureFile = this.sandbox.stub(fs, 'ensureFile', (file, handler) => {
handler(null);
});
this.readFile = this.sandbox.stub(fs, 'readFile', (file, handler) => {
handler(null, JSON.stringify(this.state));
});
Expand All @@ -160,5 +163,21 @@ exports['Preferences.write'] = {
test.equal(value, this.state[key]);
test.done();
});
},

writeEnsureFileFalse: function(test) {
test.expect(1);

var key = 'key';
var value = 'value';
var error = 'error';

this.ensureFile.restore();
this.ensureFile = this.sandbox.stub(fs, 'ensureFile', (file, handler) => handler(error));

Preferences.write(key, value).catch(rejection => {
test.equal(rejection, error);
test.done();
});
}
};

0 comments on commit a53a076

Please sign in to comment.