Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): show test "path" when filtering #6649

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions packages/vitest/src/node/stdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { Writable } from 'node:stream'
import c from 'tinyrainbow'
import prompt from 'prompts'
import { relative, resolve } from 'pathe'
import { getTests, isWindows, stdout } from '../utils'
import type { File, Task } from '@vitest/runner'
import { isAtomTest } from '@vitest/runner/utils'
import { isWindows, stdout } from '../utils'
import { toArray } from '../utils/base'
import type { Vitest } from './core'
import type { FilterType } from './watch-filter'
import { WatchFilter } from './watch-filter'

const keys = [
Expand Down Expand Up @@ -36,6 +39,38 @@ ${keys
)
}

function* traverseFilteredTestNames(parentName: string, filter: RegExp, t: Task): Generator<FilterType> {
if (isAtomTest(t)) {
if (t.name.match(filter)) {
const displayName = `${parentName} > ${t.name}`
yield { name: t.name, toString: () => displayName }
}
}
else {
parentName = parentName.length ? `${parentName} > ${t.name}` : t.name
for (const task of t.tasks) {
yield * traverseFilteredTestNames(parentName, filter, task)
}
}
}

function* getFilteredTestNames(pattern: string, suite: File[]): Generator<FilterType> {
try {
const reg = new RegExp(pattern)
// TODO: we cannot run tests per workspace yet: filtering files
const files = new Set<string>()
for (const file of suite) {
if (!files.has(file.name)) {
files.add(file.name)
yield * traverseFilteredTestNames('', reg, file)
}
}
}
catch {
// `new RegExp` may throw error when input is invalid regexp
}
}

export function registerConsoleShortcuts(
ctx: Vitest,
stdin: NodeJS.ReadStream = process.stdin,
Expand Down Expand Up @@ -126,24 +161,13 @@ export function registerConsoleShortcuts(

async function inputNamePattern() {
off()
const watchFilter = new WatchFilter(
const watchFilter = new WatchFilter<'object'>(
'Input test name pattern (RegExp)',
stdin,
stdout,
)
const filter = await watchFilter.filter((str: string) => {
const files = ctx.state.getFiles()
const tests = getTests(files)
try {
const reg = new RegExp(str)
return tests
.map(test => test.name)
.filter(testName => testName.match(reg))
}
catch {
// `new RegExp` may throw error when input is invalid regexp
return []
}
return [...getFilteredTestNames(str, ctx.state.getFiles())]
})

on()
Expand Down
24 changes: 17 additions & 7 deletions packages/vitest/src/node/watch-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ const MAX_RESULT_COUNT = 10
const SELECTION_MAX_INDEX = 7
const ESC = '\u001B['

type FilterFunc = (keyword: string) => Promise<string[]> | string[]
export interface FilterType {
Copy link
Member Author

@userquin userquin Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this to FilterObject or FilterItem and replace name with key.

This way when we add the x command to run a test/suite we we just use key with the corresponding id.

name: string
toString: () => string
}
type FilterItemType<T extends 'string' | 'object' = 'string'> = T extends 'string' ? string : FilterType
type FilterFuncType<T extends 'string' | 'object' = 'string'> = (keyword: string) => Promise<FilterItemType<T>[]> | FilterItemType<T>[]

export class WatchFilter {
export class WatchFilter<T extends 'string' | 'object' = 'string'> {
private filterRL: readline.Interface
private currentKeyword: string | undefined = undefined
private message: string
private results: string[] = []
private results: FilterItemType<T>[] = []
private selectionIndex = -1
private onKeyPress?: (str: string, key: any) => void
private stdin: NodeJS.ReadStream
Expand All @@ -40,7 +45,7 @@ export class WatchFilter {
}
}

public async filter(filterFunc: FilterFunc): Promise<string | undefined> {
public async filter(filterFunc: FilterFuncType<T>): Promise<string | undefined> {
this.write(this.promptLine())

const resultPromise = createDefer<string | undefined>()
Expand All @@ -58,7 +63,7 @@ export class WatchFilter {
}

private filterHandler(
filterFunc: FilterFunc,
filterFunc: FilterFuncType<T>,
onSubmit: (result?: string) => void,
) {
return async (str: string | undefined, key: any) => {
Expand All @@ -78,12 +83,17 @@ export class WatchFilter {
onSubmit(undefined)
break
case key?.name === 'enter':
case key?.name === 'return':
case key?.name === 'return': {
const selection = this.results[this.selectionIndex]
const result = typeof selection === 'string'
? selection
: selection?.name
onSubmit(
this.results[this.selectionIndex] || this.currentKeyword || '',
result || this.currentKeyword || '',
)
this.currentKeyword = undefined
break
}
case key?.name === 'up':
if (this.selectionIndex && this.selectionIndex > 0) {
this.selectionIndex--
Expand Down