Skip to content

Commit 5823d36

Browse files
committed
fixup! fixup! wip! Refactor glob handling
1 parent 750b015 commit 5823d36

File tree

18 files changed

+61
-57
lines changed

18 files changed

+61
-57
lines changed

docs/05-command-line.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ $ npx ava --help
3030

3131
The above relies on your shell expanding the glob patterns.
3232
Without arguments, AVA uses the following patterns:
33-
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js
33+
**/test.js **/test-*.js **/*.test.js **/test/**/*.js **/__tests__/**/*.js
3434
```
3535

3636
*Note that the CLI will use your local install of AVA when available, even when run globally.*
3737

38-
**FIXME**
38+
AVA searches for test files using the following patterns:
3939

40-
Directories are recursed, with all `*.js` files being treated as test files. Directories named `fixtures`, `helpers` and `node_modules` are *always* ignored. So are files starting with `_` which allows you to place helpers in the same directory as your test files.
40+
* `**/test.js`
41+
* `**/test-*.js`
42+
* `**/*.test.js`
43+
* `**/test/**/*.js`
44+
* `**/__tests__/**/*.js`
45+
46+
Files inside `node_modules` are *always* ignored. So are files starting with `_`. These are treated as helpers.
4147

4248
When using `npm test`, you can pass positional arguments directly `npm test test2.js`, but flags needs to be passed like `npm test -- --verbose`.
4349

docs/06-configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
4848

4949
## Options
5050

51-
- `files`: **FIXME** file & directory paths and glob patterns that select which files AVA will run tests from. Files with an underscore prefix are ignored. All matched files in selected directories are run. By default only selects files with `js` extensions, even if the glob pattern matches other files. Specify `extensions` and `babel.extensions` to allow other file extensions
51+
- `files`: glob patterns that select which files AVA will run tests from. Files with an underscore prefix are ignored. By default only selects files with `js` extensions, even if the glob pattern matches other files. Specify `extensions` and `babel.extensions` to allow other file extensions
5252
- `sources`: files that, when changed, cause tests to be re-run in watch mode. See the [watch mode recipe for details](https://github.com/avajs/ava/blob/master/docs/recipes/watch-mode.md#source-files-and-test-files)
5353
- `match`: not typically useful in the `package.json` configuration, but equivalent to [specifying `--match` on the CLI](./05-command-line.md#running-tests-with-matching-titles)
5454
- `cache`: cache compiled test and helper files under `node_modules/.cache/ava`. If `false`, files are cached in a temporary directory instead
@@ -64,7 +64,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
6464
- `babel.extensions`: extensions of test files that will be precompiled using AVA's Babel presets. Setting this overrides the default `"js"` value, so make sure to include that extension in the list
6565
- `timeout`: Timeouts in AVA behave differently than in other test frameworks. AVA resets a timer after each test, forcing tests to quit if no new test results were received within the specified timeout. This can be used to handle stalled tests. See our [timeout documentation](./07-test-timeouts.md) for more options.
6666

67-
**FIXME** Note that providing files on the CLI overrides the `files` option. If you've configured a glob pattern, for instance `test/**/*.test.js`, you may want to repeat it when using the CLI: `ava 'test/integration/*.test.js'`.
67+
Note that providing files on the CLI overrides the `files` option.
6868

6969
## Using `ava.config.js`
7070

docs/08-common-pitfalls.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ test('one is one', t => {
8383
});
8484
```
8585

86-
### Helpers are not compiled when using a non-default test folder
87-
88-
**FIXME**
89-
90-
This is a [known issue](https://github.com/avajs/ava/issues/1319). You should put your tests in a folder called `test` or `__tests__`.
91-
9286
---
9387

9488
Is your problem not listed here? Submit a pull request or comment on [this issue](https://github.com/avajs/ava/issues/404).

docs/recipes/watch-mode.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,12 @@ AVA uses [`chokidar`] as the file watcher. Note that even if you see warnings ab
6565

6666
## Source files and test files
6767

68-
**FIXME**
69-
7068
In AVA there's a distinction between *source files* and *test files*. As you can imagine the *test files* contain your tests. *Source files* are all other files that are needed for the tests to run, be it your source code or test fixtures.
7169

7270
By default AVA watches for changes to the test files, snapshot files, `package.json`, and any other `.js` files. It'll ignore files in [certain directories](https://github.com/novemberborn/ignore-by-default/blob/master/index.js) as provided by the [`ignore-by-default`] package.
7371

7472
You can configure patterns for the source files in the [`ava` section of your `package.json`, or `ava.config.js` file][config], using the `sources` key.
7573

76-
You can specify patterns to match files in the folders that would otherwise be ignored, e.g. use `node_modules/some-dependency/*.js` to specify all `.js` files in `node_modules/some-dependency` as a source, even though normally all files in `node_modules` are ignored. Note that you need to specify an exact directory; `{bower_components,node_modules}/**/*.js` won't work.
77-
7874
If your tests write to disk they may trigger the watcher to rerun your tests. Configure patterns for the source files to avoid this.
7975

8076
## Dependency tracking

lib/cli.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ exports.run = async () => { // eslint-disable-line complexity
5252
5353
The above relies on your shell expanding the glob patterns.
5454
Without arguments, AVA uses the following patterns:
55-
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js
55+
**/test.js **/test-*.js **/*.test.js **/test/**/*.js **/__tests__/**/*.js
5656
`, {
5757
flags: {
5858
watch: {
@@ -250,17 +250,19 @@ exports.run = async () => { // eslint-disable-line complexity
250250
});
251251
});
252252

253+
const files = cli.input.map(file => path.resolve(process.cwd(), file));
254+
253255
if (conf.watch) {
254256
const watcher = new Watcher({
255257
api,
256258
reporter,
257-
files: cli.input,
259+
files,
258260
globs,
259261
resolveTestsFrom
260262
});
261263
watcher.observeStdin(process.stdin);
262264
} else {
263-
const runStatus = await api.run(cli.input);
265+
const runStatus = await api.run(files);
264266
process.exitCode = runStatus.suggestExitCode({matching: match.length > 0});
265267
reporter.endRun();
266268
}

lib/globs.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const path = require('path');
3-
const arrFlatten = require('arr-flatten');
43
const globby = require('globby');
54
const ignoreByDefault = require('ignore-by-default');
65
const micromatch = require('micromatch');
@@ -133,9 +132,7 @@ function getChokidarPatterns({sourcePatterns, testPatterns}) {
133132
const ignored = defaultIgnorePatterns.map(pattern => `${pattern}/**/*`);
134133

135134
for (const pattern of [...sourcePatterns, ...testPatterns]) {
136-
if (pattern.startsWith('!')) {
137-
ignored.push(pattern.slice(1));
138-
} else {
135+
if (!pattern.startsWith('!')) {
139136
paths.push(pattern);
140137
}
141138
}
@@ -145,21 +142,36 @@ function getChokidarPatterns({sourcePatterns, testPatterns}) {
145142

146143
exports.getChokidarPatterns = getChokidarPatterns;
147144

148-
const matches = (file, patterns) => {
149-
const ignore = [...defaultIgnorePatterns];
150-
patterns = patterns.filter(pattern => {
151-
if (pattern.startsWith('!')) {
152-
ignore.push(pattern.slice(1));
153-
return false;
154-
}
145+
const matchingCache = new WeakMap();
146+
const processMatchingPatterns = input => {
147+
let result = matchingCache.get(input);
148+
if (!result) {
149+
const ignore = [
150+
...defaultIgnorePatterns,
151+
// Unlike globby(), micromatch needs a complete pattern when ignoring directories.
152+
...defaultIgnorePatterns.map(pattern => `${pattern}/**/*`)
153+
];
154+
const patterns = input.filter(pattern => {
155+
if (pattern.startsWith('!')) {
156+
// Unlike globby(), micromatch needs a complete pattern when ignoring directories.
157+
ignore.push(pattern.slice(1), `${pattern.slice(1)}/**/*`);
158+
return false;
159+
}
160+
161+
return true;
162+
});
163+
164+
result = {patterns, ignore};
165+
matchingCache.set(input, result);
166+
}
155167

156-
return true;
157-
});
168+
return result;
169+
};
158170

159-
return micromatch.some(file, patterns, {
160-
// Unlike globby(), micromatch needs a complete pattern when ignoring directories.
161-
ignore: arrFlatten(ignore.map(pattern => [pattern, `${pattern}/**/*`]))
162-
});
171+
const matches = (file, patterns) => {
172+
let ignore;
173+
({patterns, ignore} = processMatchingPatterns(patterns));
174+
return micromatch.some(file, patterns, {ignore});
163175
};
164176

165177
function isSource(file, {testPatterns, sourcePatterns}) {

lib/watcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Watcher {
374374

375375
if (dirtySources.length === 0) {
376376
// Run any new or changed tests
377-
this.run(addedOrChangedTests);
377+
this.run(addedOrChangedTests.map(file => nodePath.resolve(this.globs.cwd, file)));
378378
return;
379379
}
380380

@@ -396,7 +396,7 @@ class Watcher {
396396
}
397397

398398
// Run all affected tests
399-
this.run(union(addedOrChangedTests, uniq(flatten(testsBySource))));
399+
this.run(union(addedOrChangedTests, uniq(flatten(testsBySource))).map(file => nodePath.resolve(this.globs.cwd, file)));
400400
}
401401
}
402402

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ava": {
3+
"files": ["*.js"]
4+
}
5+
}

test/fixture/pkg-conf/resolve-dir/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "application-name",
33
"version": "0.0.1",
44
"ava": {
5-
"files": "dir-a/*.js"
5+
"files": ["dir-a/*.js"]
66
}
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"ava": {
3-
"files": "tests"
3+
"files": ["tests/test.js"]
44
}
55
}

0 commit comments

Comments
 (0)