From 715306a9605e6c91f7cf2bdd736b6089ae0752ae Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 4 Apr 2026 12:02:32 +0200 Subject: [PATCH] test: use on-disk fixture for test-npm-install Instead of writing the fixtures on the fly in the test, put them in the fixtures directory that can be copied into a temporary directory to reproduce in a debugger. Signed-off-by: Joyee Cheung --- test/fixtures/npm-install/example/example.js | 1 + .../fixtures/npm-install/example/package.json | 5 ++ test/fixtures/npm-install/home/.gitkeep | 0 .../npm-install/install-dir/package.json | 5 ++ test/fixtures/npm-install/npm-prefix/.gitkeep | 0 test/fixtures/npm-install/npm-tmp/.gitkeep | 0 test/parallel/test-npm-install.js | 73 ++++++------------- 7 files changed, 34 insertions(+), 50 deletions(-) create mode 100644 test/fixtures/npm-install/example/example.js create mode 100644 test/fixtures/npm-install/example/package.json create mode 100644 test/fixtures/npm-install/home/.gitkeep create mode 100644 test/fixtures/npm-install/install-dir/package.json create mode 100644 test/fixtures/npm-install/npm-prefix/.gitkeep create mode 100644 test/fixtures/npm-install/npm-tmp/.gitkeep diff --git a/test/fixtures/npm-install/example/example.js b/test/fixtures/npm-install/example/example.js new file mode 100644 index 00000000000000..f16abdc55b3f4c --- /dev/null +++ b/test/fixtures/npm-install/example/example.js @@ -0,0 +1 @@ +exports.value = 42; diff --git a/test/fixtures/npm-install/example/package.json b/test/fixtures/npm-install/example/package.json new file mode 100644 index 00000000000000..b0777f479f5b60 --- /dev/null +++ b/test/fixtures/npm-install/example/package.json @@ -0,0 +1,5 @@ +{ + "name": "example", + "version": "1.0.0", + "main": "example.js" +} diff --git a/test/fixtures/npm-install/home/.gitkeep b/test/fixtures/npm-install/home/.gitkeep new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/fixtures/npm-install/install-dir/package.json b/test/fixtures/npm-install/install-dir/package.json new file mode 100644 index 00000000000000..02ea3ae090f094 --- /dev/null +++ b/test/fixtures/npm-install/install-dir/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "example": "../example" + } +} diff --git a/test/fixtures/npm-install/npm-prefix/.gitkeep b/test/fixtures/npm-install/npm-prefix/.gitkeep new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/fixtures/npm-install/npm-tmp/.gitkeep b/test/fixtures/npm-install/npm-tmp/.gitkeep new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index fe9dbd46d9a7e0..0b295522a80cdf 100644 --- a/test/parallel/test-npm-install.js +++ b/test/parallel/test-npm-install.js @@ -6,64 +6,37 @@ if (common.isInsideDirWithUnusualChars) common.skip('npm does not support this install path'); const path = require('path'); -const exec = require('child_process').exec; const assert = require('assert'); const fs = require('fs'); const fixtures = require('../common/fixtures'); +const { spawnSyncAndAssert } = require('../common/child_process'); const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); -const npmSandbox = tmpdir.resolve('npm-sandbox'); -fs.mkdirSync(npmSandbox); -const homeDir = tmpdir.resolve('home'); -fs.mkdirSync(homeDir); -const installDir = tmpdir.resolve('install-dir'); -fs.mkdirSync(installDir); - -const npmPath = path.join( - __dirname, - '..', - '..', - 'deps', - 'npm', - 'bin', - 'npm-cli.js' -); - -const pkgContent = JSON.stringify({ - dependencies: { - 'package-name': fixtures.path('packages/main') - } -}); -const pkgPath = path.join(installDir, 'package.json'); +// Copy fixtures/npm-install to the tmpdir for testing +fs.cpSync(fixtures.path('npm-install'), tmpdir.path, { recursive: true }); -fs.writeFileSync(pkgPath, pkgContent); +const npmPath = path.join(__dirname, '..', '..', 'deps', 'npm', 'bin', 'npm-cli.js'); -const env = { ...process.env, - PATH: path.dirname(process.execPath), - NODE: process.execPath, - NPM: npmPath, - NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'), - NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'), - NPM_CONFIG_AUDIT: false, - NPM_CONFIG_UPDATE_NOTIFIER: false, - HOME: homeDir }; +const env = { + ...process.env, + PATH: path.dirname(process.execPath), + NODE: process.execPath, + NPM: npmPath, + NPM_CONFIG_PREFIX: tmpdir.resolve('npm-prefix'), + NPM_CONFIG_TMP: tmpdir.resolve('npm-tmp'), + NPM_CONFIG_AUDIT: false, + NPM_CONFIG_UPDATE_NOTIFIER: false, + HOME: tmpdir.resolve('home'), +}; -exec(`"${common.isWindows ? process.execPath : '$NODE'}" "${common.isWindows ? npmPath : '$NPM'}" install`, { - cwd: installDir, - env: env -}, common.mustCall(handleExit)); - -function handleExit(error, stdout, stderr) { - const code = error ? error.code : 0; - const signalCode = error ? error.signal : null; - - if (code !== 0) { - process.stderr.write(stderr); - } +const installDir = tmpdir.resolve('install-dir'); +spawnSyncAndAssert( + process.execPath, + [npmPath, 'install'], + { cwd: installDir, env }, + {} +); - assert.strictEqual(code, 0, `npm install got error code ${code}`); - assert.strictEqual(signalCode, null, `unexpected signal: ${signalCode}`); - assert(fs.existsSync(`${installDir}/node_modules/package-name`)); -} +assert(fs.existsSync(path.join(installDir, 'node_modules', 'example')));