Skip to content

Commit

Permalink
Fix missing errors when expect.real([])
Browse files Browse the repository at this point in the history
  • Loading branch information
kotas committed Feb 2, 2014
1 parent ebd274b commit c1b4827
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
7 changes: 3 additions & 4 deletions index.js
Expand Up @@ -45,14 +45,13 @@ function expect(options, expectation) {
var numTests = 0, numPasses = 0, numFailures = 0;

function eachFile(file, encoding, done) {
var originalFile = file;

numTests++;

// To fix relative path to be based on cwd (where gulpfile.js exists)
var originalFile = file;
file = originalFile.clone();
file.base = file.cwd;

numTests++;

var _this = this;
fileTester.test(file, function (err) {
if (err && !options.reportUnexpected) {
Expand Down
19 changes: 13 additions & 6 deletions lib/file-tester.js
Expand Up @@ -14,9 +14,7 @@ module.exports = FileTester;

function FileTester(expectation, options) {
this.rules = parseExpectation(expectation);
if (options && options.checkRealFile) {
this.rules.push(new Rule(null, checkFileExists));
}
this.options = xtend({ checkRealFile: false }, options);
}

function parseExpectation(expectation) {
Expand Down Expand Up @@ -49,6 +47,7 @@ function checkFileExists(file, callback) {
}

FileTester.prototype.test = function (file, callback) {
var _this = this;
var matchedAny = false;
async.eachSeries(this.rules, function (rule, next) {
if (!rule.matchFilePath(file.relative)) {
Expand All @@ -57,10 +56,18 @@ FileTester.prototype.test = function (file, callback) {
matchedAny = true;
rule.testFile(file, next);
}, function (err) {
if (!err && !matchedAny) {
err = new ExpectationError('unexpected');
if (err) {
return callback(err);
}
if (!matchedAny) {
return callback(new ExpectationError('unexpected'));
}

if (_this.options.checkRealFile) {
checkFileExists(file, callback);
} else {
callback();
}
callback(err);
});
};

Expand Down
11 changes: 10 additions & 1 deletion test/file-tester-spec.js
Expand Up @@ -52,6 +52,15 @@ describe('FileTester', function () {
});
});

context('with an empty array', function () {
var tester = new FileTester([]);

it('should always fail as unexpected', function (done) {
var file = createFile('foo.txt');
tester.test(file, done.expectFail('unexpected'));
});
});

context('with a hash of file name key and true', function () {
var tester = new FileTester({
'foo.txt': true,
Expand Down Expand Up @@ -154,7 +163,7 @@ describe('FileTester', function () {
});

it('should fail if the file not exists', function (done) {
var tester = new FileTester('notexists.txt', { checkRealFile: true });
var tester = new FileTester('nonexists.txt', { checkRealFile: true });
var file = createFile('nonexists.txt');
tester.test(file, done.expectFail('not on filesystem'));
});
Expand Down
24 changes: 24 additions & 0 deletions test/gulp-plugin-spec.js
Expand Up @@ -65,6 +65,23 @@ describe('gulp-expect-file', function () {
});
});

context('with empty array', function () {
it('tests no files in stream', function (done) {
gutil.log.expect(/PASS/);
var stream = expect([]);
testStream(stream, done);
stream.end();
});

it('fails if any file is in stream', function (done) {
gutil.log.expect(/FAIL: foo\.txt is unexpected/);
var stream = expect([]);
testStream(stream, done);
stream.write(createFile('foo.txt'));
stream.end();
});
});

context('with { reportUnexpected: true }', function () {
it('should report unexpected files', function (done) {
gutil.log.expect(/FAIL: bar\.txt is unexpected/);
Expand Down Expand Up @@ -171,6 +188,13 @@ describe('gulp-expect-file', function () {
stream.write(createFile('nonexists.txt'));
stream.end();
});

it('passes with no files', function (done) {
gutil.log.expect(/PASS/);
var stream = expect.real([]);
testStream(stream, done);
stream.end();
});
});

});

0 comments on commit c1b4827

Please sign in to comment.