GeezerVison is showing github.com — feat(experimental): introduce `experimental.vcsProvider` (#9928) · vitest-dev/vitest@5611500
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/config/experimental.md +61Lines changed: 61 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -410,6 +410,67 @@ export default defineConfig({ | 410 | 410 | If you are running tests in Deno, TypeScript files are processed by the runtime without any additional configurations. | 411 | 411 | ::: | 412 | 412 |
| | 413 | +## experimental.vcsProvider <Version type="experimental">4.1.1</Version> {#experimental-vcsprovider} | | 414 | + | | 415 | +- **Type:** `VCSProvider | string` | | 416 | + | | 417 | +```ts | | 418 | +interface VCSProvider { | | 419 | + findChangedFiles(options: VCSProviderOptions): Promise<string[]> | | 420 | +} | | 421 | + | | 422 | +interface VCSProviderOptions { | | 423 | + root: string | | 424 | + changedSince?: string | boolean | | 425 | +} | | 426 | +``` | | 427 | + | | 428 | +- **Default:** `'git'` | | 429 | + | | 430 | +Custom provider for detecting changed files. Used with the [`--changed`](/guide/cli#changed) flag to determine which files have been modified. | | 431 | + | | 432 | +By default, Vitest uses Git to detect changed files. You can provide a custom implementation of the `VCSProvider` interface to use a different version control system: | | 433 | + | | 434 | +```ts [vitest.config.ts] | | 435 | +import { defineConfig } from 'vitest/config' | | 436 | + | | 437 | +export default defineConfig({ | | 438 | + test: { | | 439 | + experimental: { | | 440 | + vcsProvider: { | | 441 | + async findChangedFiles({ root, changedSince }) { | | 442 | + // return paths of changed files | | 443 | + return [] | | 444 | + }, | | 445 | + }, | | 446 | + }, | | 447 | + }, | | 448 | +}) | | 449 | +``` | | 450 | + | | 451 | +You can also pass a string path to a module with a default export that implements the `VCSProvider` interface: | | 452 | + | | 453 | +```js [vitest.config.js] | | 454 | +import { defineConfig } from 'vitest/config' | | 455 | + | | 456 | +export default defineConfig({ | | 457 | + test: { | | 458 | + experimental: { | | 459 | + vcsProvider: './my-vcs-provider.js', | | 460 | + }, | | 461 | + }, | | 462 | +}) | | 463 | +``` | | 464 | + | | 465 | +```js [my-vcs-provider.js] | | 466 | +export default { | | 467 | + async findChangedFiles({ root, changedSince }) { | | 468 | + // return paths of changed files | | 469 | + return [] | | 470 | + }, | | 471 | +} | | 472 | +``` | | 473 | + | 413 | 474 | ## experimental.nodeLoader <Version type="experimental">4.1.0</Version> {#experimental-nodeloader} | 414 | 475 |
| 415 | 476 | - **Type:** `boolean` | |
Collapse file Copy file name to clipboardExpand all lines: docs/guide/cli-generated.md +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -949,3 +949,10 @@ Control whether Vitest uses Vite's module runner to run the code or fallback to | 949 | 949 | - **Config:** [experimental.nodeLoader](/config/experimental#experimental-nodeloader) | 950 | 950 |
| 951 | 951 | Controls whether Vitest will use Node.js Loader API to process in-source or mocked files. This has no effect if `viteModuleRunner` is enabled. Disabling this can increase performance. (default: `true`) | | 952 | + | | 953 | +### experimental.vcsProvider | | 954 | + | | 955 | +- **CLI:** `--experimental.vcsProvider <path>` | | 956 | +- **Config:** [experimental.vcsProvider](/config/experimental#experimental-vcsprovider) | | 957 | + | | 958 | +Custom provider for detecting changed files. (default: `git`) |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/cli/cli-config.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -901,6 +901,11 @@ export const cliOptionsConfig: VitestCLIOptions = { | 901 | 901 | nodeLoader: { | 902 | 902 | description: 'Controls whether Vitest will use Node.js Loader API to process in-source or mocked files. This has no effect if `viteModuleRunner` is enabled. Disabling this can increase performance. (default: `true`)', | 903 | 903 | }, | | 904 | + vcsProvider: { | | 905 | + argument: '<path>', | | 906 | + description: 'Custom provider for detecting changed files. (default: `git`)', | | 907 | + subcommands: null, | | 908 | + }, | 904 | 909 | }, | 905 | 910 | }, | 906 | 911 | // disable CLI options | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/config/resolveConfig.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -950,6 +950,10 @@ export function resolveConfig( | 950 | 950 | resolved.experimental.importDurations.thresholds.warn ??= 100 | 951 | 951 | resolved.experimental.importDurations.thresholds.danger ??= 500 | 952 | 952 |
| | 953 | + if (typeof resolved.experimental.vcsProvider === 'string' && resolved.experimental.vcsProvider !== 'git') { | | 954 | + resolved.experimental.vcsProvider = resolvePath(resolved.experimental.vcsProvider, resolved.root) | | 955 | + } | | 956 | + | 953 | 957 | return resolved | 954 | 958 | } | 955 | 959 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/core.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ import type { ResolvedConfig, TestProjectConfiguration, UserConfig, VitestRunMod | 15 | 15 | import type { CoverageProvider, ResolvedCoverageOptions } from './types/coverage' | 16 | 16 | import type { Reporter } from './types/reporter' | 17 | 17 | import type { TestRunResult } from './types/tests' | | 18 | +import type { VCSProvider } from './vcs/vcs' | 18 | 19 | import os, { tmpdir } from 'node:os' | 19 | 20 | import { getTasks, hasFailed, limitConcurrency } from '@vitest/runner/utils' | 20 | 21 | import { SnapshotManager } from '@vitest/snapshot/manager' | @@ -51,6 +52,7 @@ import { VitestSpecifications } from './specifications' | 51 | 52 | import { StateManager } from './state' | 52 | 53 | import { populateProjectsTags } from './tags' | 53 | 54 | import { TestRun } from './test-run' | | 55 | +import { loadVCSProvider } from './vcs/vcs' | 54 | 56 | import { VitestWatcher } from './watcher' | 55 | 57 |
| 56 | 58 | const WATCHER_DEBOUNCE = 100 | @@ -101,6 +103,13 @@ export class Vitest { | 101 | 103 | * Vitest behaviour. | 102 | 104 | */ | 103 | 105 | public readonly watcher: VitestWatcher | | 106 | + /** | | 107 | + * The version control system provider used to detect changed files. | | 108 | + * This is used with the `--changed` flag to determine which test files to run. | | 109 | + * By default, Vitest uses Git. You can provide a custom implementation via | | 110 | + * `experimental.vcsProvider` in your config. | | 111 | + */ | | 112 | + public vcs!: VCSProvider | 104 | 113 |
| 105 | 114 | /** @internal */ configOverride: Partial<ResolvedConfig> = {} | 106 | 115 | /** @internal */ filenamePattern?: string[] | @@ -263,6 +272,7 @@ export class Vitest { | 263 | 272 | configurable: true, | 264 | 273 | }) | 265 | 274 | } | | 275 | + this.vcs = await loadVCSProvider(this.runner, resolved.experimental.vcsProvider) | 266 | 276 |
| 267 | 277 | if (this.config.watch) { | 268 | 278 | // hijack server restart | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/coverage.ts +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -331,11 +331,17 @@ export class BaseCoverageProvider { | 331 | 331 |
| 332 | 332 | async onTestRunStart(): Promise<void> { | 333 | 333 | if (this.options.changed) { | 334 | | - const { VitestGit } = await import('./git') | 335 | | - const vitestGit = new VitestGit(this.ctx.config.root) | 336 | | - const changedFiles = await vitestGit.findChangedFiles({ changedSince: this.options.changed }) | | 334 | + try { | | 335 | + const changedFiles = await this.ctx.vcs.findChangedFiles({ | | 336 | + root: this.ctx.config.root, | | 337 | + changedSince: this.options.changed, | | 338 | + }) | 337 | 339 |
| 338 | | - this.changedFiles = changedFiles ?? undefined | | 340 | + this.changedFiles = changedFiles | | 341 | + } | | 342 | + catch { | | 343 | + this.changedFiles = undefined | | 344 | + } | 339 | 345 | } | 340 | 346 | else if (this.ctx.config.changed) { | 341 | 347 | this.changedFiles = this.ctx.config.related | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/specifications.ts +3-8Lines changed: 3 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,7 @@ import { join, relative, resolve } from 'pathe' | 6 | 6 | import pm from 'picomatch' | 7 | 7 | import { isWindows } from '../utils/env' | 8 | 8 | import { groupFilters, parseFilter } from './cli/filter' | 9 | | -import { GitNotFoundError, IncludeTaskLocationDisabledError, LocationFilterFileNotFoundError } from './errors' | | 9 | +import { IncludeTaskLocationDisabledError, LocationFilterFileNotFoundError } from './errors' | 10 | 10 |
| 11 | 11 | export class VitestSpecifications { | 12 | 12 | private readonly _cachedSpecs = new Map<string, TestSpecification[]>() | @@ -121,15 +121,10 @@ export class VitestSpecifications { | 121 | 121 |
| 122 | 122 | private async filterTestsBySource(specs: TestSpecification[]): Promise<TestSpecification[]> { | 123 | 123 | if (this.vitest.config.changed && !this.vitest.config.related) { | 124 | | - const { VitestGit } = await import('./git') | 125 | | - const vitestGit = new VitestGit(this.vitest.config.root) | 126 | | - const related = await vitestGit.findChangedFiles({ | | 124 | + const related = await this.vitest.vcs.findChangedFiles({ | | 125 | + root: this.vitest.config.root, | 127 | 126 | changedSince: this.vitest.config.changed, | 128 | 127 | }) | 129 | | - if (!related) { | 130 | | - process.exitCode = 1 | 131 | | - throw new GitNotFoundError() | 132 | | - } | 133 | 128 | this.vitest.config.related = Array.from(new Set(related)) | 134 | 129 | } | 135 | 130 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/types/config.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ import type { | 17 | 17 | } from '../reporters' | 18 | 18 | import type { TestCase, TestModule, TestSuite } from '../reporters/reported-tasks' | 19 | 19 | import type { TestSequencerConstructor } from '../sequencers/types' | | 20 | +import type { VCSProvider } from '../vcs/vcs' | 20 | 21 | import type { WatcherTriggerPattern } from '../watcher' | 21 | 22 | import type { BenchmarkUserOptions } from './benchmark' | 22 | 23 | import type { BrowserConfigOptions, ResolvedBrowserOptions } from './browser' | @@ -937,6 +938,15 @@ export interface InlineConfig { | 937 | 938 | * This option only affects `loader.load` method, Vitest always defines a `loader.resolve` to populate the module graph. | 938 | 939 | */ | 939 | 940 | nodeLoader?: boolean | | 941 | + | | 942 | + /** | | 943 | + * Custom provider for detecting changed files. Used with the `--changed` flag | | 944 | + * to determine which files have been modified. | | 945 | + * | | 946 | + * By default, Vitest uses Git to detect changed files. You can provide a custom | | 947 | + * implementation of the `VCSProvider` interface to use a different version control system. | | 948 | + */ | | 949 | + vcsProvider?: VCSProvider | string | 940 | 950 | } | 941 | 951 |
| 942 | 952 | /** | |
Collapse file Copy file name to clipboardExpand all lines: packages/vitest/src/node/vcs/git.ts +6-10Lines changed: 6 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Output } from 'tinyexec' | | 2 | +import type { VCSProvider, VCSProviderOptions } from './vcs' | 2 | 3 | import { resolve } from 'pathe' | 3 | 4 | import { x } from 'tinyexec' | | 5 | +import { GitNotFoundError } from '../errors' | 4 | 6 |
| 5 | | -export interface GitOptions { | 6 | | - changedSince?: string | boolean | 7 | | -} | 8 | | - | 9 | | -export class VitestGit { | | 7 | +export class GitVCSProvider implements VCSProvider { | 10 | 8 | private root!: string | 11 | 9 |
| 12 | | - constructor(private cwd: string) {} | 13 | | - | 14 | 10 | private async resolveFilesWithGitCommand(args: string[]): Promise<string[]> { | 15 | 11 | let result: Output | 16 | 12 |
| @@ -29,10 +25,10 @@ export class VitestGit { | 29 | 25 | .map(changedPath => resolve(this.root, changedPath)) | 30 | 26 | } | 31 | 27 |
| 32 | | - async findChangedFiles(options: GitOptions): Promise<string[] | null> { | 33 | | - const root = await this.getRoot(this.cwd) | | 28 | + async findChangedFiles(options: VCSProviderOptions): Promise<string[]> { | | 29 | + const root = this.root || await this.getRoot(options.root) | 34 | 30 | if (!root) { | 35 | | - return null | | 31 | + throw new GitNotFoundError() | 36 | 32 | } | 37 | 33 |
| 38 | 34 | this.root = root | |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { ModuleRunner } from 'vite/module-runner' | | 2 | +import { resolve } from 'pathe' | | 3 | +import { GitVCSProvider } from './git' | | 4 | + | | 5 | +export interface VCSProviderOptions { | | 6 | + root: string | | 7 | + changedSince?: string | boolean | | 8 | +} | | 9 | + | | 10 | +export interface VCSProvider { | | 11 | + // eslint-disable-next-line ts/method-signature-style | | 12 | + findChangedFiles(options: VCSProviderOptions): Promise<string[]> | | 13 | +} | | 14 | + | | 15 | +export async function loadVCSProvider(runner: ModuleRunner, vcsProvider: string | VCSProvider | undefined): Promise<VCSProvider> { | | 16 | + if (typeof vcsProvider === 'object' && vcsProvider != null) { | | 17 | + return wrapVCSProvider(vcsProvider) | | 18 | + } | | 19 | + if (!vcsProvider || vcsProvider === 'git') { | | 20 | + return new GitVCSProvider() | | 21 | + } | | 22 | + const module = await runner.import(vcsProvider) as { default: VCSProvider } | | 23 | + if (!module.default || typeof module.default !== 'object' || typeof module.default.findChangedFiles !== 'function') { | | 24 | + throw new Error(`The vcsProvider module '${vcsProvider}' doesn't have a default export with \`findChangedFiles\` method.`) | | 25 | + } | | 26 | + return wrapVCSProvider(module.default) | | 27 | +} | | 28 | + | | 29 | +function wrapVCSProvider(provider: VCSProvider): VCSProvider { | | 30 | + return { | | 31 | + async findChangedFiles(options) { | | 32 | + const changedFiles = await provider.findChangedFiles(options) | | 33 | + return changedFiles.map(file => resolve(options.root, file)) | | 34 | + }, | | 35 | + } | | 36 | +} |
|