Files
pnpm/env/node.fetcher/test/node.test.ts
Zoltan Kochan 5d5818e44f style: enforce node: protocol for builtin imports (#10951)
Add n/prefer-node-protocol rule and autofix all bare builtin imports
to use the node: prefix. Simplify the simple-import-sort builtins
pattern to just ^node: since all imports now use the prefix.
2026-03-13 07:59:51 +01:00

117 lines
3.7 KiB
TypeScript

import path from 'node:path'
import { Readable } from 'node:stream'
import { jest } from '@jest/globals'
import type { FetchNodeOptionsToDir as FetchNodeOptions } from '@pnpm/node.fetcher'
import { tempDir } from '@pnpm/prepare'
import { StoreIndex } from '@pnpm/store.index'
import AdmZip from 'adm-zip'
import { Response } from 'node-fetch'
jest.unstable_mockModule('detect-libc', () => ({
isNonGlibcLinux: jest.fn(),
}))
const { fetchNode } = await import('@pnpm/node.fetcher')
const { isNonGlibcLinux } = await import('detect-libc')
// A stable fake hex digest used as placeholder sha256 in mock SHASUMS256.txt files.
// Any non-zero value works; the tarball content won't match, so integrity will
// fail — but all URL assertions run before that happens.
const FAKE_SHA256 = '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef'
const fetchMock = jest.fn(async (url: string) => {
if (url.endsWith('SHASUMS256.txt')) {
// Return a minimal SHASUMS file covering the artifacts used in tests.
return new Response(
`${FAKE_SHA256} node-v22.0.0-linux-x64-musl.tar.gz\n`
)
}
if (url.endsWith('.zip')) {
// The Windows code path for pnpm's node bootstrapping expects a subdir
// within the .zip file.
const pkgName = path.basename(url, '.zip')
const zip = new AdmZip()
zip.addFile(`${pkgName}/dummy-file`, Buffer.from('test'))
return new Response(Readable.from(zip.toBuffer()))
}
return new Response(Readable.from(Buffer.alloc(0)))
})
const storeIndexes: StoreIndex[] = []
afterAll(() => {
for (const si of storeIndexes) si.close()
})
beforeEach(() => {
jest.mocked(isNonGlibcLinux).mockReturnValue(Promise.resolve(false))
fetchMock.mockClear()
})
test.skip('install Node using a custom node mirror', async () => {
tempDir()
const nodeMirrorBaseUrl = 'https://pnpm-node-mirror-test.localhost/download/release/'
const storeDir = path.resolve('store')
const storeIndex = new StoreIndex(storeDir)
storeIndexes.push(storeIndex)
const opts: FetchNodeOptions = {
nodeMirrorBaseUrl,
storeDir,
storeIndex,
}
await fetchNode(fetchMock, '16.4.0', path.resolve('node'), opts)
for (const call of fetchMock.mock.calls) {
expect(call[0]).toMatch(nodeMirrorBaseUrl)
}
})
test.skip('install Node using the default node mirror', async () => {
tempDir()
const storeDir = path.resolve('store')
const storeIndex = new StoreIndex(storeDir)
storeIndexes.push(storeIndex)
const opts: FetchNodeOptions = {
storeDir,
storeIndex,
}
await fetchNode(fetchMock, '16.4.0', path.resolve('node'), opts)
for (const call of fetchMock.mock.calls) {
expect(call[0]).toMatch('https://nodejs.org/download/release/')
}
})
test('auto-detects musl on non-glibc Linux and uses unofficial-builds mirror', async () => {
jest.mocked(isNonGlibcLinux).mockReturnValue(Promise.resolve(true))
tempDir()
// The function will throw because the downloaded tarball content won't match
// the fake sha256 we put in the SHASUMS256.txt mock, but all fetch calls are
// recorded before the integrity check, so we can assert the correct URLs.
const storeIndex = new StoreIndex(path.resolve('store'))
storeIndexes.push(storeIndex)
await expect(
fetchNode(fetchMock, '22.0.0', path.resolve('node'), {
storeDir: path.resolve('store'),
storeIndex,
platform: 'linux',
arch: 'x64',
retry: { retries: 0 },
})
).rejects.toThrow()
const shasumsUrl = fetchMock.mock.calls[0][0] as string
expect(shasumsUrl).toContain('unofficial-builds.nodejs.org')
const tarballUrl = fetchMock.mock.calls[1][0] as string
expect(tarballUrl).toContain('unofficial-builds.nodejs.org')
expect(tarballUrl).toContain('node-v22.0.0-linux-x64-musl.tar.gz')
})