Skip to content

Commit

Permalink
Supporting both the inline 'toggleStates' property and 'everythingOn'.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosaml committed Apr 14, 2014
1 parent ee65deb commit df58195
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"test": "grunt test"
},
"dependencies": {
"redact": "~0.0.8"
"redact": "~0.0.8",
"lodash": "~2.4.1"
},
"devDependencies": {
"grunt": "~0.4.2",
Expand Down
23 changes: 20 additions & 3 deletions tasks/lib/redactTask.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
'use strict';

var _ = require("lodash");

exports._config = function () {
var file = this.file;
var toggleStatesFilePath = this.toggleStatesFileName;
var toggleStates = this.toggleStates || {};
var everythingOn = this.everythingOn || false;

return {
verify: function () {
if (!file.exists(toggleStatesFilePath)) {
throw new Error("Could not find the toggle states file at " + toggleStatesFilePath);
if (!file.exists(toggleStatesFilePath) && _.isEmpty(toggleStates)) {
throw new Error("Could not find the toggle states file at " + toggleStatesFilePath + " and no inline toggle states are defined");
}
return true;
},
read: function () {
return file.readJSON(toggleStatesFilePath);
var fromFile = {};
if (file.exists(toggleStatesFilePath)) {
fromFile = file.readJSON(toggleStatesFilePath);
}
var merged = _.merge(fromFile, toggleStates);

if (everythingOn) {
return _.mapValues(merged, function (value) { console.log(value); return true; });
}

return merged;
}
};
};
Expand Down Expand Up @@ -48,6 +63,8 @@ exports.run = function (grunt, redact, options) {

this.workingDirectory = options['workingDirectory'];
this.toggleStatesFileName = options['toggleStatesFile'];
this.toggleStates = options['toggleStates'];
this.everythingOn = options['everythingOn'];

exports._config().verify();
this.features = this._config().read();
Expand Down
46 changes: 42 additions & 4 deletions test/spec/redactTaskSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,65 @@ describe('grunt-redact plugin', function () {
}}};
});

describe('when reading the toggle states file', function () {
describe('when reading toggle states', function () {
beforeEach(function () {
redactTask.everythingOn = undefined;
redactTask.toggleStates = undefined;
redactTask.toggleStatesFileName = 'path/to/myBeautifulToggles.json';

redactTask.file = jasmine.createSpyObj('file', ['exists', 'readJSON']);
});

it('should throw an error if the toggle states file does not exist', function () {
it('should throw an error if the toggle states file does not exist and no toggles are defined in task the configuration', function () {
redactTask.file.exists.andReturn(false);
redactTask.toggleStates = undefined;

expect(function () {
redactTask._config().verify();
}).toThrow(new Error("Could not find the toggle states file at path/to/myBeautifulToggles.json"));
}).toThrow(new Error("Could not find the toggle states file at path/to/myBeautifulToggles.json and no inline toggle states are defined"));

expect(redactTask.file.exists).toHaveBeenCalledWith('path/to/myBeautifulToggles.json');
});

it('should not complain about the absence of the external file if inline toggles are defined', function () {
redactTask.file.exists.andReturn(false);
redactTask.toggleStates = { someToggle: false };

expect(redactTask._config().verify()).toBeTruthy();
expect(redactTask.file.exists).toHaveBeenCalledWith('path/to/myBeautifulToggles.json');
});

it('should return toggle config in JSON format', function () {
it('should return toggle config from external file in JSON format', function () {
var config = {my_feature: true};
redactTask.file.exists.andReturn(true);
redactTask.file.readJSON.andReturn(config);
expect(redactTask._config().read()).toBe(config);
expect(redactTask.file.readJSON).toHaveBeenCalledWith('path/to/myBeautifulToggles.json');
});

it('should return toggle config from inline definitions in JSON format', function () {
redactTask.toggleStates = { blablabla: true };
redactTask.file.exists.andReturn(false);
expect(redactTask._config().read()).toEqual({ blablabla: true });
expect(redactTask.file.readJSON).not.toHaveBeenCalled();
});

it('should override states specified in file if they are also defined inline', function () {
redactTask.file.exists.andReturn(true);
redactTask.file.readJSON.andReturn({ blablabla: true });
redactTask.toggleStates = {blablabla: false};

expect(redactTask._config().read()).toEqual({ blablabla: false });
});

it('should set all toggles to true if everythingOn is set', function() {
redactTask.everythingOn = true;
redactTask.toggleStates = { someToggle: false };
redactTask.file.exists.andReturn(true);
redactTask.file.readJSON.andReturn({ blablabla: false });

expect(redactTask._config().read()).toEqual({ blablabla: true, someToggle: true });
});
});

describe('when files are to be listed', function () {
Expand Down

0 comments on commit df58195

Please # to comment.