From 318747da56911c6d47911bc26ee7baa3930d0eb5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 14 Sep 2024 07:50:34 +0900 Subject: [PATCH 1/4] fix(workspace): fix glob negation pattern --- .../vitest/src/node/workspace/resolveWorkspace.ts | 2 +- .../workspace/negated/packages/a/package.json | 3 +++ .../workspace/negated/packages/a/test.test.ts | 3 +++ .../workspace/negated/packages/b/package.json | 3 +++ .../workspace/negated/packages/b/test.test.ts | 3 +++ .../workspace/negated/packages/c/package.json | 3 +++ .../workspace/negated/packages/c/test.test.ts | 3 +++ .../fixtures/workspace/negated/vitest.workspace.ts | 4 ++++ test/config/test/workspace.test.ts | 11 +++++++++++ 9 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/config/fixtures/workspace/negated/packages/a/package.json create mode 100644 test/config/fixtures/workspace/negated/packages/a/test.test.ts create mode 100644 test/config/fixtures/workspace/negated/packages/b/package.json create mode 100644 test/config/fixtures/workspace/negated/packages/b/test.test.ts create mode 100644 test/config/fixtures/workspace/negated/packages/c/package.json create mode 100644 test/config/fixtures/workspace/negated/packages/c/test.test.ts create mode 100644 test/config/fixtures/workspace/negated/vitest.workspace.ts diff --git a/packages/vitest/src/node/workspace/resolveWorkspace.ts b/packages/vitest/src/node/workspace/resolveWorkspace.ts index 75c0785b3995..8ae7441655ba 100644 --- a/packages/vitest/src/node/workspace/resolveWorkspace.ts +++ b/packages/vitest/src/node/workspace/resolveWorkspace.ts @@ -158,7 +158,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 (!stringOption.includes('*') && !stringOption.startsWith('!')) { 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', From e6189a1ad8a580d7ddded92256b967448bf5ed71 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 17 Sep 2024 09:26:12 +0900 Subject: [PATCH 2/4] fix: use fast-glob's isDynamicPattern --- .../src/node/workspace/fast-glob-pattern.ts | 67 +++++++++++++++++++ .../src/node/workspace/resolveWorkspace.ts | 3 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/vitest/src/node/workspace/fast-glob-pattern.ts 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..2294c9af5cda --- /dev/null +++ b/packages/vitest/src/node/workspace/fast-glob-pattern.ts @@ -0,0 +1,67 @@ +import type { GlobOptions } from 'tinyglobby' + +type Pattern = string +type PatternTypeOptions = GlobOptions & { extglob?: boolean; braceExpansion?: boolean } + +// 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 + +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 8ae7441655ba..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('*') && !stringOption.startsWith('!')) { + if (!isDynamicPattern(stringOption)) { const file = resolve(vitest.config.root, stringOption) if (!existsSync(file)) { From 4adca94a911804ca5c41331e9b3482bd70bc06a4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 17 Sep 2024 09:30:49 +0900 Subject: [PATCH 3/4] chore: cleanup --- packages/vitest/src/node/workspace/fast-glob-pattern.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/vitest/src/node/workspace/fast-glob-pattern.ts b/packages/vitest/src/node/workspace/fast-glob-pattern.ts index 2294c9af5cda..01f1100fe67b 100644 --- a/packages/vitest/src/node/workspace/fast-glob-pattern.ts +++ b/packages/vitest/src/node/workspace/fast-glob-pattern.ts @@ -1,7 +1,5 @@ -import type { GlobOptions } from 'tinyglobby' - type Pattern = string -type PatternTypeOptions = GlobOptions & { extglob?: boolean; braceExpansion?: boolean } +type PatternTypeOptions = any // copy of fast-glob's isDynamicPattern until it's implemented on tinyglobby // https://github.com/SuperchupuDev/tinyglobby/issues/28 From 13ea69e293eeffe568e7e19d9f549176f60f869e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 17 Sep 2024 09:40:27 +0900 Subject: [PATCH 4/4] chore: license --- .../vitest/src/node/workspace/fast-glob-pattern.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/vitest/src/node/workspace/fast-glob-pattern.ts b/packages/vitest/src/node/workspace/fast-glob-pattern.ts index 01f1100fe67b..bf84f736a34c 100644 --- a/packages/vitest/src/node/workspace/fast-glob-pattern.ts +++ b/packages/vitest/src/node/workspace/fast-glob-pattern.ts @@ -5,6 +5,18 @@ type PatternTypeOptions = any // 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 = /[*?]|^!/