Files
next.js/test/scripts/merge-errors-json/merge-errors-json.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

125 lines
3.1 KiB
TypeScript

import { mkdtempSync, rmSync } from 'fs'
import { writeJSONSync, readJSONSync } from 'fs-extra'
import { join } from 'path'
import { tmpdir } from 'os'
import execa from 'execa'
describe('merge-errors-json driver', () => {
it('should correctly merge errors.json files with conflicting new entries', async () => {
const base = {
'1': 'Original message 1',
'2': 'Original message 2',
}
const current = {
...base,
'3': 'From a conflicting branch that was merged first 1',
'4': 'From a conflicting branch that was merged first 2',
}
const rebased = await simulateRebase({
base,
current,
incoming: {
...base,
'3': 'From our branch 1',
'4': 'From our branch 2',
},
})
expect(rebased).toEqual({
...current,
'5': 'From our branch 1',
'6': 'From our branch 2',
})
})
it('should handle empty base file', async () => {
const base = {}
const current = {
'1': 'Message from a conflicting branch that was merged first',
}
const rebased = await simulateRebase({
base,
current,
incoming: { '1': 'New message from our branch' },
})
expect(rebased).toEqual({
...current,
'2': 'New message from our branch',
})
})
it('should handle stacked PRs', async () => {
const base = {
'1': 'Original message 1',
'2': 'Original message 2',
}
const current = {
...base,
'3': 'Message from a conflicting branch that was merged first',
}
const ourBranch1 = {
...base,
'3': 'New message from our-branch-1',
}
const ourBranch2 = {
...ourBranch1,
'4': 'New message from our-branch-2', // will conflict after our-branch-1 is rebased and its error is renumbered to 4
}
const branch1Rebased = await simulateRebase({
base: base,
current: current,
incoming: ourBranch1,
})
expect(branch1Rebased).toEqual({
...current,
'4': 'New message from our-branch-1',
})
const branch2Rebased = await simulateRebase({
base: ourBranch1,
current: branch1Rebased,
incoming: ourBranch2,
})
expect(branch2Rebased).toEqual({
...branch1Rebased,
'5': 'New message from our-branch-2',
})
})
})
type Contents = Record<string, string>
async function simulateRebase({
base,
current,
incoming,
}: {
base: Contents
current: Contents
incoming: Contents
}) {
const tempDir = mkdtempSync(join(tmpdir(), 'merge-driver-test-'))
const paths = {
base: join(tempDir, 'base.json'),
current: join(tempDir, 'current.json'),
other: join(tempDir, 'other.json'),
}
writeJSONSync(paths.base, base, { spaces: 2 })
// during a rebase, the branch we're rebasing onto will be "current", and our commit will be "other"
writeJSONSync(paths.current, current, { spaces: 2 })
writeJSONSync(paths.other, incoming, { spaces: 2 })
await execa('node', [
'scripts/merge-errors-json/merge.mjs',
paths.current,
paths.base,
paths.other,
])
const result = readJSONSync(paths.current)
rmSync(tempDir, { recursive: true, force: true })
return result
}