Skip to content

Commit 715306a

Browse files
committed
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 <joyeec9h3@gmail.com>
1 parent bf1aebc commit 715306a

File tree

7 files changed

+34
-50
lines changed

7 files changed

+34
-50
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.value = 42;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"main": "example.js"
5+
}

test/fixtures/npm-install/home/.gitkeep

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"example": "../example"
4+
}
5+
}

test/fixtures/npm-install/npm-prefix/.gitkeep

Whitespace-only changes.

test/fixtures/npm-install/npm-tmp/.gitkeep

Whitespace-only changes.

test/parallel/test-npm-install.js

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,37 @@ if (common.isInsideDirWithUnusualChars)
66
common.skip('npm does not support this install path');
77

88
const path = require('path');
9-
const exec = require('child_process').exec;
109
const assert = require('assert');
1110
const fs = require('fs');
1211
const fixtures = require('../common/fixtures');
12+
const { spawnSyncAndAssert } = require('../common/child_process');
1313

1414
const tmpdir = require('../common/tmpdir');
1515
tmpdir.refresh();
16-
const npmSandbox = tmpdir.resolve('npm-sandbox');
17-
fs.mkdirSync(npmSandbox);
18-
const homeDir = tmpdir.resolve('home');
19-
fs.mkdirSync(homeDir);
20-
const installDir = tmpdir.resolve('install-dir');
21-
fs.mkdirSync(installDir);
22-
23-
const npmPath = path.join(
24-
__dirname,
25-
'..',
26-
'..',
27-
'deps',
28-
'npm',
29-
'bin',
30-
'npm-cli.js'
31-
);
32-
33-
const pkgContent = JSON.stringify({
34-
dependencies: {
35-
'package-name': fixtures.path('packages/main')
36-
}
37-
});
3816

39-
const pkgPath = path.join(installDir, 'package.json');
17+
// Copy fixtures/npm-install to the tmpdir for testing
18+
fs.cpSync(fixtures.path('npm-install'), tmpdir.path, { recursive: true });
4019

41-
fs.writeFileSync(pkgPath, pkgContent);
20+
const npmPath = path.join(__dirname, '..', '..', 'deps', 'npm', 'bin', 'npm-cli.js');
4221

43-
const env = { ...process.env,
44-
PATH: path.dirname(process.execPath),
45-
NODE: process.execPath,
46-
NPM: npmPath,
47-
NPM_CONFIG_PREFIX: path.join(npmSandbox, 'npm-prefix'),
48-
NPM_CONFIG_TMP: path.join(npmSandbox, 'npm-tmp'),
49-
NPM_CONFIG_AUDIT: false,
50-
NPM_CONFIG_UPDATE_NOTIFIER: false,
51-
HOME: homeDir };
22+
const env = {
23+
...process.env,
24+
PATH: path.dirname(process.execPath),
25+
NODE: process.execPath,
26+
NPM: npmPath,
27+
NPM_CONFIG_PREFIX: tmpdir.resolve('npm-prefix'),
28+
NPM_CONFIG_TMP: tmpdir.resolve('npm-tmp'),
29+
NPM_CONFIG_AUDIT: false,
30+
NPM_CONFIG_UPDATE_NOTIFIER: false,
31+
HOME: tmpdir.resolve('home'),
32+
};
5233

53-
exec(`"${common.isWindows ? process.execPath : '$NODE'}" "${common.isWindows ? npmPath : '$NPM'}" install`, {
54-
cwd: installDir,
55-
env: env
56-
}, common.mustCall(handleExit));
57-
58-
function handleExit(error, stdout, stderr) {
59-
const code = error ? error.code : 0;
60-
const signalCode = error ? error.signal : null;
61-
62-
if (code !== 0) {
63-
process.stderr.write(stderr);
64-
}
34+
const installDir = tmpdir.resolve('install-dir');
35+
spawnSyncAndAssert(
36+
process.execPath,
37+
[npmPath, 'install'],
38+
{ cwd: installDir, env },
39+
{}
40+
);
6541

66-
assert.strictEqual(code, 0, `npm install got error code ${code}`);
67-
assert.strictEqual(signalCode, null, `unexpected signal: ${signalCode}`);
68-
assert(fs.existsSync(`${installDir}/node_modules/package-name`));
69-
}
42+
assert(fs.existsSync(path.join(installDir, 'node_modules', 'example')));

0 commit comments

Comments
 (0)