allow comparison against base commit

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

View file

@ -49,7 +49,7 @@ export class FullConfigInternal {
cliArgs: string[] = []; cliArgs: string[] = [];
cliGrep: string | undefined; cliGrep: string | undefined;
cliGrepInvert: string | undefined; cliGrepInvert: string | undefined;
cliOnlyChanged = false; cliOnlyChanged: string | boolean | undefined;
cliProjectFilter?: string[]; cliProjectFilter?: string[];
cliListOnly = false; cliListOnly = false;
cliPassWithNoTests?: boolean; cliPassWithNoTests?: boolean;

View file

@ -192,7 +192,7 @@ async function runTests(args: string[], opts: { [key: string]: any }) {
config.cliArgs = args; config.cliArgs = args;
config.cliGrep = opts.grep as string | undefined; 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.cliGrepInvert = opts.grepInvert as string | undefined;
config.cliListOnly = !!opts.list; config.cliListOnly = !!opts.list;
config.cliProjectFilter = opts.project || undefined; config.cliProjectFilter = opts.project || undefined;
@ -353,7 +353,7 @@ const testOptions: [string, string][] = [
['--max-failures <N>', `Stop after the first N failures`], ['--max-failures <N>', `Stop after the first N failures`],
['--no-deps', 'Do not run project dependencies'], ['--no-deps', 'Do not run project dependencies'],
['--output <dir>', `Folder for output artifacts (default: "test-results")`], ['--output <dir>', `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`], ['--pass-with-no-tests', `Makes test run succeed even if no tests were found`],
['--project <project-name...>', `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)`], ['--project <project-name...>', `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)`],
['--quiet', `Suppress stdio`], ['--quiet', `Suppress stdio`],

View file

@ -80,16 +80,17 @@ export type TestFileFilter = {
column: number | null; column: number | null;
}; };
export async function detectChangedFiles(): Promise<string[]> { export async function detectChangedFiles(onlyChangedParam: string | true): Promise<string[]> {
const baseCommit = onlyChangedParam === true ? 'HEAD' : onlyChangedParam;
const untrackedFiles = childProcess.execSync('git ls-files --others --exclude-standard', { encoding: 'utf-8' }).split('\n').filter(Boolean); 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]; return [...untrackedFiles, ...changedFiles];
} }
export async function createFileFiltersFromArguments(args: string[], onlyChanged: boolean): Promise<TestFileFilter[]> { export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | boolean | undefined): Promise<TestFileFilter[]> {
if (onlyChanged) if (onlyChanged)
args = await detectChangedFiles(); args = await detectChangedFiles(onlyChanged);
return args.map(arg => { return args.map(arg => {
const match = /^(.*?):(\d+):?(\d+)?$/.exec(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<Matcher> { export async function createFileMatcherFromArguments(args: string[], onlyChanged: string | undefined): Promise<Matcher> {
const filters = await createFileFiltersFromArguments(args, onlyChanged); const filters = await createFileFiltersFromArguments(args, onlyChanged);
return createFileMatcher(filters.map(filter => filter.re || filter.exact || '')); return createFileMatcher(filters.map(filter => filter.re || filter.exact || ''));
} }

View file

@ -74,3 +74,32 @@ test('should detect changed files', async ({ runInlineTest }) => {
expect(result.output).toContain('b.spec.ts'); 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');
});