forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-npm-package.js
executable file
·139 lines (122 loc) · 3.86 KB
/
test-npm-package.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
/**
* Usage:
* test-npm-package.js [--install] [--rebuild] <source> <test-arg>+
*
* Everything after the <source> directory gets passed to `npm run` to build
* the test command.
*
* If `--install` is passed, we'll run a full `npm install` before running the
* test suite. Same for `--rebuild` and `npm rebuild`.
*
* We always use the node used to spawn this script and the `npm` version
* bundled in `deps/npm`.
*
* If an additional `--logfile=<filename>` option is passed before `<source>`,
* the stdout output of the test script will be written to that file.
*/
'use strict';
const { spawn, spawnSync } = require('child_process');
const { createHash } = require('crypto');
const { createWriteStream, mkdirSync, rmdirSync } = require('fs');
const path = require('path');
const common = require('../test/common');
const tmpDir = require('../test/common/tmpdir');
const projectDir = path.resolve(__dirname, '..');
const npmBin = path.join(projectDir, 'deps', 'npm', 'bin', 'npm-cli.js');
const nodePath = path.dirname(process.execPath);
function spawnCopyDeepSync(source, destination) {
if (common.isWindows) {
mkdirSync(destination); // Prevent interactive prompt
return spawnSync('xcopy.exe', ['/E', source, destination]);
}
return spawnSync('cp', ['-r', `${source}/`, destination]);
}
function runNPMPackageTests({ srcDir, install, rebuild, testArgs, logfile }) {
// Make sure we don't conflict with concurrent test runs
const srcHash = createHash('md5').update(srcDir).digest('hex');
tmpDir.path = `${tmpDir.path}.npm.${srcHash}`;
tmpDir.refresh();
const npmCache = path.join(tmpDir.path, 'npm-cache');
const npmPrefix = path.join(tmpDir.path, 'npm-prefix');
const npmTmp = path.join(tmpDir.path, 'npm-tmp');
const npmUserconfig = path.join(tmpDir.path, 'npm-userconfig');
const pkgDir = path.join(tmpDir.path, 'pkg');
spawnCopyDeepSync(srcDir, pkgDir);
const npmOptions = {
cwd: pkgDir,
env: Object.assign({}, process.env, {
'npm_config_cache': npmCache,
'npm_config_prefix': npmPrefix,
'npm_config_tmp': npmTmp,
'npm_config_userconfig': npmUserconfig,
}),
stdio: 'inherit',
};
if (common.isWindows) {
npmOptions.env.home = tmpDir.path;
npmOptions.env.Path = `${nodePath};${process.env.Path}`;
} else {
npmOptions.env.HOME = tmpDir.path;
npmOptions.env.PATH = `${nodePath}:${process.env.PATH}`;
}
if (rebuild) {
spawnSync(process.execPath, [
npmBin,
'rebuild',
], npmOptions);
}
if (install) {
spawnSync(process.execPath, [
npmBin,
'install',
'--ignore-scripts',
'--no-save',
], npmOptions);
}
const testChild = spawn(process.execPath, [
npmBin,
'--silent',
'run',
...testArgs,
], Object.assign({}, npmOptions, { stdio: 'pipe' }));
testChild.stdout.pipe(process.stdout);
testChild.stderr.pipe(process.stderr);
if (logfile) {
const logStream = createWriteStream(logfile);
testChild.stdout.pipe(logStream);
}
testChild.on('exit', () => {
tmpDir.refresh();
rmdirSync(tmpDir.path);
});
}
function parseArgs(args) {
let srcDir;
let rebuild = false;
let install = false;
let logfile = null;
const testArgs = [];
args.forEach((arg) => {
if (srcDir) {
testArgs.push(arg);
return;
}
if (arg === '--install') {
install = true;
} else if (arg === '--rebuild') {
rebuild = true;
} else if (arg[0] !== '-') {
srcDir = path.resolve(projectDir, arg);
} else if (arg.startsWith('--logfile=')) {
logfile = path.resolve(projectDir, arg.slice('--logfile='.length));
} else {
throw new Error(`Unrecognized option ${arg}`);
}
});
if (!srcDir) {
throw new Error('Expected a source directory');
}
return { srcDir, install, rebuild, testArgs, logfile };
}
runNPMPackageTests(parseArgs(process.argv.slice(2)));