From 9cd33a752d4fe8ee07bde97464d9a372bc2e4ef2 Mon Sep 17 00:00:00 2001 From: vitalets Date: Wed, 15 Jan 2025 11:53:30 +0400 Subject: [PATCH] Add new config prop populateGitInfo, see #34094 --- packages/playwright/src/common/config.ts | 5 ++++ .../playwright/src/isomorphic/teleReceiver.ts | 1 + packages/playwright/types/test.d.ts | 22 ++++++++++++++++ tests/playwright-test/reporter-html.spec.ts | 25 ++++++++++++++++--- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts index 443cb58319..9f909e8e20 100644 --- a/packages/playwright/src/common/config.ts +++ b/packages/playwright/src/common/config.ts @@ -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); diff --git a/packages/playwright/src/isomorphic/teleReceiver.ts b/packages/playwright/src/isomorphic/teleReceiver.ts index 1d41b793cd..7079017bc6 100644 --- a/packages/playwright/src/isomorphic/teleReceiver.ts +++ b/packages/playwright/src/isomorphic/teleReceiver.ts @@ -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']], diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 501d1bcdb1..3d047edde4 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -1293,6 +1293,23 @@ interface TestConfig { */ 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 { */ 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). */ diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts index 2640cb61c9..de01207873 100644 --- a/tests/playwright-test/reporter-html.spec.ts +++ b/tests/playwright-test/reporter-html.spec.ts @@ -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({