Skip to content

Commit 868b441

Browse files
jasnellMylesBorins
authored andcommitted
test: begin normalizing fixtures use
Adds a new `../common/fixtures' module to begin normalizing `test/fixtures` use. Our test code is a bit inconsistent with regards to use of the fixtures directory. Some code uses `path.join()`, some code uses string concats, some other code uses template strings, etc. In mnay cases, significant duplication of code is seen when accessing fixture files, etc. This updates many (but by no means all) of the tests in the test suite to use the new consistent API. There are still many more to update, which would make an excelent Code-n-Learn exercise. PR-URL: #14332 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent c76ec71 commit 868b441

File tree

125 files changed

+497
-508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+497
-508
lines changed

test/common/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,37 @@ Decrements the `Countdown` counter.
374374
Specifies the remaining number of times `Countdown.prototype.dec()` must be
375375
called before the callback is invoked.
376376

377+
## Fixtures Module
378+
379+
The `common/fixtures` module provides convenience methods for working with
380+
files in the `test/fixtures` directory.
381+
382+
### fixtures.fixturesDir
383+
384+
* [&lt;String>]
385+
386+
The absolute path to the `test/fixtures/` directory.
387+
388+
### fixtures.path(...args)
389+
390+
* `...args` [&lt;String>]
391+
392+
Returns the result of `path.join(fixtures.fixturesDir, ...args)`.
393+
394+
### fixtures.readSync(args[, enc])
395+
396+
* `args` [&lt;String>] | [&lt;Array>]
397+
398+
Returns the result of
399+
`fs.readFileSync(path.join(fixtures.fixturesDir, ...args), 'enc')`.
400+
401+
### fixtures.readKey(arg[, enc])
402+
403+
* `arg` [&lt;String>]
404+
405+
Returns the result of
406+
`fs.readFileSync(path.join(fixtures.fixturesDir, 'keys', arg), 'enc')`.
407+
377408
## WPT Module
378409

379410
The wpt.js module is a port of parts of

test/common/fixtures.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable required-modules */
2+
'use strict';
3+
4+
const path = require('path');
5+
const fs = require('fs');
6+
7+
const fixturesDir = path.join(__dirname, '..', 'fixtures');
8+
9+
function fixturesPath(...args) {
10+
return path.join(fixturesDir, ...args);
11+
}
12+
13+
function readFixtureSync(args, enc) {
14+
if (Array.isArray(args))
15+
return fs.readFileSync(fixturesPath(...args), enc);
16+
return fs.readFileSync(fixturesPath(args), enc);
17+
}
18+
19+
function readFixtureKey(name, enc) {
20+
return fs.readFileSync(fixturesPath('keys', name), enc);
21+
}
22+
23+
module.exports = {
24+
fixturesDir,
25+
path: fixturesPath,
26+
readSync: readFixtureSync,
27+
readKey: readFixtureKey
28+
};

test/common/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const { exec, execSync, spawn, spawnSync } = require('child_process');
2929
const stream = require('stream');
3030
const util = require('util');
3131
const Timer = process.binding('timer_wrap').Timer;
32+
const { fixturesDir } = require('./fixtures');
3233

3334
const testRoot = process.env.NODE_TEST_DIR ?
3435
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
3536

3637
const noop = () => {};
3738

38-
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
39+
exports.fixturesDir = fixturesDir;
40+
3941
exports.tmpDirName = 'tmp';
4042
// PORT should match the definition in test/testpy/__init__.py.
4143
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;

test/parallel/test-async-wrap-GH13045.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ if (!common.hasCrypto)
88

99
const assert = require('assert');
1010
const https = require('https');
11-
const fs = require('fs');
11+
const fixtures = require('../common/fixtures');
1212

1313
const serverOptions = {
14-
key: fs.readFileSync(`${common.fixturesDir}/keys/agent1-key.pem`),
15-
cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`),
16-
ca: fs.readFileSync(`${common.fixturesDir}/keys/ca1-cert.pem`)
14+
key: fixtures.readKey('agent1-key.pem'),
15+
cert: fixtures.readKey('agent1-cert.pem'),
16+
ca: fixtures.readKey('ca1-cert.pem')
1717
};
1818

1919
const server = https.createServer(serverOptions, common.mustCall((req, res) => {

test/parallel/test-async-wrap-getasyncid.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = require('assert');
55
const fs = require('fs');
66
const net = require('net');
77
const providers = Object.assign({}, process.binding('async_wrap').Providers);
8+
const fixtures = require('../common/fixtures');
89

910
// Make sure that all Providers are tested.
1011
{
@@ -218,9 +219,10 @@ if (common.hasCrypto) {
218219
const TCP = process.binding('tcp_wrap').TCP;
219220
const tcp = new TCP();
220221

221-
const ca = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
222-
const cert = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
223-
const key = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
222+
const ca = fixtures.readSync('test_ca.pem', 'ascii');
223+
const cert = fixtures.readSync('test_cert.pem', 'ascii');
224+
const key = fixtures.readSync('test_key.pem', 'ascii');
225+
224226
const credentials = require('tls').createSecureContext({ ca, cert, key });
225227

226228
// TLSWrap is exposed, but needs to be instantiated via tls_wrap.wrap().

test/parallel/test-child-process-detached.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const assert = require('assert');
25-
const path = require('path');
25+
const fixtures = require('../common/fixtures');
2626

2727
const spawn = require('child_process').spawn;
28-
const childPath = path.join(common.fixturesDir,
29-
'parent-process-nonpersistent.js');
28+
const childPath = fixtures.path('parent-process-nonpersistent.js');
3029
let persistentPid = -1;
3130

3231
const child = spawn(process.execPath, [ childPath ]);

test/parallel/test-child-process-execfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const execFile = require('child_process').execFile;
5-
const path = require('path');
65
const uv = process.binding('uv');
6+
const fixtures = require('../common/fixtures');
77

8-
const fixture = path.join(common.fixturesDir, 'exit.js');
8+
const fixture = fixtures.path('exit.js');
99

1010
{
1111
execFile(

test/parallel/test-child-process-exit-code.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525
const spawn = require('child_process').spawn;
26-
const path = require('path');
26+
const fixtures = require('../common/fixtures');
2727

28-
const exitScript = path.join(common.fixturesDir, 'exit.js');
28+
const exitScript = fixtures.path('exit.js');
2929
const exitChild = spawn(process.argv[0], [exitScript, 23]);
3030
exitChild.on('exit', common.mustCall(function(code, signal) {
3131
assert.strictEqual(code, 23);
3232
assert.strictEqual(signal, null);
3333
}));
3434

3535

36-
const errorScript = path.join(common.fixturesDir,
37-
'child_process_should_emit_error.js');
36+
const errorScript = fixtures.path('child_process_should_emit_error.js');
3837
const errorChild = spawn(process.argv[0], [errorScript]);
3938
errorChild.on('exit', common.mustCall(function(code, signal) {
4039
assert.ok(code !== 0);

test/parallel/test-child-process-fork-close.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525
const fork = require('child_process').fork;
26+
const fixtures = require('../common/fixtures');
2627

27-
const cp = fork(`${common.fixturesDir}/child-process-message-and-exit.js`);
28+
const cp = fork(fixtures.path('child-process-message-and-exit.js'));
2829

2930
let gotMessage = false;
3031
let gotExit = false;

test/parallel/test-child-process-fork-stdio-string-variant.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const common = require('../common');
77

88
const assert = require('assert');
99
const fork = require('child_process').fork;
10+
const fixtures = require('../common/fixtures');
1011

11-
const childScript = `${common.fixturesDir}/child-process-spawn-node`;
12+
const childScript = fixtures.path('child-process-spawn-node');
1213
const errorRegexp = /^TypeError: Incorrect value of stdio option:/;
1314
const malFormedOpts = {stdio: '33'};
1415
const payload = {hello: 'world'};

test/parallel/test-child-process-fork.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const common = require('../common');
2424
const assert = require('assert');
2525
const fork = require('child_process').fork;
2626
const args = ['foo', 'bar'];
27+
const fixtures = require('../common/fixtures');
2728

28-
const n = fork(`${common.fixturesDir}/child-process-spawn-node.js`, args);
29+
const n = fork(fixtures.path('child-process-spawn-node.js'), args);
2930

3031
assert.strictEqual(n.channel, n._channel);
3132
assert.deepStrictEqual(args, ['foo', 'bar']);

test/parallel/test-child-process-fork3.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const child_process = require('child_process');
25+
const fixtures = require('../common/fixtures');
2526

26-
child_process.fork(`${common.fixturesDir}/empty.js`); // should not hang
27+
child_process.fork(fixtures.path('empty.js')); // should not hang

test/parallel/test-child-process-ipc.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121

2222
'use strict';
2323

24-
const common = require('../common');
24+
require('../common');
2525
const assert = require('assert');
2626

27-
const spawn = require('child_process').spawn;
27+
const { spawn } = require('child_process');
28+
const fixtures = require('../common/fixtures');
2829

29-
const path = require('path');
30-
31-
const sub = path.join(common.fixturesDir, 'echo.js');
30+
const sub = fixtures.path('echo.js');
3231

3332
let gotHelloWorld = false;
3433
let gotEcho = false;

test/parallel/test-child-process-send-after-close.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const cp = require('child_process');
5-
const path = require('path');
6-
const fixture = path.join(common.fixturesDir, 'empty.js');
5+
const fixtures = require('../common/fixtures');
6+
7+
const fixture = fixtures.path('empty.js');
78
const child = cp.fork(fixture);
89

910
child.on('close', common.mustCall((code, signal) => {

test/parallel/test-child-process-send-returns-boolean.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const path = require('path');
54
const net = require('net');
65
const { fork, spawn } = require('child_process');
6+
const fixtures = require('../common/fixtures');
77

8-
const emptyFile = path.join(common.fixturesDir, 'empty.js');
8+
const emptyFile = fixtures.path('empty.js');
99

1010
const n = fork(emptyFile);
1111

test/parallel/test-child-process-spawn-typeerror.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const child_process = require('child_process');
26-
const spawn = child_process.spawn;
27-
const fork = child_process.fork;
28-
const execFile = child_process.execFile;
25+
const { spawn, fork, execFile } = require('child_process');
26+
const fixtures = require('../common/fixtures');
2927
const cmd = common.isWindows ? 'rundll32' : 'ls';
3028
const invalidcmd = 'hopefully_you_dont_have_this_on_your_machine';
3129
const invalidArgsMsg = /Incorrect value of args option/;
3230
const invalidOptionsMsg = /"options" argument must be an object/;
3331
const invalidFileMsg =
3432
/^TypeError: "file" argument must be a non-empty string$/;
35-
const empty = `${common.fixturesDir}/empty.js`;
33+
34+
const empty = fixtures.path('empty.js');
3635

3736
assert.throws(function() {
3837
const child = spawn(invalidcmd, 'this is not an array');

test/parallel/test-child-process-stdout-flush.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const path = require('path');
2625
const spawn = require('child_process').spawn;
27-
const sub = path.join(common.fixturesDir, 'print-chars.js');
26+
const fixtures = require('../common/fixtures');
27+
28+
const sub = fixtures.path('print-chars.js');
2829

2930
const n = 500000;
3031

test/parallel/test-cli-eval.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const common = require('../common');
3131
const assert = require('assert');
3232
const child = require('child_process');
3333
const path = require('path');
34+
const fixtures = require('../common/fixtures');
3435
const nodejs = `"${process.execPath}"`;
3536

3637
if (process.argv.length > 2) {
@@ -138,7 +139,7 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
138139

139140
// Regression test for https://github.com/nodejs/node/issues/3574.
140141
{
141-
const emptyFile = path.join(common.fixturesDir, 'empty.js');
142+
const emptyFile = fixtures.path('empty.js');
142143

143144
child.exec(`${nodejs} -e 'require("child_process").fork("${emptyFile}")'`,
144145
common.mustCall((err, stdout, stderr) => {

test/parallel/test-cli-syntax.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const common = require('../common');
44
const assert = require('assert');
5-
const {exec, spawnSync} = require('child_process');
6-
const path = require('path');
5+
const { exec, spawnSync } = require('child_process');
6+
const fixtures = require('../common/fixtures');
77

88
const node = process.execPath;
99

@@ -24,7 +24,7 @@ const notFoundRE = /^Error: Cannot find module/m;
2424
'syntax/good_syntax_shebang',
2525
'syntax/illegal_if_not_wrapped.js'
2626
].forEach(function(file) {
27-
file = path.join(common.fixturesDir, file);
27+
file = fixtures.path(file);
2828

2929
// loop each possible option, `-c` or `--check`
3030
syntaxArgs.forEach(function(args) {
@@ -46,7 +46,7 @@ const notFoundRE = /^Error: Cannot find module/m;
4646
'syntax/bad_syntax_shebang.js',
4747
'syntax/bad_syntax_shebang'
4848
].forEach(function(file) {
49-
file = path.join(common.fixturesDir, file);
49+
file = fixtures.path(file);
5050

5151
// loop each possible option, `-c` or `--check`
5252
syntaxArgs.forEach(function(args) {
@@ -73,7 +73,7 @@ const notFoundRE = /^Error: Cannot find module/m;
7373
'syntax/file_not_found.js',
7474
'syntax/file_not_found'
7575
].forEach(function(file) {
76-
file = path.join(common.fixturesDir, file);
76+
file = fixtures.path(file);
7777

7878
// loop each possible option, `-c` or `--check`
7979
syntaxArgs.forEach(function(args) {

0 commit comments

Comments
 (0)