Skip to content
Permalink
Browse files

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.
  • Loading branch information
novemberborn committed Dec 31, 2019
1 parent e330c3e commit 647c24e92d7ffb587016f9fb925cdad18f5c494f
Showing with 52 additions and 5 deletions.
  1. +1 −1 docs/05-command-line.md
  2. +3 −3 eslint-plugin-helper.js
  3. +21 −1 lib/globs.js
  4. +24 −0 test/eslint-plugin-helper.js
  5. +3 −0 test/globs.js
@@ -55,7 +55,7 @@ AVA searches for test files using the following patterns:
* `**/tests/**/*.js`
* `**/__tests__/**/*.js`

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:
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:

* `**/__tests__/**/__helper__/**/*`
* `**/__tests__/**/__helpers__/**/*`
@@ -1,8 +1,7 @@
'use strict';
const path = require('path');
const babelManager = require('./lib/babel-manager');
const normalizeExtensions = require('./lib/extensions');
const {classify, hasExtension, matches, normalizeFileForMatching, normalizeGlobs, normalizePatterns} = require('./lib/globs');
const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizeGlobs, normalizePatterns} = require('./lib/globs');
const loadConfig = require('./lib/load-config');

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

return {isHelper, isTest};
@@ -194,11 +194,31 @@ function normalizeFileForMatching(cwd, file) {

exports.normalizeFileForMatching = normalizeFileForMatching;

function isHelperish(file) { // Assume file has been normalized already.
// File names starting with an underscore are deemed "helpers".
if (path.basename(file).startsWith('_')) {
return true;
}

// This function assumes the file has been normalized. If it couldn't be,
// don't check if it's got a parent directory that starts with an underscore.
// Deem it not a "helper".
if (path.isAbsolute(file)) {
return false;
}

// If the file has a parent directory that starts with only a single
// underscore, it's deemed a "helper".
return path.dirname(file).split('/').some(dir => /^_(?:$|[^_])/.test(dir));
}

exports.isHelperish = isHelperish;

function classify(file, {cwd, extensions, filePatterns, ignoredByWatcherPatterns}) {
file = normalizeFileForMatching(cwd, file);
return {
isIgnoredByWatcher: matchesIgnorePatterns(file, ignoredByWatcherPatterns),
isTest: hasExtension(extensions, file) && !path.basename(file).startsWith('_') && filePatterns.length > 0 && matches(file, filePatterns)
isTest: hasExtension(extensions, file) && !isHelperish(file) && filePatterns.length > 0 && matches(file, filePatterns)
};
}

@@ -28,6 +28,10 @@ test('classifies files according to the configuration', t => {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'tests/_helper/file.foo')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(projectDir, 'helpers/helper.foo')), {
isHelper: false,
isTest: false
@@ -57,6 +61,10 @@ test('classifies files according to configuration override', t => {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'tests/_helper/file.foo')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyFile(path.join(overrideDir, 'helpers/helper.foo')), {
isHelper: true,
isTest: false
@@ -82,6 +90,10 @@ test('classifies imports with extension according to the configuration', t => {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper/file.foo')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper.foo')), {
isHelper: false,
isTest: false
@@ -107,6 +119,10 @@ test('classifies imports with extension according to the override', t => {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper/file.foo')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper.foo')), {
isHelper: true,
isTest: false
@@ -128,6 +144,10 @@ test('classifies imports without extension according to the configuration', t =>
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'tests/_helper/file')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(projectDir, 'helpers/helper')), {
isHelper: false,
isTest: false
@@ -153,6 +173,10 @@ test('classifies imports without extension according to the override', t => {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'tests/_helper/file')), {
isHelper: true,
isTest: false
});
t.deepEqual(helper.classifyImport(path.join(overrideDir, 'helpers/helper')), {
isHelper: true,
isTest: false
@@ -61,6 +61,7 @@ test('isTest with defaults', t => {
notTest('node_modules/foo.js');
notTest('fixtures/foo.js');
notTest('helpers/foo.js');
notTest('_foo/bar.js');
notTest('__tests__/__helper__/foo.js');
notTest('__tests__/__helper__/test.js');
notTest('__tests__/__helpers__/foo.js');
@@ -71,6 +72,8 @@ test('isTest with defaults', t => {
notTest('__tests__/__fixtures__/test.js');
isTest('__tests__/helper/foo.js');
isTest('__tests__/fixtures/foo.js');
isTest('test/foo.js');
notTest('test/_foo/bar.js');
notTest('test/helper/foo.js');
notTest('test/helper/test.js');
notTest('test/helpers/foo.js');

0 comments on commit 647c24e

Please sign in to comment.
You can’t perform that action at this time.