chore: add config markers / overwrite mode (#33723)
This commit is contained in:
parent
f123f7ac69
commit
5f85a4a419
|
@ -97,10 +97,16 @@ export async function applySuggestedRebaselines(config: FullConfigInternal, repo
|
||||||
for (const range of ranges)
|
for (const range of ranges)
|
||||||
result = result.substring(0, range.start) + range.newText + result.substring(range.end);
|
result = result.substring(0, range.start) + range.newText + result.substring(range.end);
|
||||||
|
|
||||||
const gitFolder = findGitRoot(path.dirname(fileName), gitCache);
|
if (process.env.PWTEST_UPDATE_SNAPSHOTS === 'overwrite') {
|
||||||
const relativeName = path.relative(gitFolder || process.cwd(), fileName);
|
await fs.promises.writeFile(fileName, result);
|
||||||
files.push(relativeName);
|
} else if (process.env.PWTEST_UPDATE_SNAPSHOTS === 'manual') {
|
||||||
patches.push(createPatch(relativeName, source, result));
|
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');
|
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);
|
cache.set(dir, parentResult);
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue