From 7a9135e801c79ef347182f1dcd4ddd178445cb4e Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 21 Nov 2024 10:49:18 -0500 Subject: [PATCH] support exclude globs --- README.md | 6 ++++++ lib/run.js | 14 +++++++++++++- test/cli.test.js | 19 ++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e700e4d..d0939aa 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ borp --reporter foo:stderr # with a local custom reporter borp --reporter ./lib/some-reporter.mjs + +# matching all test.js files except ones in nested node_modules directories +borp 'test/**/*.test.js' '!test/**/node_modules/**/*.test.js' ``` Borp will automatically run all tests files matching `*.test.{js|ts}`. @@ -137,6 +140,9 @@ full path to some yaml file. The current supported options are: + `files` (string[]): An array of test files to include. Globs are supported. + Note: any glob that starts with a `!` (bang character) will be treated as + an ignore glob, e.g. `'!test/**/node_modules/**/*'` will ignore all files + in nested `node_modules` directories that would otherwise be matched. + `reporters` (string[]): An array of reporters to use. May be relative path strings, or module name strings. diff --git a/lib/run.js b/lib/run.js index 3da4ace..fab2fa5 100644 --- a/lib/run.js +++ b/lib/run.js @@ -196,15 +196,27 @@ export default async function runWithTypeScript (config) { if (prefix) { files = files.map((file) => join(prefix, file.replace(/ts$/, 'js'))) } + const expandedFiles = [] + const globs = [] for (let i = 0; i < files.length; i += 1) { if (files[i].includes('*') === false) { expandedFiles.push(files[i]) continue } - const parsed = await glob(files[i].replace(/^['"]/, '').replace(/['"]$/, ''), { ignore, cwd, windowsPathsNoEscape: true }) + const pattern = files[i].replace(/^['"]/, '').replace(/['"]$/, '') + if (pattern[0] === '!') { + ignore.push(pattern.slice(1)) + continue + } + globs.push(pattern) + } + + if (globs.length > 0) { + const parsed = await glob(globs, { ignore, cwd, windowsPathsNoEscape: true }) Array.prototype.push.apply(expandedFiles, parsed) } + files = expandedFiles } else if (config.pattern) { let pattern = config.pattern diff --git a/test/cli.test.js b/test/cli.test.js index b2b8f41..91e1ab9 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -112,7 +112,24 @@ test('interprets globs for files', async () => { cwd }) - strictEqual(stdout.indexOf('tests 2') >= 0, true) + strictEqual(stdout.indexOf('✔ add') >= 0, true) + strictEqual(stdout.indexOf('✔ add2') >= 0, true) + strictEqual(stdout.indexOf('✔ a thing'), -1) +}) + +test('interprets globs for files with an ignore rule', async () => { + const cwd = join(import.meta.url, '..', 'fixtures', 'files-glob') + const { stdout } = await execa('node', [ + borp, + '\'**/*.test.js\'', + '\'!test1/**/node_modules/**/*\'' + ], { + cwd + }) + + strictEqual(stdout.indexOf('✔ add') >= 0, true) + strictEqual(stdout.indexOf('✔ add2') >= 0, true) + strictEqual(stdout.indexOf('✔ a thing'), -1) }) test('Post compile script should be executed when --post-compile is sent with esm', async () => {