Add new config prop populateGitInfo, see #34094

This commit is contained in:
vitalets 2025-01-15 11:53:30 +04:00
parent 275f334b58
commit 9cd33a752d
4 changed files with 49 additions and 4 deletions

View file

@ -20,6 +20,7 @@ import os from 'os';
import type { Config, Fixtures, Project, ReporterDescription } from '../../types/test';
import type { Location } from '../../types/testReporter';
import type { TestRunnerPluginRegistration } from '../plugins';
import { gitCommitInfo } from '../plugins/gitCommitInfoPlugin';
import { getPackageJsonPath, mergeObjects } from '../util';
import type { Matcher } from '../util';
import type { ConfigCLIOverrides } from './ipc';
@ -91,6 +92,7 @@ export class FullConfigInternal {
grepInvert: takeFirst(userConfig.grepInvert, null),
maxFailures: takeFirst(configCLIOverrides.debug ? 1 : undefined, configCLIOverrides.maxFailures, userConfig.maxFailures, 0),
metadata: takeFirst(userConfig.metadata, {}),
populateGitInfo: takeFirst(userConfig.populateGitInfo, false),
preserveOutput: takeFirst(userConfig.preserveOutput, 'always'),
reporter: takeFirst(configCLIOverrides.reporter, resolveReporters(userConfig.reporter, configDir), [[defaultReporter]]),
reportSlowTests: takeFirst(userConfig.reportSlowTests, { max: 5, threshold: 15000 }),
@ -134,6 +136,9 @@ export class FullConfigInternal {
this.webServers = [];
}
if (this.config.populateGitInfo)
this.plugins.push({ factory: gitCommitInfo });
const projectConfigs = configCLIOverrides.projects || userConfig.projects || [userConfig];
this.projects = projectConfigs.map(p => new FullProjectInternal(configDir, userConfig, this, p, this.configCLIOverrides, packageJsonDir));
resolveProjectDependencies(this.projects);

View file

@ -596,6 +596,7 @@ export const baseFullConfig: reporterTypes.FullConfig = {
grepInvert: null,
maxFailures: 0,
metadata: {},
populateGitInfo: false,
preserveOutput: 'always',
projects: [],
reporter: [[process.env.CI ? 'dot' : 'list']],

View file

@ -1293,6 +1293,23 @@ interface TestConfig<TestArgs = {}, WorkerArgs = {}> {
*/
outputDir?: string;
/**
* Whether to populate [metadata](https://playwright.dev/docs/api/class-testconfig#test-config-output-metadata) with Git info.
*
* **Usage**
*
* ```js
* // playwright.config.ts
* import { defineConfig } from '@playwright/test';
*
* export default defineConfig({
* populateGitInfo: !!process.env.CI,
* });
* ```
*
*/
populateGitInfo?: boolean;
/**
* Whether to preserve test output in the
* [testConfig.outputDir](https://playwright.dev/docs/api/class-testconfig#test-config-output-dir). Defaults to
@ -1796,6 +1813,11 @@ export interface FullConfig<TestArgs = {}, WorkerArgs = {}> {
*/
metadata: Metadata;
/**
* See [testConfig.populateGitInfo](https://playwright.dev/docs/api/class-testconfig#test-config-populategitinfo).
*/
populateGitInfo: boolean;
/**
* See [testConfig.preserveOutput](https://playwright.dev/docs/api/class-testconfig#test-config-preserve-output).
*/

View file

@ -1144,14 +1144,12 @@ for (const useIntermediateMergeReport of [true, false] as const) {
});
test.describe('gitCommitInfo plugin', () => {
test('should include metadata', async ({ runInlineTest, writeFiles, showReport, page }) => {
test('should include metadata with populateGitInfo = true', async ({ runInlineTest, writeFiles, showReport, page }) => {
const files = {
'uncommitted.txt': `uncommitted file`,
'playwright.config.ts': `
import { gitCommitInfo } from 'playwright/lib/plugins';
import { test, expect } from '@playwright/test';
const plugins = [gitCommitInfo()];
export default { '@playwright/test': { plugins } };
export default { populateGitInfo: true };
`,
'example.spec.ts': `
import { test, expect } from '@playwright/test';
@ -1197,6 +1195,25 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect.soft(page.getByTestId('metadata-error')).not.toBeVisible();
});
test('should not include metadata with populateGitInfo = false', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'uncommitted.txt': `uncommitted file`,
'playwright.config.ts': `
export default { populateGitInfo: false };
`,
'example.spec.ts': `
import { test, expect } from '@playwright/test';
test('my sample test', async ({}) => { expect(2).toBe(2); });
`,
}, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' }, undefined);
await showReport();
expect(result.exitCode).toBe(0);
await expect.soft(page.locator('text="my sample test"')).toBeVisible();
await expect.soft(page.getByTestId('metadata-error')).not.toBeVisible();
await expect.soft(page.getByTestId('metadata-chip')).not.toBeVisible();
});
test('should use explicitly supplied metadata', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({