diff --git a/packages/playwright/src/runner/rebase.ts b/packages/playwright/src/runner/rebase.ts index bc59e8374a..780735499b 100644 --- a/packages/playwright/src/runner/rebase.ts +++ b/packages/playwright/src/runner/rebase.ts @@ -54,6 +54,7 @@ export async function applySuggestedRebaselines(config: FullConfigInternal, repo const patches: string[] = []; const files: string[] = []; + const gitCache = new Map(); for (const fileName of [...suggestedRebaselines.keys()].sort()) { const source = await fs.promises.readFile(fileName, 'utf8'); @@ -96,7 +97,8 @@ export async function applySuggestedRebaselines(config: FullConfigInternal, repo for (const range of ranges) result = result.substring(0, range.start) + range.newText + result.substring(range.end); - const relativeName = path.relative(process.cwd(), fileName); + const gitFolder = findGitRoot(path.dirname(fileName), gitCache); + const relativeName = path.relative(gitFolder || process.cwd(), fileName); files.push(relativeName); patches.push(createPatch(relativeName, source, result)); } @@ -119,3 +121,25 @@ function createPatch(fileName: string, before: string, after: string) { ...text.split('\n').slice(4) ].join('\n'); } + +function findGitRoot(dir: string, cache: Map): string | null { + const result = cache.get(dir); + if (result !== undefined) + return result; + + const gitPath = path.join(dir, '.git'); + if (fs.existsSync(gitPath) && fs.lstatSync(gitPath).isDirectory()) { + cache.set(dir, dir); + return dir; + } + + const parentDir = path.dirname(dir); + if (dir === parentDir) { + cache.set(dir, null); + return null; + } + + const parentResult = findGitRoot(parentDir, cache); + cache.set(dir, parentResult); + return parentResult; +} diff --git a/tests/playwright-test/update-aria-snapshot.spec.ts b/tests/playwright-test/update-aria-snapshot.spec.ts index 63769f8408..f7bc744561 100644 --- a/tests/playwright-test/update-aria-snapshot.spec.ts +++ b/tests/playwright-test/update-aria-snapshot.spec.ts @@ -26,6 +26,7 @@ function trimPatch(patch: string) { test('should update snapshot with the update-snapshots flag with multiple projects', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'playwright.config.ts': ` export default { projects: [{ name: 'p1' }, { name: 'p2' }] }; `, @@ -73,6 +74,7 @@ test('should update snapshot with the update-snapshots flag with multiple projec test('should update missing snapshots', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'a.spec.ts': ` import { test, expect } from '@playwright/test'; test('test', async ({ page }) => { @@ -116,6 +118,7 @@ test('should update missing snapshots', async ({ runInlineTest }, testInfo) => { test('should generate baseline with regex', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'a.spec.ts': ` import { test, expect } from '@playwright/test'; test('test', async ({ page }) => { @@ -172,6 +175,7 @@ test('should generate baseline with regex', async ({ runInlineTest }, testInfo) test('should generate baseline with special characters', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'a.spec.ts': ` import { test, expect } from '@playwright/test'; test('test', async ({ page }) => { @@ -241,6 +245,7 @@ test('should generate baseline with special characters', async ({ runInlineTest test('should update missing snapshots in tsx', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'playwright.config.ts': playwrightCtConfigText, 'playwright/index.html': ``, 'playwright/index.ts': ``, @@ -286,6 +291,7 @@ test('should update missing snapshots in tsx', async ({ runInlineTest }, testInf test('should update multiple files', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'playwright.config.ts': playwrightCtConfigText, 'playwright/index.html': ``, 'playwright/index.ts': ``, @@ -365,6 +371,7 @@ diff --git a/src/button-2.test.tsx b/src/button-2.test.tsx test('should generate baseline for input values', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'a.spec.ts': ` import { test, expect } from '@playwright/test'; test('test', async ({ page }) => { @@ -400,6 +407,7 @@ test('should generate baseline for input values', async ({ runInlineTest }, test test('should not update snapshots when locator did not match', async ({ runInlineTest }, testInfo) => { const result = await runInlineTest({ + '.git/marker': '', 'a.spec.ts': ` import { test, expect } from '@playwright/test'; test('test', async ({ page }) => {