Skip to content

Commit

Permalink
Fix tests - make them work without PhantomJS and Compass
Browse files Browse the repository at this point in the history
Make Travis happy!
  • Loading branch information
mklabs authored and sindresorhus committed Sep 14, 2012
1 parent 1d92592 commit 2802360
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 23 deletions.
53 changes: 47 additions & 6 deletions cli/tasks/yeoman.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

var fs = require('fs'),
join = require('path').join,
util = require('util'),
h5bp = require('../');
var fs = require('fs');
var join = require('path').join;
var util = require('util');
var which = require('which');

// ant build script has a nice notion of environment, this defaults to
// production. And we only support env=prod for now.
Expand Down Expand Up @@ -33,13 +32,20 @@ module.exports = function(grunt) {
// maintained (todo: inline script/style minified)
// - basics - same as buildkit plus minor html optimizations
// - minify - same as build plus full html minification
//
// - test - same as default build plus but conditionally runs compass /
// manifest task depending on whether or not compass / phantomjs binaries are
// available within the path. During the checking process, we output warning
// infos about missing deps. It might make sense to make it the default (img
// task internally does this check)
var targets = {
default : ' rjs concat css min img rev usemin manifest',
usemin : 'usemin-handler rjs concat css img rev usemin manifest',
text : 'usemin-handler rjs concat css min rev usemin manifest',
buildkit : 'usemin-handler rjs concat css min img rev usemin manifest html:buildkit',
basics : 'usemin-handler rjs concat css min img rev usemin manifest html:basics',
minify : 'usemin-handler rjs concat css min img rev usemin manifest html:compress'
minify : 'usemin-handler rjs concat css min img rev usemin manifest html:compress',
test : 'usemin-handler rjs concat css img rev usemin manifest'
};

var targetList = grunt.log.wordlist(Object.keys(targets));
Expand All @@ -55,9 +61,19 @@ module.exports = function(grunt) {
}

var tasks = ['intro', 'clean coffee compass mkdirs', targets[target], 'copy time'].join(' ');

// Conditionally remove compass / manifest task if either compass or
// phantomjs binary is missing. Done only for `test` target (specifically
// used for our `npm test`). For each, output warning infos.
if( target === 'test' ) {
tasks = grunt.helper( 'build:skip', tasks, 'compass' );
tasks = grunt.helper( 'build:skip', tasks, 'phantomjs', 'manifest' );
}

grunt.log.subhead('Running ' + target + ' target')
.writeln(' - ' + grunt.log.wordlist(tasks.split(' '), { separator: ' ' }));


grunt.task.run(tasks);
});

Expand All @@ -74,6 +90,31 @@ module.exports = function(grunt) {
return err;
});

// Helper to conditionally remove a given task from the provided list of
// `tasks` (as a String). A `cmd` and `task` Strings should be provided,
// if `task` is omitted, it then takes the same value of the `cmd` one.
//
// When a binary is known as non available, and that the task is skipped,
// this helper also output warning info to the user.
//
// - tasks - String list of tasks to be run
// - cmd - The String name of the binary to check against the system.
// - task - (optional) A string name to define the specific task to remove
// from the returned tasks list. If undefined, is the same than
// `binary`.
//
// Returns optionally modified `tasks` String.
grunt.registerHelper('build:skip', function(tasks, cmd, task) {
try {
which.sync(cmd);
} catch(err) {
grunt.helper('not installed', cmd);
tasks = tasks.replace(task || cmd, '');
}

return tasks;
});

grunt.registerHelper('pad', function pad(str, max) {
return str.length > max ? str :
str + new Array(max - str.length + 1).join(' ');
Expand Down
29 changes: 23 additions & 6 deletions cli/test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

var fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
rimraf = require('rimraf'),
mkdirp = require('mkdirp'),
Runnable = require('./runnable');
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var which = require('which');
var Runnable = require('./runnable');

// top level exports
var helpers = module.exports;
Expand Down Expand Up @@ -140,3 +141,19 @@ helpers.gruntfile = function(options) {
};
};


// Mocha before helper. Takes a command String to be checked against `which`
// package, and a callback to call on completion, most likely the mocha async
// `done` callback.
//
// Setups the relevant Boolean flag on the test context.
//
helpers.installed = function installed(command, cb) {
var ctx = this;
return function installed(done) {
which(command, function(err) {
ctx[command] = !err;
done();
});
};
};
48 changes: 37 additions & 11 deletions cli/test/test-build.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

var fs = require('fs'),
grunt = require('grunt'),
assert = require('assert'),
helpers = require('./helpers');
/*global describe, before, after, beforeEach, afterEach, describe, it */
var fs = require('fs');
var grunt = require('grunt');
var assert = require('assert');
var helpers = require('./helpers');

var opts = grunt.cli.options;
opts.redirect = !opts.silent;

// XXX Conform to coding guidelines, mostly literral spacing stuff

describe('yeoman init && yeoman build', function() {

before(helpers.directory('.test'));
Expand All @@ -17,7 +19,14 @@ describe('yeoman init && yeoman build', function() {
}
}));

describe('When I run init app with default prompts', function() {
// Handle missing dependencies during test run, we only run compass /
// manifest related test when necessary binaries are available. This helper
// defines the respective boolean flag on the mocha test context, and used
// within some of our tests to conditionnaly go through.
before(helpers.installed('compass'));
before(helpers.installed('phantomjs'));

describe('When I run init app with default prompts', function(done) {
it('should output to stdout expected file writes', function(done) {
var yeoman = helpers.run('init --force', opts);
yeoman
Expand Down Expand Up @@ -106,7 +115,7 @@ describe('yeoman init && yeoman build', function() {
before(function() {
var self = this;
// setup the runnable, the actual run happens on last step
this.yeoman = helpers.run('build --no-color', opts);
this.yeoman = helpers.run('build:test --no-color', opts);
});

afterEach(function(done) {
Expand All @@ -121,23 +130,37 @@ describe('yeoman init && yeoman build', function() {
.expect(/Running "clean(:.+)?" (\(.+\) )?task/)
.expect(/Running "mkdirs(:.+)?" (\(.+\) )?task/)
.expect(/Running "coffee(:.+)?" (\(.+\) )?task/)
.expect(/Running "compass(:.+)?" (\(.+\) )?task/)
.expect(/Running "usemin-handler(:.+)?" (\(.+\) )?task/)
.expect(/Running "rjs(:.+)?" (\(.+\) )?task/)
.expect(/Running "concat(:.+)?" (\(.+\) )?task/)
.expect(/Running "css(:.+)?" (\(.+\) )?task/)
.expect(/Running "img(:.+)?" (\(.+\) )?task/)
.expect(/Running "rev(:.+)?" (\(.+\) )?task/)
.expect(/Running "usemin(:.+)?" (\(.+\) )?task/)
.expect(/Running "manifest(:.+)?" (\(.+\) )?task/)
.expect(/Running "copy(:.+)?" (\(.+\) )?task/)
.expect(/Running "time(:.+)?" (\(.+\) )?task/);

if( this.compass ) {
this.yeoman.expect(/Running "compass(:.+)?" (\(.+\) )?task/);
}

if( this.phantomjs ) {
this.yeoman.expect(/Running "manifest(:.+)?" (\(.+\) )?task/);
}
});

describe('build', function() {
it('should output the list of task at the beginning of the build', function() {
this.yeoman
.expect(/intro clean coffee compass mkdirs usemin-handler rjs concat css img rev usemin manifest copy time/);
this.yeoman.expect(/intro clean/);
this.yeoman.expect(/mkdirs usemin-handler rjs concat css img rev usemin/);

if( this.compass ) {
this.yeoman.expect(/compass mkdirs/);
}

if( this.phantomjs) {
this.yeoman.expect(/usemin manifest copy/);
}
});
});

Expand All @@ -159,10 +182,12 @@ describe('yeoman init && yeoman build', function() {

describe('compass', function() {
it('should go through compass:dist', function() {
if( !this.compass ) { return; }
this.yeoman.expect(/Running "compass:dist" \(compass\) task/);
});

it('should write to styles/main.css', function() {
if( !this.compass ) { return; }
this.yeoman.expect(/(overwrite|identical|create)(.+)styles\/main.css/);
});
});
Expand Down Expand Up @@ -243,6 +268,7 @@ describe('yeoman init && yeoman build', function() {
this.yeoman.expect('Starting static web server on port 3501');
});
it('should write to manifest.appcache', function() {
if( !this.phantomjs ) { return; }
this.yeoman
.expect('Writing to manifest.appcache')
.expect('This manifest was created by confess.js, http://github.com/jamesgpearce/confess')
Expand Down

0 comments on commit 2802360

Please sign in to comment.