From df581953f3b0e0b1e63c1b230116dec37b6f75db Mon Sep 17 00:00:00 2001 From: Carlos Lopes Date: Mon, 14 Apr 2014 10:45:55 +0200 Subject: [PATCH] Supporting both the inline 'toggleStates' property and 'everythingOn'. --- package.json | 3 ++- tasks/lib/redactTask.js | 23 ++++++++++++++++--- test/spec/redactTaskSpec.js | 46 +++++++++++++++++++++++++++++++++---- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8bc10a3..4968456 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tasks/lib/redactTask.js b/tasks/lib/redactTask.js index f25df15..f317b7d 100644 --- a/tasks/lib/redactTask.js +++ b/tasks/lib/redactTask.js @@ -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; } }; }; @@ -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(); diff --git a/test/spec/redactTaskSpec.js b/test/spec/redactTaskSpec.js index e820165..c8b1dcf 100644 --- a/test/spec/redactTaskSpec.js +++ b/test/spec/redactTaskSpec.js @@ -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 () {