Skip to content

Commit 31b268e

Browse files
committed
Refactor API
* extract files processing from api * extract test-data class from API * fix breakage from rebase * pass testData to reporters in every method * rename testData to runStatus * runStatus.listenToTestRun => runStatus.observeFork * rename test-data.js to run-status.js * fix failing watcher test
1 parent 1e0b1c1 commit 31b268e

File tree

14 files changed

+775
-631
lines changed

14 files changed

+775
-631
lines changed

api.js

+128-310
Large diffs are not rendered by default.

cli.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ var logger = new Logger(reporter);
142142

143143
logger.start();
144144

145-
api.on('test', logger.test);
146-
api.on('error', logger.unhandledError);
145+
api.on('test-run', function (runStatus) {
146+
reporter.api = runStatus;
147+
runStatus.on('test', logger.test);
148+
runStatus.on('error', logger.unhandledError);
147149

148-
api.on('stdout', logger.stdout);
149-
api.on('stderr', logger.stderr);
150+
runStatus.on('stdout', logger.stdout);
151+
runStatus.on('stderr', logger.stderr);
152+
});
150153

151154
var files = cli.input.length ? cli.input : arrify(conf.files);
152155
if (files.length === 0) {
@@ -173,9 +176,9 @@ if (cli.flags.watch) {
173176
}
174177
} else {
175178
api.run(files)
176-
.then(function () {
177-
logger.finish();
178-
logger.exit(api.failCount > 0 || api.rejectionCount > 0 || api.exceptionCount > 0 ? 1 : 0);
179+
.then(function (runStatus) {
180+
logger.finish(runStatus);
181+
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
179182
})
180183
.catch(function (err) {
181184
// Don't swallow exceptions. Note that any expected error should already

lib/ava-files.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var Promise = require('bluebird');
4+
var slash = require('slash');
5+
var globby = require('globby');
6+
var flatten = require('arr-flatten');
7+
8+
function defaultExcludePatterns() {
9+
return [
10+
'!**/node_modules/**',
11+
'!**/fixtures/**',
12+
'!**/helpers/**'
13+
];
14+
}
15+
16+
function defaultIncludePatterns() {
17+
return [
18+
'test.js',
19+
'test-*.js',
20+
'test'
21+
];
22+
}
23+
24+
function AvaFiles(files) {
25+
if (!(this instanceof AvaFiles)) {
26+
throw new TypeError('Class constructor AvaFiles cannot be invoked without \'new\'');
27+
}
28+
29+
if (!files || !files.length) {
30+
files = defaultIncludePatterns();
31+
}
32+
33+
this.excludePatterns = defaultExcludePatterns();
34+
35+
this.files = files;
36+
}
37+
38+
AvaFiles.prototype.findTestFiles = function () {
39+
return handlePaths(this.files, this.excludePatterns);
40+
};
41+
42+
function handlePaths(files, excludePatterns) {
43+
// convert pinkie-promise to Bluebird promise
44+
files = Promise.resolve(globby(files.concat(excludePatterns)));
45+
46+
return files
47+
.map(function (file) {
48+
if (fs.statSync(file).isDirectory()) {
49+
var pattern = path.join(file, '**', '*.js');
50+
51+
if (process.platform === 'win32') {
52+
// Always use / in patterns, harmonizing matching across platforms.
53+
pattern = slash(pattern);
54+
}
55+
56+
return handlePaths([pattern], excludePatterns);
57+
}
58+
59+
// globby returns slashes even on Windows. Normalize here so the file
60+
// paths are consistently platform-accurate as tests are run.
61+
return path.normalize(file);
62+
})
63+
.then(flatten)
64+
.filter(function (file) {
65+
return path.extname(file) === '.js' && path.basename(file)[0] !== '_';
66+
})
67+
.map(function (file) {
68+
return path.resolve(file);
69+
});
70+
}
71+
72+
module.exports = AvaFiles;
73+
module.exports.defaultIncludePatterns = defaultIncludePatterns;
74+
module.exports.defaultExcludePatterns = defaultExcludePatterns;

lib/logger.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,64 @@ function Logger(reporter) {
1414

1515
module.exports = Logger;
1616

17-
Logger.prototype.start = function () {
17+
Logger.prototype.start = function (runStatus) {
1818
if (!this.reporter.start) {
1919
return;
2020
}
2121

22-
this.write(this.reporter.start());
22+
this.write(this.reporter.start(runStatus), runStatus);
2323
};
2424

25-
Logger.prototype.reset = function () {
25+
Logger.prototype.reset = function (runStatus) {
2626
if (!this.reporter.reset) {
2727
return;
2828
}
2929

30-
this.write(this.reporter.reset());
30+
this.write(this.reporter.reset(runStatus), runStatus);
3131
};
3232

33-
Logger.prototype.test = function (test) {
34-
this.write(this.reporter.test(test));
33+
Logger.prototype.test = function (test, runStatus) {
34+
this.write(this.reporter.test(test, runStatus), runStatus);
3535
};
3636

37-
Logger.prototype.unhandledError = function (err) {
37+
Logger.prototype.unhandledError = function (err, runStatus) {
3838
if (!this.reporter.unhandledError) {
3939
return;
4040
}
4141

42-
this.write(this.reporter.unhandledError(err));
42+
this.write(this.reporter.unhandledError(err, runStatus), runStatus);
4343
};
4444

45-
Logger.prototype.finish = function () {
45+
Logger.prototype.finish = function (runStatus) {
4646
if (!this.reporter.finish) {
4747
return;
4848
}
4949

50-
this.write(this.reporter.finish());
50+
this.write(this.reporter.finish(runStatus), runStatus);
5151
};
5252

53-
Logger.prototype.write = function (str) {
53+
Logger.prototype.write = function (str, runStatus) {
5454
if (typeof str === 'undefined') {
5555
return;
5656
}
5757

58-
this.reporter.write(str);
58+
this.reporter.write(str, runStatus);
5959
};
6060

61-
Logger.prototype.stdout = function (data) {
61+
Logger.prototype.stdout = function (data, runStatus) {
6262
if (!this.reporter.stdout) {
6363
return;
6464
}
6565

66-
this.reporter.stdout(data);
66+
this.reporter.stdout(data, runStatus);
6767
};
6868

69-
Logger.prototype.stderr = function (data) {
69+
Logger.prototype.stderr = function (data, runStatus) {
7070
if (!this.reporter.stderr) {
7171
return;
7272
}
7373

74-
this.reporter.stderr(data);
74+
this.reporter.stderr(data, runStatus);
7575
};
7676

7777
Logger.prototype.exit = function (code) {

lib/reporters/mini.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ MiniReporter.prototype.reportCounts = function () {
135135
return status;
136136
};
137137

138-
MiniReporter.prototype.finish = function () {
138+
MiniReporter.prototype.finish = function (runStatus) {
139139
this.clearInterval();
140140

141141
var status = this.reportCounts();
@@ -151,7 +151,7 @@ MiniReporter.prototype.finish = function () {
151151
var i = 0;
152152

153153
if (this.failCount > 0) {
154-
this.api.errors.forEach(function (test) {
154+
runStatus.errors.forEach(function (test) {
155155
if (!test.error || !test.error.message) {
156156
return;
157157
}
@@ -173,7 +173,7 @@ MiniReporter.prototype.finish = function () {
173173
}
174174

175175
if (this.rejectionCount > 0 || this.exceptionCount > 0) {
176-
this.api.errors.forEach(function (err) {
176+
runStatus.errors.forEach(function (err) {
177177
if (err.title) {
178178
return;
179179
}

lib/reporters/tap.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ TapReporter.prototype.unhandledError = function (err) {
7575
return output.join('\n');
7676
};
7777

78-
TapReporter.prototype.finish = function () {
78+
TapReporter.prototype.finish = function (runStatus) {
7979
var output = [
8080
'',
81-
'1..' + (this.api.passCount + this.api.failCount + this.api.skipCount),
82-
'# tests ' + (this.api.passCount + this.api.failCount + this.api.skipCount),
83-
'# pass ' + this.api.passCount
81+
'1..' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
82+
'# tests ' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
83+
'# pass ' + runStatus.passCount
8484
];
8585

86-
if (this.api.skipCount > 0) {
87-
output.push('# skip ' + this.api.skipCount);
86+
if (runStatus.skipCount > 0) {
87+
output.push('# skip ' + runStatus.skipCount);
8888
}
8989

90-
output.push('# fail ' + (this.api.failCount + this.api.rejectionCount + this.api.exceptionCount), '');
90+
output.push('# fail ' + (runStatus.failCount + runStatus.rejectionCount + runStatus.exceptionCount), '');
9191

9292
return output.join('\n');
9393
};

lib/reporters/verbose.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ VerboseReporter.prototype.start = function () {
2020
return '';
2121
};
2222

23-
VerboseReporter.prototype.test = function (test) {
23+
VerboseReporter.prototype.test = function (test, runStatus) {
2424
if (test.error) {
2525
return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message);
2626
}
@@ -31,7 +31,7 @@ VerboseReporter.prototype.test = function (test) {
3131
return ' ' + colors.skip('- ' + test.title);
3232
}
3333

34-
if (this.api.fileCount === 1 && this.api.testCount === 1 && test.title === '[anonymous]') {
34+
if (runStatus.fileCount === 1 && runStatus.testCount === 1 && test.title === '[anonymous]') {
3535
return undefined;
3636
}
3737

@@ -65,37 +65,37 @@ VerboseReporter.prototype.unhandledError = function (err) {
6565
return output;
6666
};
6767

68-
VerboseReporter.prototype.finish = function () {
68+
VerboseReporter.prototype.finish = function (runStatus) {
6969
var output = '\n';
7070

71-
if (this.api.failCount > 0) {
72-
output += ' ' + colors.error(this.api.failCount, plur('test', this.api.failCount), 'failed') + '\n';
71+
if (runStatus.failCount > 0) {
72+
output += ' ' + colors.error(runStatus.failCount, plur('test', runStatus.failCount), 'failed') + '\n';
7373
} else {
74-
output += ' ' + colors.pass(this.api.passCount, plur('test', this.api.passCount), 'passed') + '\n';
74+
output += ' ' + colors.pass(runStatus.passCount, plur('test', runStatus.passCount), 'passed') + '\n';
7575
}
7676

77-
if (this.api.skipCount > 0) {
78-
output += ' ' + colors.skip(this.api.skipCount, plur('test', this.api.skipCount), 'skipped') + '\n';
77+
if (runStatus.skipCount > 0) {
78+
output += ' ' + colors.skip(runStatus.skipCount, plur('test', runStatus.skipCount), 'skipped') + '\n';
7979
}
8080

81-
if (this.api.todoCount > 0) {
82-
output += ' ' + colors.todo(this.api.todoCount, plur('test', this.api.todoCount), 'todo') + '\n';
81+
if (runStatus.todoCount > 0) {
82+
output += ' ' + colors.todo(runStatus.todoCount, plur('test', runStatus.todoCount), 'todo') + '\n';
8383
}
8484

85-
if (this.api.rejectionCount > 0) {
86-
output += ' ' + colors.error(this.api.rejectionCount, 'unhandled', plur('rejection', this.api.rejectionCount)) + '\n';
85+
if (runStatus.rejectionCount > 0) {
86+
output += ' ' + colors.error(runStatus.rejectionCount, 'unhandled', plur('rejection', runStatus.rejectionCount)) + '\n';
8787
}
8888

89-
if (this.api.exceptionCount > 0) {
90-
output += ' ' + colors.error(this.api.exceptionCount, 'uncaught', plur('exception', this.api.exceptionCount)) + '\n';
89+
if (runStatus.exceptionCount > 0) {
90+
output += ' ' + colors.error(runStatus.exceptionCount, 'uncaught', plur('exception', runStatus.exceptionCount)) + '\n';
9191
}
9292

93-
if (this.api.failCount > 0) {
93+
if (runStatus.failCount > 0) {
9494
output += '\n';
9595

9696
var i = 0;
9797

98-
this.api.tests.forEach(function (test) {
98+
runStatus.tests.forEach(function (test) {
9999
if (!(test.error && test.error.message)) {
100100
return;
101101
}

0 commit comments

Comments
 (0)