Skip to content

Commit a3b5e89

Browse files
committed
cache by test file content and ava version hash
1 parent 60b2337 commit a3b5e89

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

index.js

+14
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ var setImmediate = require('set-immediate-shim');
33
var relative = require('path').relative;
44
var hasFlag = require('has-flag');
55
var chalk = require('chalk');
6+
var join = require('path').join;
7+
var relative = require('path').relative;
68
var serializeError = require('./lib/serialize-value');
79
var Runner = require('./lib/runner');
810
var send = require('./lib/send');
911
var log = require('./lib/logger');
1012

1113
var runner = new Runner();
14+
var xdgBasedir = require('xdg-basedir');
15+
var cache = require('cacha')(join(xdgBasedir.cache, 'ava'));
16+
var Configstore = require('configstore');
17+
var config = new Configstore('ava', {
18+
lastCacheClean: Date.now()
19+
});
20+
var WEEK = 7 * 24 * 60 * 60 * 1000;
21+
22+
if (Date.now() - config.get('lastCacheClean') > WEEK) {
23+
config.set('lastCacheClean', Date.now());
24+
cache.clean();
25+
}
1226

1327
// note that test files have require('ava')
1428
require('./lib/babel').avaRequired = true;

lib/babel.js

+34-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ var resolveFrom = require('resolve-from');
66
var hasGenerator = require('has-generator');
77
var serializeValue = require('./serialize-value');
88
var send = require('./send');
9+
var hasha = require('hasha');
10+
var hashObj = require('hash-obj');
11+
var xdgBasedir = require('xdg-basedir');
12+
var path = require('path');
13+
var cache = require('cacha')(path.join(xdgBasedir.cache, 'ava'));
914

1015
var testPath = process.argv[2];
1116

@@ -31,24 +36,6 @@ var options = {
3136
plugins: [powerAssert]
3237
};
3338

34-
// check if test files required ava and show error, when they didn't
35-
exports.avaRequired = false;
36-
37-
process.on('uncaughtException', function (exception) {
38-
send('uncaughtException', {exception: serializeValue(exception)});
39-
});
40-
41-
// include test file
42-
var transpiled = babel.transformFileSync(testPath, options);
43-
requireFromString(transpiled.code, testPath, {
44-
appendPaths: module.paths
45-
});
46-
47-
// if ava was not required, show an error
48-
if (!exports.avaRequired) {
49-
throw new Error('No tests found in ' + testPath + ', make sure to import "ava" at the top of your test file');
50-
}
51-
5239
// parse and re-emit ava messages
5340
process.on('message', function (message) {
5441
if (!message.ava) {
@@ -82,6 +69,35 @@ process.on('ava-teardown', function () {
8269
setTimeout(exit, 100);
8370
});
8471

72+
process.on('uncaughtException', function (exception) {
73+
send('uncaughtException', {exception: serializeValue(exception)});
74+
});
75+
76+
var hash = hashObj({
77+
code: hasha.fromFileSync(testPath),
78+
avaVersion: require('../package.json').version
79+
});
80+
81+
var code = cache.getSync(hash, 'utf8');
82+
83+
if (code === undefined) {
84+
code = babel.transformFileSync(testPath, options).code;
85+
cache.setSync(hash, code, 'utf8');
86+
}
87+
88+
// check if test files required ava and show error, when they didn't
89+
exports.avaRequired = false;
90+
91+
// include test file
92+
requireFromString(code, testPath, {
93+
appendPaths: module.paths
94+
});
95+
96+
// if ava was not required, show an error
97+
if (!exports.avaRequired) {
98+
throw new Error('No tests found in ' + testPath + ', make sure to import "ava" at the top of your test file');
99+
}
100+
85101
function exit() {
86102
send('teardown');
87103
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
"babel-plugin-espower": "^1.1.0",
7878
"babel-runtime": "^5.8.29",
7979
"bluebird": "^3.0.0",
80+
"cacha": "^1.0.3",
8081
"chalk": "^1.0.0",
8182
"co-with-promise": "^4.6.0",
83+
"configstore": "^1.3.0",
8284
"core-assert": "^0.1.0",
8385
"debug": "^2.2.0",
8486
"destroy-circular": "jamestalmage/destroy-circular#feeb7d1",
@@ -88,6 +90,8 @@
8890
"globby": "^4.0.0",
8991
"has-flag": "^1.0.0",
9092
"has-generator": "^1.0.0",
93+
"hash-obj": "^1.0.0",
94+
"hasha": "^2.0.2",
9195
"is-generator-fn": "^1.0.0",
9296
"is-promise": "^2.1.0",
9397
"loud-rejection": "^1.2.0",
@@ -102,7 +106,8 @@
102106
"resolve-from": "^1.0.0",
103107
"set-immediate-shim": "^1.0.1",
104108
"squeak": "^1.2.0",
105-
"update-notifier": "^0.5.0"
109+
"update-notifier": "^0.5.0",
110+
"xdg-basedir": "^2.0.0"
106111
},
107112
"devDependencies": {
108113
"coveralls": "^2.11.4",

0 commit comments

Comments
 (0)