test: improve test-npm-install
[platform/upstream/nodejs.git] / test / parallel / test-npm-install.js
1 'use strict';
2 const common = require('../common');
3
4 const path = require('path');
5 const spawn = require('child_process').spawn;
6 const assert = require('assert');
7 const fs = require('fs');
8
9 common.refreshTmpDir();
10
11 const npmPath = path.join(
12   common.testDir,
13   '..',
14   'deps',
15   'npm',
16   'bin',
17   'npm-cli.js'
18 );
19
20 const args = [
21   npmPath,
22   'install'
23 ];
24
25 const pkgContent = JSON.stringify({
26   dependencies: {
27     'package-name': common.fixturesDir + '/packages/main'
28   }
29 });
30
31 const pkgPath = path.join(common.tmpDir, 'package.json');
32
33 fs.writeFileSync(pkgPath, pkgContent);
34
35 const proc = spawn(process.execPath, args, {
36   cwd: common.tmpDir
37 });
38
39 function handleExit(code, signalCode) {
40   assert.equal(code, 0, 'npm install should run without an error');
41   assert.ok(signalCode === null, 'signalCode should be null');
42   assert.doesNotThrow(function() {
43     fs.accessSync(common.tmpDir + '/node_modules/package-name');
44   });
45 }
46
47 proc.on('exit', common.mustCall(handleExit));