detect base ref from environment variables

This commit is contained in:
Simon Knott 2024-07-17 11:21:05 +02:00
parent 2d9a299d66
commit d907883518
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
4 changed files with 76 additions and 5 deletions

View file

@ -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;

View file

@ -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;

View file

@ -80,15 +80,14 @@ export type TestFileFilter = {
column: number | null;
};
export async function detectChangedFiles(onlyChangedParam: string | true): Promise<string[]> {
const baseCommit = onlyChangedParam === true ? 'HEAD' : onlyChangedParam;
export async function detectChangedFiles(baseCommit: string): Promise<string[]> {
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<TestFileFilter[]> {
export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | undefined): Promise<TestFileFilter[]> {
if (onlyChanged)
args = await detectChangedFiles(onlyChanged);

View file

@ -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');
});
});