Skip to content

Commit

Permalink
Merge a509df9 into c1210d0
Browse files Browse the repository at this point in the history
  • Loading branch information
smackesey committed Dec 26, 2013
2 parents c1210d0 + a509df9 commit 8bd843d
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 12 deletions.
63 changes: 53 additions & 10 deletions lib/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,30 @@ helpers.gruntfile = function (options, done) {
fs.writeFile('Gruntfile.js', out.join('\n'), done);
};

helpers.assertFile = function (file, reg) {
var here = fs.existsSync(file);
assert.ok(here, file + ', no such file or directory');

if (!reg) {
return assert.ok(here);
// assertFile and assertNoFile take an arbitrary number of String arguments
// representing paths to files. The two methods check for the existence and
// nonexistence of files, respectively

helpers.assertFile = function () {
var args = Array.prototype.slice.call(arguments, 0);
if(_.last(args) instanceof RegExp) { // DEPRECATED CASE
helpers.assertFileContent(args[0], args[1]);
} else {
args = typeof(args[0]) === 'string' ? args : args[0];
args.forEach( function (file) {
var here = fs.existsSync(arguments[0]);
assert.ok(here, file + ', no such file or directory')
});
}
};

var body = fs.readFileSync(file, 'utf8');
assert.ok(reg.test(body), file + ' did not match \'' + reg + '\'.');
helpers.assertNoFile = function () {
var args = Array.prototype.slice.call(arguments, 0);
args = typeof(args[0]) === 'string' ? args : args[0];
args.forEach( function(file) {
var here = fs.existsSync(file);
assert.ok(!here, file + ' exists');
});
};

// Check all files present in the array are existing.
Expand All @@ -126,16 +140,45 @@ helpers.assertFile = function (file, reg) {
//
// helpers.assertFiles(['foo.js', 'bar.js', ['baz.js', /function baz/]]);
//
helpers.assertFiles = function (files) {
helpers.assertFiles = function (files) { // DEPRECATED
files.forEach(function (item) {
var file = item;
var rx;
if (item instanceof Array) {
file = item[0];
rx = item[1];
helpers.assertFile(file, rx);
} else {
helpers.assertFile(file);
}
});
};

// assertFileContent and assertNoFileContent respectively check for files
// matching and not matching the provided patterns. Both functions take two
// different signatures:
// (1) String, RegExp
// (2) [ [String, RegExp], [String, Regexp], ... ]

helpers.assertFileContent = function () {
var args = Array.prototype.slice.call(arguments, 0);
var pairs = typeof(args[0]) === 'string' ? [ args ] : args[0];
pairs.forEach(function (pair) {
var file = pair[0], regex = pair[1];
helpers.assertFile(file);
var body = fs.readFileSync(file, 'utf8');
assert.ok(regex.test(body), file + ' did not match \'' + regex + '\'.');
});
};

helpers.assertFile(file, rx);
helpers.assertNoFileContent = function (file, reg) {
var args = Array.prototype.slice.call(arguments, 0);
var pairs = typeof(args[0]) === 'string' ? [ args ] : args[0];
pairs.forEach(function (pair) {
var file = pair[0], regex = pair[1];
helpers.assertFile(file);
var body = fs.readFileSync(file, 'utf8');
assert.ok(!regex.test(body), file + ' did not match \'' + regex + '\'.');
});
};

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/testFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Roses are red.
1 change: 1 addition & 0 deletions test/fixtures/testFile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Violets are blue.
145 changes: 143 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/*global it, describe, before, beforeEach */

var util = require('util');
var path = require('path');
var fs = require('fs');
var assert = require('assert');
var yeoman = require('..');
var helpers = yeoman.test;

describe('yeoman.test', function () {
'use strict';

beforeEach(function () {
beforeEach( function () {
process.chdir(path.join(__dirname, './fixtures'));
var self = this;
this.StubGenerator = function (args, options) {
self.args = args;
self.options = options;
};

util.inherits(this.StubGenerator, yeoman.Base);
});

Expand All @@ -40,4 +42,143 @@ describe('yeoman.test', function () {
assert.equal(this.options.ui, 'tdd');
});
});

describe('#assertFile', function () {

it('accept a file that exists', function () {
assert.doesNotThrow( helpers.assertFile.bind(helpers, 'testFile') );
});

it('accept an array of files all of which exist', function () {
assert.doesNotThrow( helpers.assertFile.bind(helpers, ['testFile', 'testFile2']) );
});

it('reject a file that does not exist', function () {
assert.throws( helpers.assertFile.bind(helpers, 'etherealTestFile') );
});

it('reject multiple files one of which does not exist', function () {
assert.throws( helpers.assertFile.bind(helpers, ['testFile', 'intangibleTestFile']) );
});

// DEPRECATED

it('accept a file with content that matches reg', function () {
assert.doesNotThrow( helpers.assertFile.bind(helpers, 'testFile', /Roses are red/) );
});

it('reject a file with content does not match reg', function () {
assert.throws( helpers.assertFile.bind(helpers, 'testFile', /Roses are blue/) );
});

});

describe('#assertNoFile', function () {

it('accept a file that does not exist', function () {
assert.doesNotThrow( helpers.assertNoFile.bind(helpers, 'etherealTestFile') );
});

it('accept an array of files all of which do not exist', function () {
assert.doesNotThrow(
helpers.assertNoFile.bind(helpers, ['etherealTestFile', 'intangibleTestFile']));
});

it('reject a file that exists', function() {
assert.throws( helpers.assertNoFile.bind(helpers, 'testFile') );
});

it('reject an array of files one of which exists', function () {
assert.throws(
helpers.assertNoFile.bind(helpers, ['testFile', 'etherealTestFile']));
});

});

describe('#assertFiles', function () { // DEPRECATED

it('accept an array of files all of which exist', function () {
assert.doesNotThrow(
helpers.assertFiles.bind(helpers, ['testFile', 'testFile2']));
});

it('reject an array of multiple files one of which exists', function () {
assert.throws(
helpers.assertFiles.bind(helpers, ['testFile', 'etherealTestFile']));
});

it('accept an array of file/regex pairs when each file\'s content matches the corresponding regex',
function () {
assert.doesNotThrow(
helpers.assertFiles.bind(helpers,
[['testFile', /Roses are red/],
['testFile2', /Violets are blue/]]));
});

it('reject an array of file/regex pairs when one file\'s content does not matches the corresponding regex',
function () {
assert.throws(
helpers.assertFiles.bind(helpers,
[['testFile', /Roses are red/],
['testFile2', /Violets are orange/]]));
});

});

describe('#assertFileContent', function () {

it('accept a file and regex when the file content matches the regex', function () {
assert.doesNotThrow( helpers.assertFileContent.bind(helpers, 'testFile', /Roses are red/) );
});

it('reject a file and regex when the file content does not match the regex', function () {
assert.throws( helpers.assertFileContent.bind(helpers, 'testFile', /Roses are blue/) );
});

it('accept an array of file/regex pairs when each file\'s content matches the corresponding regex',
function () {
assert.doesNotThrow(
helpers.assertFileContent.bind(helpers,
[['testFile', /Roses are red/],
['testFile2', /Violets are blue/]]));
});

it('reject an array of file/regex pairs when one file\'s content does not matches the corresponding regex',
function () {
assert.throws(
helpers.assertFileContent.bind(helpers,
[['testFile', /Roses are red/],
['testFile2', /Violets are orange/]]));
});

});

describe('#assertNoFileContent', function () {

it('accept a file and regex when the file content does not match the regex', function () {
assert.doesNotThrow( helpers.assertNoFileContent.bind(helpers, 'testFile', /Roses are blue/) );
});

it('reject a file and regex when the file content matches the regex', function () {
assert.throws( helpers.assertNoFileContent.bind(helpers, 'testFile', /Roses are red/) );
});

it('accept an array of file/regex pairs when each file\'s content does not match its corresponding regex',
function () {
assert.doesNotThrow(
helpers.assertNoFileContent.bind(helpers,
[['testFile', /Roses are green/],
['testFile2', /Violets are orange/]]));
});

it('reject an array of file/regex pairs when one file\'s content does matches its corresponding regex',
function () {
assert.throws(
helpers.assertNoFileContent.bind(helpers,
[['testFile', /Roses are red/],
['testFile2', /Violets are orange/]]));
});

});

});

0 comments on commit 8bd843d

Please sign in to comment.