Skip to content

Commit

Permalink
Merge pull request #54 from davidtheclark/tape-tests
Browse files Browse the repository at this point in the history
Switch AVA to tape; update deps
  • Loading branch information
davidtheclark committed Jan 7, 2017
2 parents 833385b + 3696d7a commit e8edba9
Show file tree
Hide file tree
Showing 9 changed files with 2,229 additions and 159 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,3 +3,4 @@ node_js:
- "0.12"
- "4"
- "6"
- "7"
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -9,9 +9,9 @@
],
"scripts": {
"lint": "node-version-gte-4 && eslint . || echo \"ESLint not supported\"",
"ava": "ava test/*.test.js",
"coverage": "nyc npm run ava && nyc report --reporter=html && open coverage/index.html",
"test": "npm run ava && npm run lint"
"tape": "tape test/*.test.js",
"coverage": "nyc npm run tape && nyc report --reporter=html && open coverage/index.html",
"test": "npm run tape && npm run lint"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,15 +40,15 @@
"require-from-string": "^1.1.0"
},
"devDependencies": {
"ava": "0.16.0",
"eslint": "3.5.0",
"eslint": "^3.13.0",
"eslint-config-davidtheclark-node": "^0.2.0",
"eslint-plugin-node": "^2.0.0",
"eslint-plugin-node": "^3.0.5",
"expect": "^1.20.2",
"lodash": "4.16.1",
"lodash": "^4.17.4",
"node-version-check": "^2.1.1",
"nyc": "^8.3.0",
"sinon": "1.17.6"
"nyc": "^10.0.0",
"sinon": "^1.17.7",
"tape": "^4.6.3"
},
"engines": {
"node": ">=0.12"
Expand Down
4 changes: 2 additions & 2 deletions test/assertSearchSequence.js
Expand Up @@ -5,9 +5,9 @@ var path = require('path');

module.exports = function (assert, readFileStub, searchPaths, startCount) {
startCount = startCount || 0;
assert.is(readFileStub.callCount, searchPaths.length + startCount);
assert.equal(readFileStub.callCount, searchPaths.length + startCount);
searchPaths.forEach(function (searchPath, i) {
assert.is(
assert.equal(
_.get(readFileStub.getCall(i + startCount), 'args[0]'),
path.join(__dirname, searchPath),
'checked ' + searchPath
Expand Down
103 changes: 71 additions & 32 deletions test/caches.test.js
@@ -1,6 +1,6 @@
'use strict';

var test = require('ava');
var test = require('tape');
var sinon = require('sinon');
var path = require('path');
var fs = require('fs');
Expand All @@ -23,13 +23,11 @@ function statStubIsDirectory(result) {
});
}

test.before(function () {
cachedLoadConfig = cosmiconfig('foo').load;
});
cachedLoadConfig = cosmiconfig('foo').load;

// The tests below rely both on this directory structure and on the
// order in which they run!
test.beforeEach(function () {
function setup() {
readFileStub = sinon.stub(fs, 'readFile', function (searchPath, encoding, callback) {
switch (searchPath) {
case absolutePath('a/b/c/d/e/f/package.json'):
Expand Down Expand Up @@ -59,14 +57,16 @@ test.beforeEach(function () {
callback(new Error('irrelevant path ' + searchPath));
}
});
});
}

test.afterEach(function () {
function teardown(assert, err) {
if (readFileStub.restore) readFileStub.restore();
if (statStub.restore) statStub.restore();
});
assert.end(err);
}

test.serial('does not use cache at first', function (assert) {
test('does not use cache at first', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d/e');
statStubIsDirectory(true);

Expand All @@ -75,7 +75,7 @@ test.serial('does not use cache at first', function (assert) {
config: { foundInD: true },
};

return cachedLoadConfig(searchPath).then(function (result) {
cachedLoadConfig(searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
'a/b/c/d/e/package.json',
'a/b/c/d/e/.foorc',
Expand All @@ -84,10 +84,14 @@ test.serial('does not use cache at first', function (assert) {
'a/b/c/d/.foorc',
]);
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('uses cache for already-visited directories', function (assert) {
test('uses cache for already-visited directories', function (assert) {
setup();
// E and D visited above
var searchPath = absolutePath('a/b/c/d/e');
statStubIsDirectory(true);
Expand All @@ -97,13 +101,17 @@ test.serial('uses cache for already-visited directories', function (assert) {
config: { foundInD: true },
};

return cachedLoadConfig(searchPath).then(function (result) {
assert.is(readFileStub.callCount, 0, 'no new calls');
cachedLoadConfig(searchPath).then(function (result) {
assert.equal(readFileStub.callCount, 0, 'no new calls');
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('uses cache for file in already-visited directories', function (assert) {
test('uses cache for file in already-visited directories', function (assert) {
setup();
// E and D visited above
var searchPath = absolutePath('a/b/c/d/e/foo.js');
statStubIsDirectory(false);
Expand All @@ -113,13 +121,17 @@ test.serial('uses cache for file in already-visited directories', function (asse
config: { foundInD: true },
};

return cachedLoadConfig(searchPath).then(function (result) {
assert.is(readFileStub.callCount, 0, 'no new calls');
cachedLoadConfig(searchPath).then(function (result) {
assert.equal(readFileStub.callCount, 0, 'no new calls');
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('uses cache when some directories in search were already visted', function (assert) {
test('uses cache when some directories in search were already visted', function (assert) {
setup();
// E and D visited above, not F
var searchPath = absolutePath('a/b/c/d/e/f');
statStubIsDirectory(true);
Expand All @@ -129,17 +141,21 @@ test.serial('uses cache when some directories in search were already visted', fu
config: { foundInD: true },
};

return cachedLoadConfig(searchPath).then(function (result) {
cachedLoadConfig(searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
'a/b/c/d/e/f/package.json',
'a/b/c/d/e/f/.foorc',
'a/b/c/d/e/f/foo.config.js',
]);
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('does not use cache for unvisited config file', function (assert) {
test('does not use cache for unvisited config file', function (assert) {
setup();
// B not yet visited
var configFile = absolutePath( 'a/b/package.json');
statStubIsDirectory(false);
Expand All @@ -151,13 +167,17 @@ test.serial('does not use cache for unvisited config file', function (assert) {
},
};

return cachedLoadConfig(null, configFile).then(function (result) {
assert.is(readFileStub.callCount, 1, 'uses readFile once for reading, no cache');
cachedLoadConfig(null, configFile).then(function (result) {
assert.equal(readFileStub.callCount, 1, 'uses readFile once for reading, no cache');
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('does not use cache with a new cosmiconfig instance', function (assert) {
test('does not use cache with a new cosmiconfig instance', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d/e');
statStubIsDirectory(true);

Expand All @@ -168,7 +188,7 @@ test.serial('does not use cache with a new cosmiconfig instance', function (asse

var loadConfig = cosmiconfig('foo').load;

return loadConfig(searchPath).then(function (result) {
loadConfig(searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
'a/b/c/d/e/package.json',
'a/b/c/d/e/.foorc',
Expand All @@ -177,10 +197,14 @@ test.serial('does not use cache with a new cosmiconfig instance', function (asse
'a/b/c/d/.foorc',
]);
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('but cache on old instance still works', function (assert) {
test('but cache on old instance still works', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d/e');
statStubIsDirectory(true);

Expand All @@ -189,13 +213,17 @@ test.serial('but cache on old instance still works', function (assert) {
config: { foundInD: true },
};

return cachedLoadConfig(searchPath).then(function (result) {
assert.is(readFileStub.callCount, 0, 'no file reading!');
cachedLoadConfig(searchPath).then(function (result) {
assert.equal(readFileStub.callCount, 0, 'no file reading!');
assert.deepEqual(result, expectedResult);
teardown(assert);
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('does not cache if you say no', function (assert) {
test('does not cache if you say no', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d');
statStubIsDirectory(true);

Expand All @@ -209,7 +237,7 @@ test.serial('does not cache if you say no', function (assert) {
}).load;

// Same call three times hits the file system every time
return Promise.resolve()
Promise.resolve()
.then(function () {
return loadConfig(searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
Expand All @@ -235,11 +263,15 @@ test.serial('does not cache if you say no', function (assert) {
'a/b/c/d/.foorc',
], 4);
assert.deepEqual(result, expectedResult);
teardown(assert);
});
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('clearFileCache', function (assert) {
test('clearFileCache', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d/.foorc');
statStubIsDirectory(false);
var expectedResult = {
Expand All @@ -248,7 +280,7 @@ test.serial('clearFileCache', function (assert) {
};
var explorer = cosmiconfig('foo');

return Promise.resolve()
Promise.resolve()
.then(function () {
return explorer.load(null, searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
Expand All @@ -275,11 +307,15 @@ test.serial('clearFileCache', function (assert) {
'a/b/c/d/.foorc',
], 0);
assert.deepEqual(result, expectedResult);
teardown(assert);
});
}).catch(function (err) {
teardown(assert, err);
});
});

test.serial('clearDirectoryCache', function (assert) {
test('clearDirectoryCache', function (assert) {
setup();
var searchPath = absolutePath('a/b/c/d/e');
statStubIsDirectory(true);
var expectedResult = {
Expand All @@ -288,7 +324,7 @@ test.serial('clearDirectoryCache', function (assert) {
};
var explorer = cosmiconfig('foo');

return Promise.resolve()
Promise.resolve()
.then(function () {
return explorer.load(searchPath).then(function (result) {
assertSearchSequence(assert, readFileStub, [
Expand Down Expand Up @@ -331,6 +367,9 @@ test.serial('clearDirectoryCache', function (assert) {
'a/b/c/d/.foorc',
], 0);
assert.deepEqual(result, expectedResult);
teardown(assert);
});
}).catch(function (err) {
teardown(assert, err);
});
});

0 comments on commit e8edba9

Please sign in to comment.