From f0f25b52ab0f43ac2438e78283b021cd9e3c8c58 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Sat, 15 Oct 2016 18:35:31 +0300 Subject: [PATCH] precompile helpers and fixtures --- api.js | 44 +++++++++++++++++++++++++++++++------------- package.json | 2 +- test/api.js | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/api.js b/api.js index 92ccfcebd..e16a81478 100644 --- a/api.js +++ b/api.js @@ -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, { @@ -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({ @@ -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) { diff --git a/package.json b/package.json index f16a20568..93fb6f2f5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/api.js b/test/api.js index e40e7d586..7eb541a24 100644 --- a/test/api.js +++ b/test/api.js @@ -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); @@ -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();