diff --git a/packages/vitest/src/node/workspace/fast-glob-pattern.ts b/packages/vitest/src/node/workspace/fast-glob-pattern.ts new file mode 100644 index 000000000000..bf84f736a34c --- /dev/null +++ b/packages/vitest/src/node/workspace/fast-glob-pattern.ts @@ -0,0 +1,77 @@ +type Pattern = string +type PatternTypeOptions = any + +// copy of fast-glob's isDynamicPattern until it's implemented on tinyglobby +// https://github.com/SuperchupuDev/tinyglobby/issues/28 +// https://github.com/mrmlnc/fast-glob/blob/da648078ae87bce81fcd42e64d5b2b1360c47b09/src/utils/pattern.ts#L35 + +/* +MIT License + +Copyright (c) Denis Malinochkin + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +const ESCAPE_SYMBOL = '\\' + +const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/ +const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*\]/ +const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/ +const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/ +const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./ + +export function isDynamicPattern(pattern: Pattern, options: PatternTypeOptions = {}): boolean { + /** + * A special case with an empty string is necessary for matching patterns that start with a forward slash. + * An empty string cannot be a dynamic pattern. + * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'. + */ + if (pattern === '') { + return false + } + + /** + * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check + * filepath directly (without read directory). + */ + if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) { + return true + } + + if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) { + return true + } + + if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) { + return true + } + + if (options.braceExpansion !== false && hasBraceExpansion(pattern)) { + return true + } + + return false +} + +function hasBraceExpansion(pattern: string): boolean { + const openingBraceIndex = pattern.indexOf('{') + + if (openingBraceIndex === -1) { + return false + } + + const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1) + + if (closingBraceIndex === -1) { + return false + } + + const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex) + + return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent) +} diff --git a/packages/vitest/src/node/workspace/resolveWorkspace.ts b/packages/vitest/src/node/workspace/resolveWorkspace.ts index 75c0785b3995..c4ebc1019b9d 100644 --- a/packages/vitest/src/node/workspace/resolveWorkspace.ts +++ b/packages/vitest/src/node/workspace/resolveWorkspace.ts @@ -8,6 +8,7 @@ import type { UserConfig, UserWorkspaceConfig, WorkspaceProjectConfiguration } f import type { WorkspaceProject } from '../workspace' import { initializeProject } from '../workspace' import { configFiles as defaultConfigFiles } from '../../constants' +import { isDynamicPattern } from './fast-glob-pattern' export async function resolveWorkspace( vitest: Vitest, @@ -158,7 +159,7 @@ async function resolveWorkspaceProjectConfigs( const stringOption = definition.replace('', vitest.config.root) // if the string doesn't contain a glob, we can resolve it directly // ['./vitest.config.js'] - if (!stringOption.includes('*')) { + if (!isDynamicPattern(stringOption)) { const file = resolve(vitest.config.root, stringOption) if (!existsSync(file)) { diff --git a/test/config/fixtures/workspace/negated/packages/a/package.json b/test/config/fixtures/workspace/negated/packages/a/package.json new file mode 100644 index 000000000000..44d21f1fa79e --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/a/package.json @@ -0,0 +1,3 @@ +{ + "name": "a" +} diff --git a/test/config/fixtures/workspace/negated/packages/a/test.test.ts b/test/config/fixtures/workspace/negated/packages/a/test.test.ts new file mode 100644 index 000000000000..8e957631471f --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/a/test.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest'; + +test('test - a') diff --git a/test/config/fixtures/workspace/negated/packages/b/package.json b/test/config/fixtures/workspace/negated/packages/b/package.json new file mode 100644 index 000000000000..bbe7ab805efe --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/b/package.json @@ -0,0 +1,3 @@ +{ + "name": "b" +} diff --git a/test/config/fixtures/workspace/negated/packages/b/test.test.ts b/test/config/fixtures/workspace/negated/packages/b/test.test.ts new file mode 100644 index 000000000000..8984e5e843b5 --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/b/test.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest'; + +test('test - b') diff --git a/test/config/fixtures/workspace/negated/packages/c/package.json b/test/config/fixtures/workspace/negated/packages/c/package.json new file mode 100644 index 000000000000..6cd5677e7633 --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/c/package.json @@ -0,0 +1,3 @@ +{ + "name": "c" +} diff --git a/test/config/fixtures/workspace/negated/packages/c/test.test.ts b/test/config/fixtures/workspace/negated/packages/c/test.test.ts new file mode 100644 index 000000000000..37d9d592a9b9 --- /dev/null +++ b/test/config/fixtures/workspace/negated/packages/c/test.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest'; + +test('test - c') diff --git a/test/config/fixtures/workspace/negated/vitest.workspace.ts b/test/config/fixtures/workspace/negated/vitest.workspace.ts new file mode 100644 index 000000000000..131aeb64b2f0 --- /dev/null +++ b/test/config/fixtures/workspace/negated/vitest.workspace.ts @@ -0,0 +1,4 @@ +export default [ + 'packages/*', + '!packages/b' +] diff --git a/test/config/test/workspace.test.ts b/test/config/test/workspace.test.ts index 497454e3040f..44202cb41b94 100644 --- a/test/config/test/workspace.test.ts +++ b/test/config/test/workspace.test.ts @@ -35,6 +35,17 @@ it('correctly resolves workspace projects with a several folder globs', async () expect(stdout).toContain('test - b') }) +it('supports glob negation pattern', async () => { + const { stderr, stdout } = await runVitest({ + root: 'fixtures/workspace/negated', + workspace: './fixtures/workspace/negated/vitest.workspace.ts', + }) + expect(stderr).toBe('') + expect(stdout).toContain('test - a') + expect(stdout).toContain('test - c') + expect(stdout).not.toContain('test - b') +}) + it('fails if project names are identical with a nice error message', async () => { const { stderr } = await runVitest({ root: 'fixtures/workspace/invalid-duplicate-configs',