Skip to content

Commit

Permalink
cache by test file content and ava version hash
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Nov 15, 2015
1 parent 9740986 commit eb53eab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ require('./lib/babel').avaRequired();
var setImmediate = require('set-immediate-shim');
var hasFlag = require('has-flag');
var chalk = require('chalk');
var join = require('path').join;
var relative = require('path').relative;
var serializeError = require('destroy-circular');
var Runner = require('./lib/runner');
var log = require('./lib/logger');
var runner = new Runner();
var xdgBasedir = require('xdg-basedir');
var cache = require('cacha')(join(xdgBasedir.cache, 'ava'));
var Configstore = require('configstore');
var config = new Configstore('ava', {
lastCacheClean: Date.now()
});
var WEEK = 7 * 24 * 60 * 60 * 1000;

if (Date.now() - config.get('lastCacheClean') > WEEK) {
config.set('lastCacheClean', Date.now());
cache.clean();
}

// check if the test is being run without AVA cli
var isForked = typeof process.send === 'function';
Expand Down
33 changes: 24 additions & 9 deletions lib/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
var resolveFrom = require('resolve-from');
var createEspowerPlugin = require('babel-plugin-espower/create');
var requireFromString = require('require-from-string');
var hasha = require('hasha');
var hashObj = require('hash-obj');
var xdgBasedir = require('xdg-basedir');
var path = require('path');
var cache = require('cacha')(path.join(xdgBasedir.cache, 'ava'));

var hasGenerators = parseInt(process.version.slice(1), 10) > 0;
var testPath = process.argv[2];
Expand All @@ -24,16 +29,32 @@ var options = {
]
};

var avaRequired;
process.on('message', function (message) {
if (message['ava-kill-command']) {
process.exit(0);
}
});

var hash = hashObj({
code: hasha.fromFileSync(testPath),
avaVersion: require('../package.json').version
});

var code = cache.getSync(hash, 'utf8');

if (code === undefined) {
code = babel.transformFileSync(testPath, options).code;
cache.setSync(hash, code, 'utf8');
}

var avaRequired;
module.exports = {
avaRequired: function () {
avaRequired = true;
}
};

var transpiled = babel.transformFileSync(testPath, options);
requireFromString(transpiled.code, testPath, {
requireFromString(code, testPath, {
appendPaths: module.paths
});

Expand All @@ -43,9 +64,3 @@ if (!avaRequired) {
process.exit(1);
});
}

process.on('message', function (message) {
if (message['ava-kill-command']) {
process.exit(0);
}
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@
"babel-plugin-espower": "^1.1.0",
"babel-runtime": "^5.8.29",
"bluebird": "^3.0.0",
"cacha": "^1.0.3",
"chalk": "^1.0.0",
"co-with-promise": "^4.6.0",
"configstore": "^1.3.0",
"core-assert": "^0.1.0",
"destroy-circular": "jamestalmage/destroy-circular#feeb7d1",
"empower": "^1.1.0",
"figures": "^1.4.0",
"fn-name": "^2.0.0",
"globby": "^3.0.1",
"has-flag": "^1.0.0",
"hash-obj": "^1.0.0",
"hasha": "^2.0.2",
"is-generator": "^1.0.2",
"meow": "^3.3.0",
"plur": "^2.0.0",
Expand All @@ -86,7 +90,8 @@
"resolve-from": "^1.0.0",
"set-immediate-shim": "^1.0.1",
"squeak": "^1.2.0",
"update-notifier": "^0.5.0"
"update-notifier": "^0.5.0",
"xdg-basedir": "^2.0.0"
},
"devDependencies": {
"coveralls": "^2.11.4",
Expand Down

0 comments on commit eb53eab

Please sign in to comment.