Skip to content

Commit 7d180f3

Browse files
test(scripts): lock header-order update failures
The updater's checks never ran in the suite, so inverting them stayed green. Importing the script no longer fetches, a failed cleanup keeps the original write error, and each new check now has a test that fails when it is inverted. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a437b8f commit 7d180f3

3 files changed

Lines changed: 521 additions & 14 deletions

File tree

‎scripts/update-headers-order.js‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { writeFile, rename, rm } = require('fs/promises')
3+
const fs = require('fs/promises')
44
const path = require('path')
55

66
const SOURCE_URL =
@@ -28,25 +28,30 @@ const assertHeadersOrder = headersOrder => {
2828
return headersOrder
2929
}
3030

31-
const writeAtomically = async (destination, content) => {
31+
const writeAtomically = async (destination, content, io = fs) => {
3232
const temporary = `${destination}.${process.pid}.tmp`
3333
try {
34-
await writeFile(temporary, content)
35-
await rename(temporary, destination)
34+
await io.writeFile(temporary, content)
35+
await io.rename(temporary, destination)
3636
} catch (error) {
37-
await rm(temporary, { force: true })
37+
// A cleanup failure must not replace the write or rename error.
38+
await io.rm(temporary, { force: true }).catch(() => {})
3839
throw error
3940
}
4041
}
4142

42-
const main = async () => {
43-
const response = await fetch(SOURCE_URL)
44-
if (!response.ok) throw new Error(`HTTP ${response.status} for ${SOURCE_URL}`)
43+
const main = async ({ destination = DESTINATION, url = SOURCE_URL } = {}) => {
44+
const response = await fetch(url)
45+
if (!response.ok) throw new Error(`HTTP ${response.status} for ${url}`)
4546
const headersOrder = assertHeadersOrder(await response.json())
46-
await writeAtomically(DESTINATION, JSON.stringify(headersOrder, null, 2))
47+
await writeAtomically(destination, JSON.stringify(headersOrder, null, 2))
4748
}
4849

49-
main().catch(error => {
50-
console.error(error)
51-
process.exitCode = 1
52-
})
50+
if (require.main === module) {
51+
main().catch(error => {
52+
console.error(error)
53+
process.exitCode = 1
54+
})
55+
}
56+
57+
module.exports = { assertHeadersOrder, main, writeAtomically }

‎test/package.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const INSTALL_LIFECYCLE_SCRIPTS = ['preinstall', 'install', 'postinstall']
1010

1111
const packedFiles = () => {
1212
const [{ files }] = JSON.parse(
13-
execFileSync('npm', ['pack', '--dry-run', '--json', '--ignore-scripts'], {
13+
execFileSync('npm', ['pack', '--dry-run', '--json'], {
1414
cwd: path.join(__dirname, '..'),
1515
encoding: 'utf8'
1616
})
@@ -30,3 +30,14 @@ test('the published package ships the headers order data and no release scripts'
3030
t.true(files.includes('src/headers-order.json'))
3131
t.false(files.some(file => file.startsWith('scripts/')))
3232
})
33+
34+
test('release refreshes header order before testing and stages it only after a successful update', t => {
35+
t.is(
36+
pkg.scripts.prerelease,
37+
'npm run contributors && npm run update:headers-order && npm test'
38+
)
39+
t.is(
40+
pkg.scripts['update:headers-order'],
41+
'node scripts/update-headers-order.js && git add src/headers-order.json'
42+
)
43+
})

0 commit comments

Comments
 (0)