From d907883518934e09c7b1fad1efc9d1636379ac30 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 17 Jul 2024 11:21:05 +0200 Subject: [PATCH] detect base ref from environment variables --- packages/playwright/src/common/config.ts | 2 +- packages/playwright/src/program.ts | 9 ++- packages/playwright/src/util.ts | 5 +- tests/playwright-test/only-changed.spec.ts | 65 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts index 0a88fe85fd..610382e175 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: string | boolean | undefined; + cliOnlyChanged: string | undefined; cliProjectFilter?: string[]; cliListOnly = false; cliPassWithNoTests?: boolean; diff --git a/packages/playwright/src/program.ts b/packages/playwright/src/program.ts index d64de03648..412d323631 100644 --- a/packages/playwright/src/program.ts +++ b/packages/playwright/src/program.ts @@ -153,6 +153,13 @@ Examples: $ npx playwright merge-reports playwright-report`); } +function getOnlyChangedArg(input: string | boolean | undefined): string | undefined { + if (typeof input === 'string') + return input; + if (input === true) + return process.env.GITHUB_BASE_REF ?? process.env['Build.PullRequest.TargetBranch'] ?? 'HEAD'; +} + async function runTests(args: string[], opts: { [key: string]: any }) { await startProfiling(); @@ -192,7 +199,7 @@ async function runTests(args: string[], opts: { [key: string]: any }) { config.cliArgs = args; config.cliGrep = opts.grep as string | undefined; - config.cliOnlyChanged = opts.onlyChanged as string | boolean | undefined; + config.cliOnlyChanged = getOnlyChangedArg(opts.onlyChanged); config.cliGrepInvert = opts.grepInvert as string | undefined; config.cliListOnly = !!opts.list; config.cliProjectFilter = opts.project || undefined; diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts index 373e89963a..ce20dcb39d 100644 --- a/packages/playwright/src/util.ts +++ b/packages/playwright/src/util.ts @@ -80,15 +80,14 @@ export type TestFileFilter = { column: number | null; }; -export async function detectChangedFiles(onlyChangedParam: string | true): Promise { - const baseCommit = onlyChangedParam === true ? 'HEAD' : onlyChangedParam; +export async function detectChangedFiles(baseCommit: string): Promise { const untrackedFiles = childProcess.execSync('git ls-files --others --exclude-standard', { 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: string | boolean | undefined): Promise { +export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | undefined): Promise { if (onlyChanged) args = await detectChangedFiles(onlyChanged); diff --git a/tests/playwright-test/only-changed.spec.ts b/tests/playwright-test/only-changed.spec.ts index 9c4f911129..d852d25082 100644 --- a/tests/playwright-test/only-changed.spec.ts +++ b/tests/playwright-test/only-changed.spec.ts @@ -103,3 +103,68 @@ test('should diff based on base commit', async ({ runInlineTest }) => { expect(result.output).toContain('b.spec.ts'); }); +test.describe('should be smart about PR base reference from CI', () => { + test('GitHub Actions', 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': true }, { GITHUB_BASE_REF: 'HEAD~1' }); + + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(result.output).toContain('b.spec.ts'); + }); + + + test('Azure DevOps', 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': true + }, { + // see https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch#primary-artifact + 'Build.PullRequest.TargetBranch': 'HEAD~1' + }); + + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(result.output).toContain('b.spec.ts'); + }); +});