From 2d9a299d66358d0259f937ded6e98f2727d43b5a Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 17 Jul 2024 11:00:28 +0200 Subject: [PATCH] allow comparison against base commit --- packages/playwright/src/common/config.ts | 2 +- packages/playwright/src/program.ts | 4 +-- packages/playwright/src/util.ts | 11 ++++---- tests/playwright-test/only-changed.spec.ts | 29 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts index 8786d50558..0a88fe85fd 100644 --- a/packages/playwright/src/common/config.ts +++ b/packages/playwright/src/common/config.ts @@ -49,7 +49,7 @@ export class FullConfigInternal { cliArgs: string[] = []; cliGrep: string | undefined; cliGrepInvert: string | undefined; - cliOnlyChanged = false; + cliOnlyChanged: string | boolean | undefined; cliProjectFilter?: string[]; cliListOnly = false; cliPassWithNoTests?: boolean; diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts index 47d8ca116d..d64de03648 100644 --- a/packages/playwright/src/program.ts +++ b/packages/playwright/src/program.ts @@ -192,7 +192,7 @@ async function runTests(args: string[], opts: { [key: string]: any }) { config.cliArgs = args; config.cliGrep = opts.grep as string | undefined; - config.cliOnlyChanged = !!opts.onlyChanged; + config.cliOnlyChanged = opts.onlyChanged as string | boolean | undefined; config.cliGrepInvert = opts.grepInvert as string | undefined; config.cliListOnly = !!opts.list; config.cliProjectFilter = opts.project || undefined; @@ -353,7 +353,7 @@ const testOptions: [string, string][] = [ ['--max-failures ', `Stop after the first N failures`], ['--no-deps', 'Do not run project dependencies'], ['--output ', `Folder for output artifacts (default: "test-results")`], - ['--only-changed', `something something docs`], + ['--only-changed [base-commit]', `something something docs`], ['--pass-with-no-tests', `Makes test run succeed even if no tests were found`], ['--project ', `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)`], ['--quiet', `Suppress stdio`], diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts index a21b06c9b1..373e89963a 100644 --- a/packages/playwright/src/util.ts +++ b/packages/playwright/src/util.ts @@ -80,16 +80,17 @@ export type TestFileFilter = { column: number | null; }; -export async function detectChangedFiles(): Promise { +export async function detectChangedFiles(onlyChangedParam: string | true): Promise { + const baseCommit = onlyChangedParam === true ? 'HEAD' : onlyChangedParam; const untrackedFiles = childProcess.execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8' }).split('\n').filter(Boolean); - const changedFiles = childProcess.execSync('git diff --name-only', { encoding: 'utf-8' }).split('\n').filter(Boolean); + const changedFiles = childProcess.execSync(`git diff ${baseCommit} --name-only`, { encoding: 'utf-8' }).split('\n').filter(Boolean); return [...untrackedFiles, ...changedFiles]; } -export async function createFileFiltersFromArguments(args: string[], onlyChanged: boolean): Promise { +export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | boolean | undefined): Promise { if (onlyChanged) - args = await detectChangedFiles(); + args = await detectChangedFiles(onlyChanged); return args.map(arg => { const match = /^(.*?):(\d+):?(\d+)?$/.exec(arg); @@ -101,7 +102,7 @@ export async function createFileFiltersFromArguments(args: string[], onlyChanged }); } -export async function createFileMatcherFromArguments(args: string[], onlyChanged: boolean): Promise { +export async function createFileMatcherFromArguments(args: string[], onlyChanged: string | undefined): Promise { const filters = await createFileFiltersFromArguments(args, onlyChanged); return createFileMatcher(filters.map(filter => filter.re || filter.exact || '')); } diff --git a/tests/playwright-test/only-changed.spec.ts b/tests/playwright-test/only-changed.spec.ts index 86a71abf11..9c4f911129 100644 --- a/tests/playwright-test/only-changed.spec.ts +++ b/tests/playwright-test/only-changed.spec.ts @@ -74,3 +74,32 @@ test('should detect changed files', async ({ runInlineTest }) => { expect(result.output).toContain('b.spec.ts'); }); +test('should diff based on base commit', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(2); }); + `, + 'b.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(2); }); + `, + async [magicFileCreationSymbol](baseDir) { + execSync(`git init --initial-branch=main`, { cwd: baseDir }); + execSync(`git add .`, { cwd: baseDir }); + execSync(`git commit -m init`, { cwd: baseDir }); + + await writeFile(join(baseDir, 'b.spec.ts'), ` + import { test, expect } from '@playwright/test'; + test('fails', () => { expect(1).toBe(3); }); + `); + + execSync(`git commit -a -m update`, { cwd: baseDir }); + } + }, { 'only-changed': `HEAD~1` }); + + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(result.output).toContain('b.spec.ts'); +}); +