Skip to content

Commit

Permalink
chore: add config markers / overwrite mode (#33723)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Nov 22, 2024
1 parent f123f7a commit 5f85a4a
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions packages/playwright/src/runner/rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}

0 comments on commit 5f85a4a

Please sign in to comment.