From 04197512179c508abb55fa528d293ee669c19b91 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 7 Oct 2023 01:42:02 +0800 Subject: [PATCH] feat: allow checkPlatform to override execution libc (#71) - Part of https://github.com/npm/npm-install-checks/pull/65 --- README.md | 2 +- lib/index.js | 4 +++- test/check-platform.js | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 828b007..2dd8c7f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,6 @@ Check if a package's `os`, `cpu` and `libc` match the running system. `force` argument skips all checks. -`environment` overrides the execution environment which comes from `process.platform` and `process.arch` by default. `environment.os` and `environment.cpu` are available. +`environment` overrides the execution environment which comes from `process.platform` `process.arch` and current `libc` environment by default. `environment.os` `environment.cpu` and `environment.libc` are available. Error code: 'EBADPLATFORM' diff --git a/lib/index.js b/lib/index.js index f0ba2c0..545472b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,7 +36,9 @@ const checkPlatform = (target, force = false, environment = {}) => { let libcFamily = null if (target.libc) { // libc checks only work in linux, any value is a failure if we aren't - if (platform !== 'linux') { + if (environment.libc) { + libcOk = checkList(environment.libc, target.libc) + } else if (platform !== 'linux') { libcOk = false } else { const report = process.report.getReport() diff --git a/test/check-platform.js b/test/check-platform.js index a84b965..00ecf04 100644 --- a/test/check-platform.js +++ b/test/check-platform.js @@ -59,6 +59,15 @@ t.test('nothing wrong with overridden cpu', async t => cpu: 'enten-cpu', })) +t.test('nothing wrong with overridden libc', async t => + checkPlatform({ + cpu: 'enten-cpu', + libc: 'enten-libc', + }, false, { + cpu: 'enten-cpu', + libc: 'enten-libc', + })) + t.test('wrong os with overridden os', async t => t.throws(() => checkPlatform({ cpu: 'any', @@ -75,6 +84,15 @@ t.test('wrong cpu with overridden cpu', async t => cpu: 'another-cpu', }), { code: 'EBADPLATFORM' })) +t.test('wrong libc with overridden libc', async t => + t.throws(() => checkPlatform({ + cpu: 'enten-cpu', + os: 'any', + libc: 'enten-libc', + }, false, { + libc: 'another-libc', + }), { code: 'EBADPLATFORM' })) + t.test('libc', (t) => { let PLATFORM = ''