Skip to content

Commit

Permalink
precompile helpers and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Oct 23, 2016
1 parent 3ea2ba1 commit f0f25b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
44 changes: 31 additions & 13 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = Api;

Api.prototype._runFile = function (file, runStatus, execArgv) {
var hash = this.precompiler.precompileFile(file);
var precompiled = {};
var precompiled = objectAssign({}, this._precompiledHelpers);
precompiled[file] = hash;

var options = objectAssign({}, this.options, {
Expand Down Expand Up @@ -134,7 +134,22 @@ Api.prototype._setupPrecompiler = function (files) {
});
};

Api.prototype._precompileHelpers = function () {
var self = this;

this._precompiledHelpers = {};

return new AvaFiles({cwd: this.options.resolveTestsFrom})
.findTestHelpers()
.map(function (file) { // eslint-disable-line array-callback-return
var hash = self.precompiler.precompileFile(file);
self._precompiledHelpers[file] = hash;
});
};

Api.prototype._run = function (files, options) {
var self = this;

options = options || {};

var runStatus = new RunStatus({
Expand All @@ -154,20 +169,23 @@ Api.prototype._run = function (files, options) {

this._setupPrecompiler(files);

if (this.options.timeout) {
this._setupTimeout(runStatus);
}
return this._precompileHelpers()
.then(function () {
if (self.options.timeout) {
self._setupTimeout(runStatus);
}

var overwatch;
if (this.options.concurrency > 0) {
var concurrency = this.options.serial ? 1 : this.options.concurrency;
overwatch = this._runWithPool(files, runStatus, concurrency);
} else {
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
overwatch = this._runWithoutPool(files, runStatus);
}
var overwatch;
if (self.options.concurrency > 0) {
var concurrency = self.options.serial ? 1 : self.options.concurrency;
overwatch = self._runWithPool(files, runStatus, concurrency);
} else {
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
overwatch = self._runWithoutPool(files, runStatus);
}

return overwatch;
return overwatch;
});
};

Api.prototype._computeForkExecArgs = function (files) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"array-uniq": "^1.0.2",
"arrify": "^1.0.0",
"auto-bind": "^0.1.0",
"ava-files": "^0.2.0",
"ava-files": "avajs/ava-files#find-test-helpers",
"ava-init": "^0.1.0",
"babel-code-frame": "^6.16.0",
"babel-core": "^6.17.0",
Expand Down
14 changes: 14 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,16 @@ function generateTests(prefix, apiCreator) {
test(prefix + 'caching is enabled by default', function (t) {
t.plan(3);
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));

var prevCwd = process.cwd();
process.chdir(path.join(__dirname, 'fixture/caching'));

var api = apiCreator();

return api.run([path.join(__dirname, 'fixture/caching/test.js')])
.finally(function () {
process.chdir(prevCwd);
})
.then(function () {
var files = fs.readdirSync(path.join(__dirname, 'fixture/caching/node_modules/.cache/ava'));
t.is(files.length, 2);
Expand All @@ -728,9 +735,16 @@ function generateTests(prefix, apiCreator) {
test(prefix + 'caching can be disabled', function (t) {
t.plan(1);
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));

var prevCwd = process.cwd();
process.chdir(path.join(__dirname, 'fixture/caching'));

var api = apiCreator({cacheEnabled: false});

return api.run([path.join(__dirname, 'fixture/caching/test.js')])
.finally(function () {
process.chdir(prevCwd);
})
.then(function () {
t.false(fs.existsSync(path.join(__dirname, 'fixture/caching/node_modules/.cache/ava')));
t.end();
Expand Down

0 comments on commit f0f25b5

Please sign in to comment.