|
2 | 2 | const common = require('../common'); |
3 | 3 | const assert = require('assert'); |
4 | 4 | const child = require('child_process'); |
5 | | -const util = require('util'); |
| 5 | +const path = require('path'); |
| 6 | + |
6 | 7 | if (process.env['TEST_INIT']) { |
7 | | - util.print('Loaded successfully!'); |
8 | | -} else { |
9 | | - // change CWD as we do this test so its not dependant on current CWD |
10 | | - // being in the test folder |
11 | | - process.chdir(__dirname); |
| 8 | + return process.stdout.write('Loaded successfully!'); |
| 9 | +} |
| 10 | + |
| 11 | +process.env.TEST_INIT = 1; |
12 | 12 |
|
13 | | - // slow but simple |
14 | | - var envCopy = JSON.parse(JSON.stringify(process.env)); |
15 | | - envCopy.TEST_INIT = 1; |
| 13 | +function test(file, expected) { |
| 14 | + const path = `"${process.execPath}" ${file}`; |
| 15 | + child.exec(path, {env: process.env}, common.mustCall((err, out) => { |
| 16 | + assert.ifError(err); |
| 17 | + assert.strictEqual(out, expected, `'node ${file}' failed!`); |
| 18 | + })); |
| 19 | +} |
16 | 20 |
|
17 | | - child.exec('"' + process.execPath + '" test-init', {env: envCopy}, |
18 | | - function(err, stdout, stderr) { |
19 | | - assert.equal(stdout, 'Loaded successfully!', |
20 | | - '`node test-init` failed!'); |
21 | | - }); |
22 | | - child.exec('"' + process.execPath + '" test-init.js', {env: envCopy}, |
23 | | - function(err, stdout, stderr) { |
24 | | - assert.equal(stdout, 'Loaded successfully!', |
25 | | - '`node test-init.js` failed!'); |
26 | | - }); |
| 21 | +{ |
| 22 | + // change CWD as we do this test so it's not dependent on current CWD |
| 23 | + // being in the test folder |
| 24 | + process.chdir(__dirname); |
| 25 | + test('test-init', 'Loaded successfully!'); |
| 26 | + test('test-init.js', 'Loaded successfully!'); |
| 27 | +} |
27 | 28 |
|
| 29 | +{ |
28 | 30 | // test-init-index is in fixtures dir as requested by ry, so go there |
29 | 31 | process.chdir(common.fixturesDir); |
| 32 | + test('test-init-index', 'Loaded successfully!'); |
| 33 | +} |
30 | 34 |
|
31 | | - child.exec('"' + process.execPath + '" test-init-index', {env: envCopy}, |
32 | | - function(err, stdout, stderr) { |
33 | | - assert.equal(stdout, 'Loaded successfully!', |
34 | | - '`node test-init-index failed!'); |
35 | | - }); |
36 | | - |
| 35 | +{ |
37 | 36 | // ensures that `node fs` does not mistakenly load the native 'fs' module |
38 | 37 | // instead of the desired file and that the fs module loads as |
39 | 38 | // expected in node |
40 | | - process.chdir(common.fixturesDir + '/test-init-native/'); |
41 | | - |
42 | | - child.exec('"' + process.execPath + '" fs', {env: envCopy}, |
43 | | - function(err, stdout, stderr) { |
44 | | - assert.equal(stdout, 'fs loaded successfully', |
45 | | - '`node fs` failed!'); |
46 | | - }); |
| 39 | + process.chdir(path.join(common.fixturesDir, 'test-init-native')); |
| 40 | + test('fs', 'fs loaded successfully'); |
47 | 41 | } |
0 commit comments