Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23f9275

Browse filesBrowse files
joyeecheungaduh95
authored andcommitted
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> PR-URL: #62584 Refs: #61898 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 4739c45 commit 23f9275
Copy full SHA for 23f9275

7 files changed

+34-50Lines changed: 34 additions & 50 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.value = 42;
Collapse file
+5Lines 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+
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/npm-install/home/.gitkeep
Whitespace-only changes.
Collapse file
+5Lines 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+
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/npm-install/npm-prefix/.gitkeep
Whitespace-only changes.
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/npm-install/npm-tmp/.gitkeep
Whitespace-only changes.
Collapse file

‎test/parallel/test-npm-install.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-npm-install.js
+23-50Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.