Browse Source

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.
use-hullabaloo
Mark Wubben 8 years ago
committed by Sindre Sorhus
parent
commit
314ef001ab
  1. 7
      api.js
  2. 10
      lib/cli.js
  3. 2
      lib/fork.js
  4. 33
      test/api.js

7
api.js

@ -49,12 +49,7 @@ class Api extends EventEmitter {
super();
autoBind(this);
this.options = Object.assign({
cwd: process.cwd(),
resolveTestsFrom: process.cwd(),
match: []
}, options);
this.options = Object.assign({match: []}, options);
this.options.require = resolveModules(this.options.require);
}
_runFile(file, runStatus, execArgv) {

10
lib/cli.js

@ -24,7 +24,7 @@ exports.run = () => {
const conf = pkgConf.sync('ava');
const filepath = pkgConf.filepath(conf);
const pkgDir = filepath === null ? process.cwd() : path.dirname(filepath);
const projectDir = filepath === null ? process.cwd() : path.dirname(filepath);
const cli = meow(`
Usage
@ -116,8 +116,8 @@ exports.run = () => {
explicitTitles: cli.flags.watch,
match: arrify(cli.flags.match),
babelConfig: babelConfig.validate(conf.babel),
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
pkgDir,
resolveTestsFrom: cli.input.length === 0 ? projectDir : process.cwd(),
projectDir,
timeout: cli.flags.timeout,
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0,
updateSnapshots: cli.flags.updateSnapshots,
@ -129,9 +129,9 @@ exports.run = () => {
if (cli.flags.tap && !cli.flags.watch) {
reporter = new TapReporter();
} else if (cli.flags.verbose || isCi) {
reporter = new VerboseReporter({color: cli.flags.color, basePath: pkgDir});
reporter = new VerboseReporter({color: cli.flags.color, basePath: projectDir});
} else {
reporter = new MiniReporter({color: cli.flags.color, watching: cli.flags.watch, basePath: pkgDir});
reporter = new MiniReporter({color: cli.flags.color, watching: cli.flags.watch, basePath: projectDir});
}
reporter.api = api;

2
lib/fork.js

@ -39,7 +39,7 @@ module.exports = (file, opts, execArgv) => {
const args = [JSON.stringify(opts), opts.color ? '--color' : '--no-color'];
const ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), args, {
cwd: opts.pkgDir,
cwd: opts.projectDir,
silent: true,
env,
execArgv: execArgv || process.execArgv

33
test/api.js

@ -4,19 +4,20 @@ const fs = require('fs');
const figures = require('figures');
const rimraf = require('rimraf');
const test = require('tap').test;
const pkgConf = require('pkg-conf');
const Api = require('../api');
const testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer');
const conf = pkgConf.sync('ava');
const pkgDir = path.dirname(pkgConf.filepath(conf));
const ROOT_DIR = path.join(__dirname, '..');
generateTests('Without Pool: ', options => {
function apiCreator(options) {
options = options || {};
options.powerAssert = true;
options.pkgDir = options.pkgDir || pkgDir;
options.projectDir = options.projectDir || ROOT_DIR;
options.resolveTestsFrom = options.resolveTestsFrom || options.projectDir;
return new Api(options);
});
}
generateTests('Without Pool: ', options => apiCreator(options || {}));
// 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
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
path.join(__dirname, 'fixture/one-pass-one-fail.js')
];
const api = new Api();
const api = apiCreator({});
return api.run(files)
.then(result => {
@ -42,7 +43,7 @@ test('Without Pool: test file with exclusive tests causes non-exclusive tests in
test('Without Pool: test files can be forced to run in exclusive mode', t => {
t.plan(4);
const api = new Api();
const api = apiCreator();
return api.run(
[path.join(__dirname, 'fixture/es2015.js')],
{runOnlyExclusive: true}
@ -57,9 +58,7 @@ test('Without Pool: test files can be forced to run in exclusive mode', t => {
generateTests('With Pool: ', options => {
options = options || {};
options.concurrency = 2;
options.powerAssert = true;
options.pkgDir = options.pkgDir || pkgDir;
return new Api(options);
return apiCreator(options);
});
function generateTests(prefix, apiCreator) {
@ -323,11 +322,11 @@ function generateTests(prefix, apiCreator) {
});
});
test(`${prefix} change process.cwd() to a test's directory with pkgDir`, t => {
test(`${prefix} control worker's process.cwd() with projectDir option`, t => {
t.plan(1);
const fullPath = path.join(__dirname, 'fixture/process-cwd-pkgdir.js');
const api = apiCreator({pkgDir: path.dirname(fullPath)});
const api = apiCreator({projectDir: path.dirname(fullPath)});
return api.run([fullPath])
.then(result => {
@ -761,7 +760,7 @@ function generateTests(prefix, apiCreator) {
test(`${prefix} emits dependencies for test files`, t => {
t.plan(8);
const api = new Api({
const api = apiCreator({
require: [path.resolve('test/fixture/with-dependencies/require-custom.js')]
});
@ -945,7 +944,7 @@ function generateTests(prefix, apiCreator) {
test(`${prefix} babelConfig:{extends:path, plugins:[...]} merges plugins with .babelrc`, t => {
t.plan(2);
const api = new Api({
const api = apiCreator({
babelConfig: {
plugins: [testCapitalizerPlugin],
extends: path.join(__dirname, 'fixture/babelrc/.alt-babelrc')
@ -1040,7 +1039,7 @@ function generatePassDebugTests(execArgv, expectedInspectIndex) {
test(`pass ${execArgv.join(' ')} to fork`, t => {
t.plan(expectedInspectIndex === -1 ? 3 : 2);
const api = new Api({testOnlyExecArgv: execArgv});
const api = apiCreator({testOnlyExecArgv: execArgv});
return api._computeForkExecArgs(['foo.js'])
.then(result => {
t.true(result.length === 1);
@ -1058,7 +1057,7 @@ function generatePassDebugIntegrationTests(execArgv) {
test(`pass ${execArgv.join(' ')} to fork`, t => {
t.plan(1);
const api = new Api({testOnlyExecArgv: execArgv});
const api = apiCreator({testOnlyExecArgv: execArgv});
return api.run([path.join(__dirname, 'fixture/debug-arg.js')])
.then(result => {
t.is(result.passCount, 1);

Loading…
Cancel
Save