chore: climb file tree to git root for patches (#33722)

This commit is contained in:
Pavel Feldman 2024-11-21 17:32:07 -08:00 committed by GitHub
parent 605df0be8f
commit e0f0996bbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -54,6 +54,7 @@ export async function applySuggestedRebaselines(config: FullConfigInternal, repo
const patches: string[] = [];
const files: string[] = [];
const gitCache = new Map<string, string | null>();
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, string | null>): 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;
}

View File

@ -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': `<script type="module" src="./index.ts"></script>`,
'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': `<script type="module" src="./index.ts"></script>`,
'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 }) => {