Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: add config markers / overwrite mode (#33723)

Pavel Feldman5f85a4a4192d
diff --git a/packages/playwright/src/runner/rebase.ts b/packages/playwright/src/runner/rebase.ts
index 780735499b..b412a4a775 100644
--- a/packages/playwright/src/runner/rebase.ts
+++ b/packages/playwright/src/runner/rebase.ts
@@ -97,10 +97,16 @@ 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 gitFolder = findGitRoot(path.dirname(fileName), gitCache);
- const relativeName = path.relative(gitFolder || process.cwd(), fileName);
- files.push(relativeName);
- patches.push(createPatch(relativeName, source, result));
+ if (process.env.PWTEST_UPDATE_SNAPSHOTS === 'overwrite') {
+ await fs.promises.writeFile(fileName, result);
+ } else if (process.env.PWTEST_UPDATE_SNAPSHOTS === 'manual') {
+ await fs.promises.writeFile(fileName, applyPatchWithConflictMarkers(source, result));
+ } else {
+ const gitFolder = findGitRoot(path.dirname(fileName), gitCache);
+ const relativeName = path.relative(gitFolder || process.cwd(), fileName);
+ files.push(relativeName);
+ patches.push(createPatch(relativeName, source, result));
+ }
}
const patchFile = path.join(project.project.outputDir, 'rebaselines.patch');
@@ -143,3 +149,40 @@ function findGitRoot(dir: string, cache: Map<string, string | null>): string | n
cache.set(dir, parentResult);
return parentResult;
}
+
+function applyPatchWithConflictMarkers(oldText: string, newText: string) {
+ const diffResult = diff.diffLines(oldText, newText);
+
+ let result = '';
+ let conflict = false;
+
+ diffResult.forEach(part => {
+ if (part.added) {
+ if (conflict) {
+ result += part.value;
+ result += '>>>>>>> SNAPSHOT\n';
+ conflict = false;
+ } else {
+ result += '<<<<<<< HEAD\n';
+ result += part.value;
+ result += '=======\n';
+ conflict = true;
+ }
+ } else if (part.removed) {
+ result += '<<<<<<< HEAD\n';
+ result += part.value;
+ result += '=======\n';
+ conflict = true;
+ } else {
+ if (conflict) {
+ result += '>>>>>>> SNAPSHOT\n';
+ conflict = false;
+ }
+ result += part.value;
+ }
+ });
+
+ if (conflict)
+ result += '>>>>>>> SNAPSHOT\n';
+ return result;
+}