make it understand dependency structure

This commit is contained in:
Simon Knott 2024-07-17 12:04:33 +02:00
parent 824bea2aa0
commit 72d7c86290
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 27 additions and 4 deletions

View file

@ -36,7 +36,7 @@ export async function collectProjectsAndTestFiles(testRun: TestRun, doNotRunTest
const config = testRun.config; const config = testRun.config;
const fsCache = new Map(); const fsCache = new Map();
const sourceMapCache = new Map(); const sourceMapCache = new Map();
const cliFileMatcher = (config.cliArgs.length || config.cliOnlyChanged) ? await createFileMatcherFromArguments(config.cliArgs, config.cliOnlyChanged) : null; const cliFileMatcher = config.cliArgs.length ? await createFileMatcherFromArguments(config.cliArgs, undefined) : null;
// First collect all files for the projects in the command line, don't apply any file filters. // First collect all files for the projects in the command line, don't apply any file filters.
const allFilesForProject = new Map<FullProjectInternal, string[]>(); const allFilesForProject = new Map<FullProjectInternal, string[]>();

View file

@ -26,6 +26,7 @@ import type { TestInfoError } from './../types/test';
import type { Location } from './../types/testReporter'; import type { Location } from './../types/testReporter';
import { calculateSha1, isRegExp, isString, sanitizeForFilePath, stringifyStackFrames } from 'playwright-core/lib/utils'; import { calculateSha1, isRegExp, isString, sanitizeForFilePath, stringifyStackFrames } from 'playwright-core/lib/utils';
import type { RawStack } from 'playwright-core/lib/utils'; import type { RawStack } from 'playwright-core/lib/utils';
import { affectedTestFiles } from './transform/compilationCache';
const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..'); const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..');
const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core/package.json')); const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core/package.json'));
@ -84,7 +85,7 @@ 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 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); const changedFiles = childProcess.execSync(`git diff ${baseCommit} --name-only`, { encoding: 'utf-8' }).split('\n').filter(Boolean);
return [...untrackedFiles, ...changedFiles]; return [...untrackedFiles, ...changedFiles, ...affectedTestFiles([...untrackedFiles, ...changedFiles].map(file => path.resolve(file)))];
} }
export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | undefined): Promise<TestFileFilter[]> { export async function createFileFiltersFromArguments(args: string[], onlyChanged: string | undefined): Promise<TestFileFilter[]> {

View file

@ -27,11 +27,17 @@ const test = baseTest.extend({
await writeFiles({ await writeFiles({
'a.spec.ts': ` 'a.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(2); }); import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`, `,
'b.spec.ts': ` 'b.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(2); }); import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export const answer = 42;
export const question = "???";
`, `,
}); });
git(`init --initial-branch=main`); git(`init --initial-branch=main`);
@ -110,3 +116,19 @@ test.describe('should be smart about PR base reference from CI', () => {
testCIEnvironment('Github Actions', 'GITHUB_BASE_REF'); testCIEnvironment('Github Actions', 'GITHUB_BASE_REF');
testCIEnvironment('Azure DevOps', 'Build.PullRequest.TargetBranch'); testCIEnvironment('Azure DevOps', 'Build.PullRequest.TargetBranch');
}); });
test('should understand dependency structure', async ({ runInlineTest, setupRepository, writeFiles }) => {
await setupRepository();
await writeFiles({
'utils.ts': `
export const answer = 42;
export const question = "what is the answer to life the universe and everything";
`,
});
const result = await runInlineTest({}, { 'only-changed': true });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(2);
expect(result.output).toContain('a.spec.ts');
expect(result.output).toContain('b.spec.ts');
});