From 476c65396155a7d16fbdd55c65863ecaabd873de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=BCnbaum?= Date: Mon, 17 Oct 2016 19:44:54 +0200 Subject: [PATCH] Change the current working directory of tests to be the same directory as package.json (#1074) #32 --- cli.js | 1 + lib/fork.js | 2 +- readme.md | 2 +- test/api.js | 23 ++++++++++++++++--- test/fixture/process-cwd-default.js | 9 ++++++++ .../{process-cwd.js => process-cwd-pkgdir.js} | 0 6 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 test/fixture/process-cwd-default.js rename test/fixture/{process-cwd.js => process-cwd-pkgdir.js} (100%) diff --git a/cli.js b/cli.js index 22bcb26e4..38f7f616f 100755 --- a/cli.js +++ b/cli.js @@ -138,6 +138,7 @@ var api = new Api({ match: arrify(cli.flags.match), babelConfig: conf.babel, resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(), + pkgDir: pkgDir, timeout: cli.flags.timeout, concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0 }); diff --git a/lib/fork.js b/lib/fork.js index 7748c69d2..495f8d8c7 100644 --- a/lib/fork.js +++ b/lib/fork.js @@ -41,7 +41,7 @@ module.exports = function (file, opts, execArgv) { }, opts); var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], { - cwd: path.dirname(file), + cwd: opts.pkgDir, silent: true, env: env, execArgv: execArgv || process.execArgv diff --git a/readme.md b/readme.md index 69768c2aa..b1d4f517c 100644 --- a/readme.md +++ b/readme.md @@ -247,7 +247,7 @@ If you're unable to use promises or observables, you may enable "callback mode" You must define all tests synchronously. They can't be defined inside `setTimeout`, `setImmediate`, etc. -Test files are run from their current directory, so [`process.cwd()`](https://nodejs.org/api/process.html#process_process_cwd) is always the same as [`__dirname`](https://nodejs.org/api/globals.html#globals_dirname). You can just use relative paths instead of doing `path.join(__dirname, 'relative/path')`. +AVA tries to run test files with their current working directory set to the directory that contains your `package.json` file. ### Creating tests diff --git a/test/api.js b/test/api.js index 69dd3e8f6..e40e7d586 100644 --- a/test/api.js +++ b/test/api.js @@ -4,9 +4,13 @@ var fs = require('fs'); var figures = require('figures'); var rimraf = require('rimraf'); var test = require('tap').test; +var pkgConf = require('pkg-conf'); var Api = require('../api'); var testCapitalizerPlugin = require('./fixture/babel-plugin-test-capitalizer'); +var conf = pkgConf.sync('ava'); +var pkgDir = path.dirname(pkgConf.filepath(conf)); + test('must be called with new', function (t) { t.throws(function () { var api = Api; @@ -18,6 +22,7 @@ test('must be called with new', function (t) { generateTests('Without Pool: ', function (options) { options = options || {}; options.powerAssert = true; + options.pkgDir = options.pkgDir || pkgDir; return new Api(options); }); @@ -63,6 +68,7 @@ generateTests('With Pool: ', function (options) { options = options || {}; options.concurrency = 2; options.powerAssert = true; + options.pkgDir = options.pkgDir || pkgDir; return new Api(options); }); @@ -307,12 +313,23 @@ function generateTests(prefix, apiCreator) { }); }); - test(prefix + 'change process.cwd() to a test\'s directory', function (t) { + test(prefix + 'run from package.json folder by default', function (t) { t.plan(1); - var api = apiCreator(); - return api.run([path.join(__dirname, 'fixture/process-cwd.js')]) + return api.run([path.join(__dirname, 'fixture/process-cwd-default.js')]) + .then(function (result) { + t.is(result.passCount, 1); + }); + }); + + test(prefix + 'change process.cwd() to a test\'s directory with pkgDir', function (t) { + t.plan(1); + + var fullPath = path.join(__dirname, 'fixture/process-cwd-pkgdir.js'); + var api = apiCreator({pkgDir: path.dirname(fullPath)}); + + return api.run([fullPath]) .then(function (result) { t.is(result.passCount, 1); }); diff --git a/test/fixture/process-cwd-default.js b/test/fixture/process-cwd-default.js new file mode 100644 index 000000000..78c4eb463 --- /dev/null +++ b/test/fixture/process-cwd-default.js @@ -0,0 +1,9 @@ +import path from 'path'; +import pkgConf from 'pkg-conf'; +import test from '../../'; + +test(t => { + const conf = pkgConf.sync('ava'); + const pkgDir = path.dirname(pkgConf.filepath(conf)); + t.is(process.cwd(), pkgDir); +}); diff --git a/test/fixture/process-cwd.js b/test/fixture/process-cwd-pkgdir.js similarity index 100% rename from test/fixture/process-cwd.js rename to test/fixture/process-cwd-pkgdir.js