Skip to content

Commit

Permalink
fix(workspace): fix glob pattern detection (#6502)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 25, 2024
1 parent f7da619 commit 7727ca8
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 1 deletion.
77 changes: 77 additions & 0 deletions packages/vitest/src/node/workspace/fast-glob-pattern.ts
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion packages/vitest/src/node/workspace/resolveWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -158,7 +159,7 @@ async function resolveWorkspaceProjectConfigs(
const stringOption = definition.replace('<rootDir>', 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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "a"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test('test - a')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "b"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test('test - b')
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "c"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test('test - c')
4 changes: 4 additions & 0 deletions test/config/fixtures/workspace/negated/vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default [
'packages/*',
'!packages/b'
]
11 changes: 11 additions & 0 deletions test/config/test/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 7727ca8

Please sign in to comment.