-
Notifications
You must be signed in to change notification settings - Fork 29.6k
/
test-npm-install.js
67 lines (56 loc) · 1.82 KB
/
test-npm-install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const path = require('path');
const exec = require('child_process').exec;
const assert = require('assert');
const fs = require('fs');
const fixtures = require('../common/fixtures');
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');
fs.writeFileSync(pkgPath, pkgContent);
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 };
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);
}
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`));
}