Skip to content

Properly resolve files from package.json dir. #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function Api(options) {

EventEmitter.call(this);

this.options = options || {};
this.options = objectAssign({}, options);
this.options.cwd = this.options.cwd || process.cwd();
this.options.resolveTestsFrom = this.options.resolveTestsFrom || this.options.cwd;
this.options.match = this.options.match || [];
this.options.require = (this.options.require || []).map(function (moduleId) {
var ret = resolveCwd(moduleId);
Expand Down Expand Up @@ -73,7 +75,7 @@ Api.prototype._onTimeout = function (runStatus) {
Api.prototype.run = function (files, options) {
var self = this;

return new AvaFiles(files)
return new AvaFiles({files: files, cwd: this.options.resolveTestsFrom})
.findTestFiles()
.then(function (files) {
return self._run(files, options);
Expand Down
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
'use strict';

var path = require('path');
var debug = require('debug')('ava');

// Prefer the local installation of AVA.
Expand Down Expand Up @@ -43,6 +44,8 @@ var conf = pkgConf.sync('ava', {
}
});

var pkgDir = path.dirname(pkgConf.filepath(conf));

// check for valid babel config shortcuts (can be either "default" or "inherit")
var isValidShortcut = ['default', 'inherit'].indexOf(conf.babel) !== -1;

Expand Down Expand Up @@ -136,6 +139,7 @@ var api = new Api({
explicitTitles: cli.flags.watch,
match: arrify(cli.flags.match),
babelConfig: conf.babel,
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
timeout: cli.flags.timeout,
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0
});
Expand Down
10 changes: 8 additions & 2 deletions lib/ava-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@ function defaultIncludePatterns() {
];
}

function AvaFiles(files, sources) {
function AvaFiles(options) {
if (!(this instanceof AvaFiles)) {
throw new TypeError('Class constructor AvaFiles cannot be invoked without \'new\'');
}

options = options || {};
var files = options.files;

if (!files || !files.length) {
files = defaultIncludePatterns();
}

this.excludePatterns = defaultExcludePatterns();

this.files = files;
this.sources = sources || [];
this.sources = options.sources || [];
this.cwd = options.cwd || process.cwd();
}

AvaFiles.prototype.findTestFiles = function () {
return handlePaths(this.files, this.excludePatterns, {
cwd: this.cwd,
cache: Object.create(null),
statCache: Object.create(null),
realpathCache: Object.create(null),
Expand Down Expand Up @@ -222,6 +227,7 @@ function handlePaths(files, excludePatterns, globOptions) {

return files
.map(function (file) {
file = path.resolve(globOptions.cwd, file);
if (fs.statSync(file).isDirectory()) {
if (alreadySearchingParent(file)) {
return null;
Expand Down
5 changes: 4 additions & 1 deletion lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ function rethrowAsync(err) {

function Watcher(logger, api, files, sources) {
this.debouncer = new Debouncer(this);
this.avaFiles = new AvaFiles(files, sources);
this.avaFiles = new AvaFiles({
files: files,
sources: sources
});

this.isTest = this.avaFiles.makeTestMatcher();

Expand Down
29 changes: 24 additions & 5 deletions test/ava-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('requires new', function (t) {
});

test('testMatcher', function (t) {
var avaFiles = new AvaFiles(['**/foo*']);
var avaFiles = new AvaFiles({files: ['**/foo*']});

var matcher = avaFiles.makeTestMatcher();

Expand Down Expand Up @@ -55,7 +55,7 @@ test('testMatcher', function (t) {
});

test('sourceMatcher - defaults', function (t) {
var avaFiles = new AvaFiles(['**/foo*']);
var avaFiles = new AvaFiles({files: ['**/foo*']});

var matcher = avaFiles.makeSourceMatcher();

Expand Down Expand Up @@ -90,7 +90,10 @@ test('sourceMatcher - defaults', function (t) {
});

test('sourceMatcher - allow matching specific node_modules directories', function (t) {
var avaFiles = new AvaFiles(['**/foo*'], ['node_modules/foo/**']);
var avaFiles = new AvaFiles({
files: ['**/foo*'],
sources: ['node_modules/foo/**']
});

var matcher = avaFiles.makeSourceMatcher();

Expand All @@ -100,7 +103,10 @@ test('sourceMatcher - allow matching specific node_modules directories', functio
});

test('sourceMatcher - providing negation patterns', function (t) {
var avaFiles = new AvaFiles(['**/foo*'], ['!**/bar*']);
var avaFiles = new AvaFiles({
files: ['**/foo*'],
sources: ['!**/bar*']
});

var matcher = avaFiles.makeSourceMatcher();

Expand All @@ -111,14 +117,27 @@ test('sourceMatcher - providing negation patterns', function (t) {
});

test('findFiles - does not return duplicates of the same file', function (t) {
var avaFiles = new AvaFiles(['**/ava-files/no-duplicates/**']);
var avaFiles = new AvaFiles({files: ['**/ava-files/no-duplicates/**']});

avaFiles.findTestFiles().then(function (files) {
t.is(files.length, 2);
t.end();
});
});

test('findFiles - honors cwd option', function (t) {
var avaFiles = new AvaFiles({
files: ['**/test/*.js'],
cwd: path.join(__dirname, 'fixture', 'ava-files', 'cwd', 'dir-b')
});

avaFiles.findTestFiles().then(function (files) {
t.is(files.length, 1);
t.is(path.basename(files[0]), 'baz.js');
t.end();
});
});

test('findFiles - finds the correct files by default', function (t) {
var fixtureDir = fixture('default-patterns');
process.chdir(fixtureDir);
Expand Down
33 changes: 33 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,39 @@ test('pkg-conf: cli takes precedence', function (t) {
});
});

test('pkg-conf(resolve-dir): works as expected when run from the package.json directory', function (t) {
execCli(['--verbose'], {dirname: 'fixture/pkg-conf/resolve-dir'}, function (err, stdout, stderr) {
t.ifError(err);
t.match(stderr, /dir-a-base-1/);
t.match(stderr, /dir-a-base-2/);
t.notMatch(stderr, /dir-a-wrapper/);
t.notMatch(stdout, /dir-a-wrapper/);
t.end();
});
});

test('pkg-conf(resolve-dir): resolves tests from the package.json dir if none are specified on cli', function (t) {
execCli(['--verbose'], {dirname: 'fixture/pkg-conf/resolve-dir/dir-a-wrapper'}, function (err, stdout, stderr) {
t.ifError(err);
t.match(stderr, /dir-a-base-1/);
t.match(stderr, /dir-a-base-2/);
t.notMatch(stderr, /dir-a-wrapper/);
t.notMatch(stdout, /dir-a-wrapper/);
t.end();
});
});

test('pkg-conf(resolve-dir): resolves tests process.cwd() if globs are passed on the command line', function (t) {
execCli(['--verbose', 'dir-a/*.js'], {dirname: 'fixture/pkg-conf/resolve-dir/dir-a-wrapper'}, function (err, stdout, stderr) {
t.ifError(err);
t.match(stderr, /dir-a-wrapper-3/);
t.match(stderr, /dir-a-wrapper-4/);
t.notMatch(stderr, /dir-a-base/);
t.notMatch(stdout, /dir-a-base/);
t.end();
});
});

test('watcher reruns test files when they changed', function (t) {
var killed = false;

Expand Down
1 change: 1 addition & 0 deletions test/fixture/ava-files/cwd/dir-a/test/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
1 change: 1 addition & 0 deletions test/fixture/ava-files/cwd/dir-a/test/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
1 change: 1 addition & 0 deletions test/fixture/ava-files/cwd/dir-b/test/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../../../';

test(t => {
t.pass();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../../../';

test(t => {
t.pass();
});
5 changes: 5 additions & 0 deletions test/fixture/pkg-conf/resolve-dir/dir-a/dir-a-base-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../../';

test(t => {
t.pass();
});
5 changes: 5 additions & 0 deletions test/fixture/pkg-conf/resolve-dir/dir-a/dir-a-base-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../../';

test(t => {
t.pass();
});
7 changes: 7 additions & 0 deletions test/fixture/pkg-conf/resolve-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "application-name",
"version": "0.0.1",
"ava": {
"files": "dir-a/*.js"
}
}
8 changes: 4 additions & 4 deletions test/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ group('chokidar is installed', function (beforeEach, test, group) {
test('initial exclude patterns override whether something is a test file', function (t) {
t.plan(2);

avaFiles = function (files, sources) {
var ret = new AvaFiles(files, sources);
avaFiles = function (options) {
var ret = new AvaFiles(options);
// Note: There is no way for users to actually set exclude patterns yet.
// This test just validates that internal updates to the default excludes pattern will be obeyed.
ret.excludePatterns = ['!*bar*'];
Expand Down Expand Up @@ -632,8 +632,8 @@ group('chokidar is installed', function (beforeEach, test, group) {
test('exclude patterns override directory matches', function (t) {
t.plan(2);

avaFiles = function (files, sources) {
var ret = new AvaFiles(files, sources);
avaFiles = function (options) {
var ret = new AvaFiles(options);
// Note: There is no way for users to actually set exclude patterns yet.
// This test just validates that internal updates to the default excludes pattern will be obeyed.
ret.excludePatterns = ['!**/exclude/**'];
Expand Down