diff --git a/config/package-is-installable/test/checkPlatform.ts b/config/package-is-installable/test/checkPlatform.ts index 242b04a49e..996ca628d4 100644 --- a/config/package-is-installable/test/checkPlatform.ts +++ b/config/package-is-installable/test/checkPlatform.ts @@ -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'],