Skip to content

Commit 314ef00

Browse files
novemberbornsindresorhus
authored andcommitted
Clarify directories used by API (#1252)
* Rename pkgDir to projectDir: this better communicates it's the directory of the user's project. * Remove unused cwd option from API. * Remove default for resolveTestsFrom. It's confusing to see process.cwd() in the code even though it's never actually used. * Ensure API test properly creates the API instances.
1 parent 73394f1 commit 314ef00

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

api.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ class Api extends EventEmitter {
4949
super();
5050
autoBind(this);
5151

52-
this.options = Object.assign({
53-
cwd: process.cwd(),
54-
resolveTestsFrom: process.cwd(),
55-
match: []
56-
}, options);
57-
52+
this.options = Object.assign({match: []}, options);
5853
this.options.require = resolveModules(this.options.require);
5954
}
6055
_runFile(file, runStatus, execArgv) {

lib/cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports.run = () => {
2424
const conf = pkgConf.sync('ava');
2525

2626
const filepath = pkgConf.filepath(conf);
27-
const pkgDir = filepath === null ? process.cwd() : path.dirname(filepath);
27+
const projectDir = filepath === null ? process.cwd() : path.dirname(filepath);
2828

2929
const cli = meow(`
3030
Usage
@@ -116,8 +116,8 @@ exports.run = () => {
116116
explicitTitles: cli.flags.watch,
117117
match: arrify(cli.flags.match),
118118
babelConfig: babelConfig.validate(conf.babel),
119-
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
120-
pkgDir,
119+
resolveTestsFrom: cli.input.length === 0 ? projectDir : process.cwd(),
120+
projectDir,
121121
timeout: cli.flags.timeout,
122122
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0,
123123
updateSnapshots: cli.flags.updateSnapshots,
@@ -129,9 +129,9 @@ exports.run = () => {
129129
if (cli.flags.tap && !cli.flags.watch) {
130130
reporter = new TapReporter();
131131
} else if (cli.flags.verbose || isCi) {
132-
reporter = new VerboseReporter({color: cli.flags.color, basePath: pkgDir});
132+
reporter = new VerboseReporter({color: cli.flags.color, basePath: projectDir});
133133
} else {
134-
reporter = new MiniReporter({color: cli.flags.color, watching: cli.flags.watch, basePath: pkgDir});
134+
reporter = new MiniReporter({color: cli.flags.color, watching: cli.flags.watch, basePath: projectDir});
135135
}
136136

137137
reporter.api = api;

lib/fork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = (file, opts, execArgv) => {
3939
const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'];
4040

4141
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
42-
cwd: opts.pkgDir,
42+
cwd: opts.projectDir,
4343
silent: true,
4444
env,
4545
execArgv: execArgv || process.execArgv

test/api.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ const fs = require('fs');
44
const figures = require('figures');
55
const rimraf = require('rimraf');
66
const test = require('tap').test;
7-
const pkgConf = require('pkg-conf');
87
const Api = require('../api');
98
const testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer');
109

11-
const conf = pkgConf.sync('ava');
12-
const pkgDir = path.dirname(pkgConf.filepath(conf));
10+
const ROOT_DIR = path.join(__dirname, '..');
1311

14-
generateTests('Without Pool: ', options => {
12+
function apiCreator(options) {
1513
options = options || {};
1614
options.powerAssert = true;
17-
options.pkgDir = options.pkgDir || pkgDir;
15+
options.projectDir = options.projectDir || ROOT_DIR;
16+
options.resolveTestsFrom = options.resolveTestsFrom || options.projectDir;
1817
return new Api(options);
19-
});
18+
}
19+
20+
generateTests('Without Pool: ', options => apiCreator(options || {}));
2021

2122
// The following two tests are only run against "Without Pool" behavior as they test the exclusive test features. These features are currently not expected to work correctly in the limited process pool. When the limited process pool behavior is finalized this test file will be updated. See: https://github.com/avajs/ava/pull/791#issuecomment-216293302
2223
test('Without Pool: test file with exclusive tests causes non-exclusive tests in other files to be ignored', t => {
@@ -28,7 +29,7 @@ test('Without Pool: test file with exclusive tests causes non-exclusive tests in
2829
path.join(__dirname, 'fixture/one-pass-one-fail.js')
2930
];
3031

31-
const api = new Api();
32+
const api = apiCreator({});
3233

3334
return api.run(files)
3435
.then(result => {
@@ -42,7 +43,7 @@ test('Without Pool: test file with exclusive tests causes non-exclusive tests in
4243
test('Without Pool: test files can be forced to run in exclusive mode', t => {
4344
t.plan(4);
4445

45-
const api = new Api();
46+
const api = apiCreator();
4647
return api.run(
4748
[path.join(__dirname, 'fixture/es2015.js')],
4849
{runOnlyExclusive: true}
@@ -57,9 +58,7 @@ test('Without Pool: test files can be forced to run in exclusive mode', t => {
5758
generateTests('With Pool: ', options => {
5859
options = options || {};
5960
options.concurrency = 2;
60-
options.powerAssert = true;
61-
options.pkgDir = options.pkgDir || pkgDir;
62-
return new Api(options);
61+
return apiCreator(options);
6362
});
6463

6564
function generateTests(prefix, apiCreator) {
@@ -323,11 +322,11 @@ function generateTests(prefix, apiCreator) {
323322
});
324323
});
325324

326-
test(`${prefix} change process.cwd() to a test's directory with pkgDir`, t => {
325+
test(`${prefix} control worker's process.cwd() with projectDir option`, t => {
327326
t.plan(1);
328327

329328
const fullPath = path.join(__dirname, 'fixture/process-cwd-pkgdir.js');
330-
const api = apiCreator({pkgDir: path.dirname(fullPath)});
329+
const api = apiCreator({projectDir: path.dirname(fullPath)});
331330

332331
return api.run([fullPath])
333332
.then(result => {
@@ -761,7 +760,7 @@ function generateTests(prefix, apiCreator) {
761760
test(`${prefix} emits dependencies for test files`, t => {
762761
t.plan(8);
763762

764-
const api = new Api({
763+
const api = apiCreator({
765764
require: [path.resolve('test/fixture/with-dependencies/require-custom.js')]
766765
});
767766

@@ -945,7 +944,7 @@ function generateTests(prefix, apiCreator) {
945944
test(`${prefix} babelConfig:{extends:path, plugins:[...]} merges plugins with .babelrc`, t => {
946945
t.plan(2);
947946

948-
const api = new Api({
947+
const api = apiCreator({
949948
babelConfig: {
950949
plugins: [testCapitalizerPlugin],
951950
extends: path.join(__dirname, 'fixture/babelrc/.alt-babelrc')
@@ -1040,7 +1039,7 @@ function generatePassDebugTests(execArgv, expectedInspectIndex) {
10401039
test(`pass ${execArgv.join(' ')} to fork`, t => {
10411040
t.plan(expectedInspectIndex === -1 ? 3 : 2);
10421041

1043-
const api = new Api({testOnlyExecArgv: execArgv});
1042+
const api = apiCreator({testOnlyExecArgv: execArgv});
10441043
return api._computeForkExecArgs(['foo.js'])
10451044
.then(result => {
10461045
t.true(result.length === 1);
@@ -1058,7 +1057,7 @@ function generatePassDebugIntegrationTests(execArgv) {
10581057
test(`pass ${execArgv.join(' ')} to fork`, t => {
10591058
t.plan(1);
10601059

1061-
const api = new Api({testOnlyExecArgv: execArgv});
1060+
const api = apiCreator({testOnlyExecArgv: execArgv});
10621061
return api.run([path.join(__dirname, 'fixture/debug-arg.js')])
10631062
.then(result => {
10641063
t.is(result.passCount, 1);

0 commit comments

Comments
 (0)