Skip to content

Commit

Permalink
feat(cli): support excluding projects with --project=!pattern (#6924)
Browse files Browse the repository at this point in the history
  • Loading branch information
haines authored Nov 19, 2024
1 parent 03ca286 commit ebfe942
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ Path to a custom tsconfig file
- **CLI:** `--project <name>`
- **Config:** [project](/config/#project)

The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`
The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`, and exclude projects with `--project=!pattern`.

### slowTestThreshold

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
project: {
description:
'The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`',
'The name of the project to run if you are using Vitest workspace feature. This can be repeated for multiple projects: `--project=1 --project=2`. You can also filter projects using wildcards like `--project=packages*`, and exclude projects with `--project=!pattern`.',
argument: '<name>',
array: true,
},
Expand Down
17 changes: 13 additions & 4 deletions packages/vitest/src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ export function escapeRegExp(s: string) {
}

export function wildcardPatternToRegExp(pattern: string): RegExp {
return new RegExp(
`^${pattern.split('*').map(escapeRegExp).join('.*')}$`,
'i',
)
const negated = pattern.startsWith('!')

if (negated) {
pattern = pattern.slice(1)
}

let regexp = `${pattern.split('*').map(escapeRegExp).join('.*')}$`

if (negated) {
regexp = `(?!${regexp})`
}

return new RegExp(`^${regexp}`, 'i')
}
20 changes: 17 additions & 3 deletions test/config/test/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

const allProjects = ['project_1', 'project_2', 'space_1']

test.each([
{ pattern: 'project_1', expected: ['project_1'] },
{ pattern: '*', expected: ['project_1', 'project_2', 'space_1'] },
{ pattern: '*', expected: allProjects },
{ pattern: '*j*', expected: ['project_1', 'project_2'] },
{ pattern: 'project*', expected: ['project_1', 'project_2'] },
{ pattern: 'space*', expected: ['space_1'] },
{ pattern: '!project_1', expected: ['project_2', 'space_1'] },
{ pattern: '!project*', expected: ['space_1'] },
{ pattern: '!project', expected: allProjects },
])('should match projects correctly: $pattern', async ({ pattern, expected }) => {
const { stdout, stderr } = await runVitest({
const { ctx, stderr, stdout } = await runVitest({
root: 'fixtures/project',
reporters: ['basic'],
project: pattern,
Expand All @@ -17,5 +22,14 @@ test.each([
expect(stderr).toBeFalsy()
expect(stdout).toBeTruthy()

expected.forEach(name => expect(stdout).toContain(name))
for (const project of allProjects) {
if (expected.includes(project)) {
expect(stdout).toContain(project)
}
else {
expect(stdout).not.toContain(project)
}
}

expect(ctx?.projects.map(p => p.name).sort()).toEqual(expected)
})

0 comments on commit ebfe942

Please sign in to comment.