Skip to content

Commit 647c24e

Browse files
committed
Do not treat files in underscore-prefixed directories as test files
When a file has a parent directory that starts with a single underscore, do not treat it as a test file. Fixes #2155.
1 parent e330c3e commit 647c24e

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

docs/05-command-line.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ AVA searches for test files using the following patterns:
5555
* `**/tests/**/*.js`
5656
* `**/__tests__/**/*.js`
5757

58-
Files inside `node_modules` are *always* ignored. So are files starting with `_`. Additionally, files matching these patterns are ignored by default, unless different patterns are configured:
58+
Files inside `node_modules` are *always* ignored. So are files starting with `_` or inside of directories that start with a single `_`. Additionally, files matching these patterns are ignored by default, unless different patterns are configured:
5959

6060
* `**/__tests__/**/__helper__/**/*`
6161
* `**/__tests__/**/__helpers__/**/*`

eslint-plugin-helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
2-
const path = require('path');
32
const babelManager = require('./lib/babel-manager');
43
const normalizeExtensions = require('./lib/extensions');
5-
const {classify, hasExtension, matches, normalizeFileForMatching, normalizeGlobs, normalizePatterns} = require('./lib/globs');
4+
const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizeGlobs, normalizePatterns} = require('./lib/globs');
65
const loadConfig = require('./lib/load-config');
76

87
const configCache = new Map();
@@ -53,7 +52,8 @@ function load(projectDir, overrides) {
5352
const {isTest} = classify(file, globs);
5453
let isHelper = false;
5554
if (!isTest && hasExtension(globs.extensions, file)) {
56-
isHelper = path.basename(file).startsWith('_') || (helperPatterns.length > 0 && matches(normalizeFileForMatching(projectDir, file), helperPatterns));
55+
file = normalizeFileForMatching(projectDir, file);
56+
isHelper = isHelperish(file) || (helperPatterns.length > 0 && matches(file, helperPatterns));
5757
}
5858

5959
return {isHelper, isTest};

lib/globs.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,31 @@ function normalizeFileForMatching(cwd, file) {
194194

195195
exports.normalizeFileForMatching = normalizeFileForMatching;
196196

197+
function isHelperish(file) { // Assume file has been normalized already.
198+
// File names starting with an underscore are deemed "helpers".
199+
if (path.basename(file).startsWith('_')) {
200+
return true;
201+
}
202+
203+
// This function assumes the file has been normalized. If it couldn't be,
204+
// don't check if it's got a parent directory that starts with an underscore.
205+
// Deem it not a "helper".
206+
if (path.isAbsolute(file)) {
207+
return false;
208+
}
209+
210+
// If the file has a parent directory that starts with only a single
211+
// underscore, it's deemed a "helper".
212+
return path.dirname(file).split('/').some(dir => /^_(?:$|[^_])/.test(dir));
213+
}
214+
215+
exports.isHelperish = isHelperish;
216+
197217
function classify(file, {cwd, extensions, filePatterns, ignoredByWatcherPatterns}) {
198218
file = normalizeFileForMatching(cwd, file);
199219
return {
200220
isIgnoredByWatcher: matchesIgnorePatterns(file, ignoredByWatcherPatterns),
201-
isTest: hasExtension(extensions, file) && !path.basename(file).startsWith('_') && filePatterns.length > 0 && matches(file, filePatterns)
221+
isTest: hasExtension(extensions, file) && !isHelperish(file) && filePatterns.length > 0 && matches(file, filePatterns)
202222
};
203223
}
204224

test/eslint-plugin-helper.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ test('classifies files according to the configuration', t => {
2828
isHelper: true,
2929
isTest: false
3030
});
31+
t.deepEqual(helper.classifyFile(path.join(projectDir, 'tests/_helper/file.foo')), {
32+
isHelper: true,
33+
isTest: false
34+
});
3135
t.deepEqual(helper.classifyFile(path.join(projectDir, 'helpers/helper.foo')), {
3236
isHelper: false,
3337
isTest: false
@@ -57,6 +61,10 @@ test('classifies files according to configuration override', t => {
5761
isHelper: true,
5862
isTest: false
5963
});
64+
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'tests/_helper/file.foo')), {
65+
isHelper: true,
66+
isTest: false
67+
});
6068
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'helpers/helper.foo')), {
6169
isHelper: true,
6270
isTest: false
@@ -82,6 +90,10 @@ test('classifies imports with extension according to the configuration', t => {
8290
isHelper: true,
8391
isTest: false
8492
});
93+
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper/file.foo')), {
94+
isHelper: true,
95+
isTest: false
96+
});
8597
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper.foo')), {
8698
isHelper: false,
8799
isTest: false
@@ -107,6 +119,10 @@ test('classifies imports with extension according to the override', t => {
107119
isHelper: true,
108120
isTest: false
109121
});
122+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper/file.foo')), {
123+
isHelper: true,
124+
isTest: false
125+
});
110126
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper.foo')), {
111127
isHelper: true,
112128
isTest: false
@@ -128,6 +144,10 @@ test('classifies imports without extension according to the configuration', t =>
128144
isHelper: true,
129145
isTest: false
130146
});
147+
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper/file')), {
148+
isHelper: true,
149+
isTest: false
150+
});
131151
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper')), {
132152
isHelper: false,
133153
isTest: false
@@ -153,6 +173,10 @@ test('classifies imports without extension according to the override', t => {
153173
isHelper: true,
154174
isTest: false
155175
});
176+
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper/file')), {
177+
isHelper: true,
178+
isTest: false
179+
});
156180
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper')), {
157181
isHelper: true,
158182
isTest: false

test/globs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ test('isTest with defaults', t => {
6161
notTest('node_modules/foo.js');
6262
notTest('fixtures/foo.js');
6363
notTest('helpers/foo.js');
64+
notTest('_foo/bar.js');
6465
notTest('__tests__/__helper__/foo.js');
6566
notTest('__tests__/__helper__/test.js');
6667
notTest('__tests__/__helpers__/foo.js');
@@ -71,6 +72,8 @@ test('isTest with defaults', t => {
7172
notTest('__tests__/__fixtures__/test.js');
7273
isTest('__tests__/helper/foo.js');
7374
isTest('__tests__/fixtures/foo.js');
75+
isTest('test/foo.js');
76+
notTest('test/_foo/bar.js');
7477
notTest('test/helper/foo.js');
7578
notTest('test/helper/test.js');
7679
notTest('test/helpers/foo.js');

0 commit comments

Comments
 (0)