Skip to content

Commit

Permalink
Add tests for Exam command and minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Willis committed Dec 4, 2015
1 parent c1ad5e8 commit e469d3d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/commands/exam.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ var TestCommand = require('ember-cli/lib/commands/test');
var TestsProcessor = require('../utils/tests-processor');
var TestsOptionsValidator = require('../utils/tests-options-validator');

/**
* Patches the Build task so that it performs any needed transforms on the Tests
* AST after the build has finished.
*
* @param {TestsOptionsValidator} validator
* @return {Void}
*/
function patchBuildTask(BuildTask, validator) {
var BuildTask$run = BuildTask.prototype.run;
BuildTask.prototype.run = function(options) {
validator.options.outputPath = options.outputPath;

return BuildTask$run.apply(this, arguments).then(function() {
return new TestsProcessor(validator).write();
});
}
}

module.exports = TestCommand.extend({
name: 'exam',

Expand All @@ -42,16 +24,21 @@ module.exports = TestCommand.extend({
{ name: 'distill', type: String, description: 'Filters your tests after build on a string or regex value.' }
].concat(TestCommand.prototype.availableOptions),

utils: {
Processor: TestsProcessor,
Validator: TestsOptionsValidator
},

/**
* Validates the command options and then runs the original test command.
*
* @override
*/
run: function(commandOptions) {
this.validator = new TestsOptionsValidator(commandOptions);
this.validator = new this.utils.Validator(commandOptions);

if (this.validator.needsAST) {
patchBuildTask(this.tasks.Build, this.validator);
this._patchBuildTask();

// If we're running specific file of tests, add it to the query.
var splitFile = commandOptions.splitFile;
Expand All @@ -62,5 +49,27 @@ module.exports = TestCommand.extend({
}

return this._super.run.apply(this, arguments);
},

/**
* Patches the Build task so that it performs any needed transforms on the Tests
* AST after the build has finished.
*
* @private
* @return {Void}
*/
_patchBuildTask: function() {
var validator = this.validator;
var BuildTask = this.tasks.Build;
var Processor = this.utils.Processor;

var BuildTask$run = BuildTask.prototype.run;
BuildTask.prototype.run = function(options) {
validator.options.outputPath = options.outputPath;

return BuildTask$run.apply(this, arguments).then(function() {
return new Processor(validator).write();
});
}
}
});
89 changes: 89 additions & 0 deletions node-tests/unit/commands/exam-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var assert = require('assert');
var RSVP = require('rsvp');
var MockProject = require('ember-cli/tests/helpers/mock-project');
var Task = require('ember-cli/lib/models/task');

var ExamCommand = require('../../../lib/commands/exam');
var TestsProcessor = require('../../../lib/utils/tests-processor');

describe('ExamCommand', function() {
describe('run', function() {
var command;
var superCall;
var originalBuildRun;

beforeEach(function() {
var tasks = {
Build: Task.extend()
};

originalBuildRun = tasks.Build.prototype.run = function() {
return RSVP.Promise.resolve();
};

var project = new MockProject();

project.isEmberCLIProject = function() { return true; };

command = new ExamCommand({
project: project,
tasks: tasks
});

superCall = { called: false };
command._super.run = function(options) {
superCall = {
called: true,
options: options
}
};
});

it('should defer to super with normal build task', function() {
command.run({});

assert.equal(originalBuildRun, command.tasks.Build.prototype.run);
assert.equal(superCall.called, true);
});

it('should defer to super after patching the build task', function() {
command.run({ split: 2 });

assert.notEqual(originalBuildRun, command.tasks.Build.prototype.run);
assert.equal(superCall.called, true);
});

it('should run a test processor after the build runs', function(done) {
var processorCalled = false;

command.utils.Processor = function ProcessorStub() {
processorCalled = true;
this.write = function() {
return RSVP.Promise.resolve();
};
};

command.run({ split: 2 });

var build = new command.tasks.Build();
build.run({ outputPath: 'dist' }).then(function() {
assert.equal(processorCalled, true);
done();
});
});

it('should set \'splitFile\' to the query option', function() {
command.run({ split: 2, splitFile: 2 });

assert.equal(superCall.options.query, 'splitFile=2');
assert.equal(superCall.called, true);
});

it('should append \'splitFile\' to the query option', function() {
command.run({ split: 2, splitFile: 2, query: 'someQuery=derp&hidepassed' });

assert.equal(superCall.options.query, 'someQuery=derp&hidepassed&splitFile=2');
assert.equal(superCall.called, true);
});
});
});

0 comments on commit e469d3d

Please sign in to comment.