test: make checkPlatform negation tests platform-independent (#11411)

* test: make checkPlatform negation tests platform-independent

The two multi-valued supportedArchitectures tests added in #11375 used
'current' alongside a value that the negation in the wanted platform
matched on some hosts (e.g. ['linux', 'current'] on Windows expands to
['linux', 'win32'], which is correctly rejected by ['!win32']). Replace
'current' with fixed second values so the multi-value code path is still
exercised without depending on process.platform / process.arch.

* test: mock process.platform / process.arch instead of avoiding 'current'

Restores the more realistic scenario from #11375 where supportedArchitectures
mixes a fixed value with 'current'. Mock process.platform / process.arch
explicitly per test so the result no longer depends on the host CI runner.
This commit is contained in:
Zoltan Kochan
2026-04-30 22:07:33 +02:00
committed by GitHub
parent 086c5e91e8
commit e6aca55bd8

View File

@@ -1,4 +1,4 @@
import { expect, jest, test } from '@jest/globals'
import { afterEach, expect, jest, test } from '@jest/globals'
import type * as DetectLibc from 'detect-libc'
const packageId = 'registry.npmjs.org/foo/1.0.0'
@@ -13,6 +13,22 @@ jest.mock('detect-libc', () => {
const { checkPlatform } = await import('../lib/checkPlatform.js')
const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')!
const originalArch = Object.getOwnPropertyDescriptor(process, 'arch')!
function setPlatform (platform: NodeJS.Platform): void {
Object.defineProperty(process, 'platform', { ...originalPlatform, value: platform })
}
function setArch (arch: NodeJS.Architecture): void {
Object.defineProperty(process, 'arch', { ...originalArch, value: arch })
}
afterEach(() => {
Object.defineProperty(process, 'platform', originalPlatform)
Object.defineProperty(process, 'arch', originalArch)
})
test('target cpu wrong', () => {
const target = {
cpu: 'enten-cpu',
@@ -153,6 +169,7 @@ test('accept another libc', () => {
})
test('accept negated os with multi-valued supportedArchitectures', () => {
setPlatform('linux')
expect(checkPlatform(packageId, { cpu: 'any', os: ['!win32'], libc: 'any' }, {
os: ['linux', 'current'],
cpu: ['current'],
@@ -161,6 +178,7 @@ test('accept negated os with multi-valued supportedArchitectures', () => {
})
test('accept negated cpu with multi-valued supportedArchitectures', () => {
setArch('x64')
expect(checkPlatform(packageId, { cpu: ['!ia32'], os: 'any', libc: 'any' }, {
os: ['current'],
cpu: ['x64', 'current'],
@@ -169,6 +187,7 @@ test('accept negated cpu with multi-valued supportedArchitectures', () => {
})
test('reject negated os when any supported value matches the negation', () => {
setPlatform('darwin')
const err = checkPlatform(packageId, { cpu: 'any', os: ['!win32'], libc: 'any' }, {
os: ['win32', 'current'],
cpu: ['current'],