Skip to content

Commit 7d166bd

Browse files
committed
test(install): cover Bun bootstrap branches
Source-archive preparation depends on both the npm CLI path and a runnable local Bun binary; cover fresh installation, cache reuse, and fail-closed behavior.
1 parent 5468eb6 commit 7d166bd

1 file changed

Lines changed: 49 additions & 13 deletions

File tree

scripts/test/no-yarn-dev-references.spec.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const path = require('node:path')
1212

1313
const yaml = require('yaml')
1414

15+
const { getBunBinary } = require('../bun')
16+
1517
const repoRoot = path.resolve(__dirname, '..', '..')
1618
const guardFile = 'scripts/test/no-yarn-dev-references.spec.js'
1719
const allowedLinePatterns = new Map([
@@ -142,24 +144,18 @@ describe('no yarn dev references', function () {
142144
.devDependencies
143145
const bootstrapDirectory = path.join(repoRoot, 'node_modules', '.cache', `bun-${bunVersion}`)
144146
const backupDirectory = `${bootstrapDirectory}.backup-${process.pid}`
147+
const originalNpmExecPath = process.env.npm_execpath
148+
const originalPath = process.env.PATH
149+
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
145150

146151
try {
147152
if (fs.existsSync(bootstrapDirectory)) {
148153
fs.renameSync(bootstrapDirectory, backupDirectory)
149154
}
150-
const bunBinary = execFileSync(
151-
process.execPath,
152-
['-e', "process.stdout.write(require('./scripts/bun').getBunBinary())"],
153-
{
154-
cwd: repoRoot,
155-
encoding: 'utf8',
156-
env: {
157-
...process.env,
158-
npm_execpath: '',
159-
PATH: [path.dirname(process.execPath), '/usr/bin', '/bin'].join(path.delimiter),
160-
},
161-
}
162-
)
155+
const globalNpmRoot = execFileSync(npm, ['root', '--global'], { encoding: 'utf8' }).trim()
156+
process.env.npm_execpath = path.join(globalNpmRoot, 'npm', 'bin', 'npm-cli.js')
157+
process.env.PATH = [path.dirname(process.execPath), '/usr/bin', '/bin'].join(path.delimiter)
158+
const bunBinary = getBunBinary()
163159

164160
assert.strictEqual(await Promise.resolve(bunBinary), path.join(
165161
bootstrapDirectory,
@@ -169,12 +165,52 @@ describe('no yarn dev references', function () {
169165
'bun.exe'
170166
))
171167
assert.strictEqual(execFileSync(bunBinary, ['--version'], { encoding: 'utf8' }).trim(), bunVersion)
168+
assert.strictEqual(getBunBinary(), bunBinary)
172169
const bootstrapPackage = JSON.parse(
173170
fs.readFileSync(path.join(bootstrapDirectory, 'package.json'), 'utf8')
174171
)
175172
assert.strictEqual(bootstrapPackage.allowScripts[`bun@${bunVersion}`], true)
176173
assert.strictEqual(fs.existsSync(path.join(bootstrapDirectory, 'package-lock.json')), false)
177174
} finally {
175+
if (originalNpmExecPath === undefined) {
176+
delete process.env.npm_execpath
177+
} else {
178+
process.env.npm_execpath = originalNpmExecPath
179+
}
180+
process.env.PATH = originalPath
181+
fs.rmSync(bootstrapDirectory, { recursive: true, force: true })
182+
if (fs.existsSync(backupDirectory)) {
183+
fs.renameSync(backupDirectory, bootstrapDirectory)
184+
}
185+
}
186+
})
187+
188+
it('fails when the pinned Bun bootstrap does not produce a runnable binary', () => {
189+
const { bun: bunVersion } = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'))
190+
.devDependencies
191+
const bootstrapDirectory = path.join(repoRoot, 'node_modules', '.cache', `bun-${bunVersion}`)
192+
const backupDirectory = `${bootstrapDirectory}.backup-${process.pid}`
193+
const fakeNpm = path.join(os.tmpdir(), `dd-fake-npm-${process.pid}.js`)
194+
const originalNpmExecPath = process.env.npm_execpath
195+
const originalPath = process.env.PATH
196+
197+
try {
198+
if (fs.existsSync(bootstrapDirectory)) {
199+
fs.renameSync(bootstrapDirectory, backupDirectory)
200+
}
201+
fs.writeFileSync(fakeNpm, '')
202+
process.env.npm_execpath = fakeNpm
203+
process.env.PATH = [path.dirname(process.execPath), '/usr/bin', '/bin'].join(path.delimiter)
204+
205+
assert.throws(() => getBunBinary(), /Could not install Bun/)
206+
} finally {
207+
if (originalNpmExecPath === undefined) {
208+
delete process.env.npm_execpath
209+
} else {
210+
process.env.npm_execpath = originalNpmExecPath
211+
}
212+
process.env.PATH = originalPath
213+
fs.rmSync(fakeNpm, { force: true })
178214
fs.rmSync(bootstrapDirectory, { recursive: true, force: true })
179215
if (fs.existsSync(backupDirectory)) {
180216
fs.renameSync(backupDirectory, bootstrapDirectory)

0 commit comments

Comments
 (0)