Skip to content

Commit db37c88

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. Backport-PR-URL: #16265 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 1d94a86 commit db37c88

File tree

113 files changed

+465
-481
lines changed

Some content is hidden

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

113 files changed

+465
-481
lines changed

test/common/README.md

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

319+
## Fixtures Module
320+
321+
The `common/fixtures` module provides convenience methods for working with
322+
files in the `test/fixtures` directory.
323+
324+
### fixtures.fixturesDir
325+
326+
* [&lt;String>]
327+
328+
The absolute path to the `test/fixtures/` directory.
329+
330+
### fixtures.path(...args)
331+
332+
* `...args` [&lt;String>]
333+
334+
Returns the result of `path.join(fixtures.fixturesDir, ...args)`.
335+
336+
### fixtures.readSync(args[, enc])
337+
338+
* `args` [&lt;String>] | [&lt;Array>]
339+
340+
Returns the result of
341+
`fs.readFileSync(path.join(fixtures.fixturesDir, ...args), 'enc')`.
342+
343+
### fixtures.readKey(arg[, enc])
344+
345+
* `arg` [&lt;String>]
346+
347+
Returns the result of
348+
`fs.readFileSync(path.join(fixtures.fixturesDir, 'keys', arg), 'enc')`.
349+
319350
## WPT Module
320351

321352
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
@@ -8,13 +8,15 @@ const { exec, execSync, spawn, spawnSync } = require('child_process');
88
const stream = require('stream');
99
const util = require('util');
1010
const Timer = process.binding('timer_wrap').Timer;
11+
const { fixturesDir } = require('./fixtures');
1112

1213
const testRoot = process.env.NODE_TEST_DIR ?
1314
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
1415

1516
const noop = () => {};
1617

17-
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
18+
exports.fixturesDir = fixturesDir;
19+
1820
exports.tmpDirName = 'tmp';
1921
// PORT should match the definition in test/testpy/__init__.py.
2022
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const assert = require('assert');
4-
const path = require('path');
4+
const fixtures = require('../common/fixtures');
55

66
const spawn = require('child_process').spawn;
7-
const childPath = path.join(common.fixturesDir,
8-
'parent-process-nonpersistent.js');
7+
const childPath = fixtures.path('parent-process-nonpersistent.js');
98
let persistentPid = -1;
109

1110
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
@@ -2,18 +2,17 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const spawn = require('child_process').spawn;
5-
const path = require('path');
5+
const fixtures = require('../common/fixtures');
66

7-
const exitScript = path.join(common.fixturesDir, 'exit.js');
7+
const exitScript = fixtures.path('exit.js');
88
const exitChild = spawn(process.argv[0], [exitScript, 23]);
99
exitChild.on('exit', common.mustCall(function(code, signal) {
1010
assert.strictEqual(code, 23);
1111
assert.strictEqual(signal, null);
1212
}));
1313

1414

15-
const errorScript = path.join(common.fixturesDir,
16-
'child_process_should_emit_error.js');
15+
const errorScript = fixtures.path('child_process_should_emit_error.js');
1716
const errorChild = spawn(process.argv[0], [errorScript]);
1817
errorChild.on('exit', common.mustCall(function(code, signal) {
1918
assert.ok(code !== 0);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const fork = require('child_process').fork;
5+
const fixtures = require('../common/fixtures');
56

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

89
let gotMessage = false;
910
let gotExit = false;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ const common = require('../common');
33
const assert = require('assert');
44
const fork = require('child_process').fork;
55
const args = ['foo', 'bar'];
6+
const fixtures = require('../common/fixtures');
67

7-
const n = fork(`${common.fixturesDir}/child-process-spawn-node.js`, args);
8+
const n = fork(fixtures.path('child-process-spawn-node.js'), args);
89
assert.deepStrictEqual(args, ['foo', 'bar']);
910

1011
n.on('message', function(m) {
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const child_process = require('child_process');
4+
const fixtures = require('../common/fixtures');
45

5-
child_process.fork(`${common.fixturesDir}/empty.js`); // should not hang
6+
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
@@ -1,13 +1,12 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55

6-
const spawn = require('child_process').spawn;
6+
const { spawn } = require('child_process');
7+
const fixtures = require('../common/fixtures');
78

8-
const path = require('path');
9-
10-
const sub = path.join(common.fixturesDir, 'echo.js');
9+
const sub = fixtures.path('echo.js');
1110

1211
let gotHelloWorld = false;
1312
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
@@ -1,15 +1,14 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const child_process = require('child_process');
5-
const spawn = child_process.spawn;
6-
const fork = child_process.fork;
7-
const execFile = child_process.execFile;
4+
const { spawn, fork, execFile } = require('child_process');
5+
const fixtures = require('../common/fixtures');
86
const cmd = common.isWindows ? 'rundll32' : 'ls';
97
const invalidcmd = 'hopefully_you_dont_have_this_on_your_machine';
108
const invalidArgsMsg = /Incorrect value of args option/;
119
const invalidOptionsMsg = /"options" argument must be an object/;
12-
const empty = `${common.fixturesDir}/empty.js`;
10+
11+
const empty = fixtures.path('empty.js');
1312

1413
assert.throws(function() {
1514
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
@@ -1,9 +1,10 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const path = require('path');
54
const spawn = require('child_process').spawn;
6-
const sub = path.join(common.fixturesDir, 'print-chars.js');
5+
const fixtures = require('../common/fixtures');
6+
7+
const sub = fixtures.path('print-chars.js');
78

89
const n = 500000;
910

test/parallel/test-cli-eval.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const common = require('../common');
1010
const assert = require('assert');
1111
const child = require('child_process');
1212
const path = require('path');
13+
const fixtures = require('../common/fixtures');
1314
const nodejs = `"${process.execPath}"`;
1415

1516
if (process.argv.length > 2) {
@@ -117,7 +118,7 @@ child.exec(`${nodejs} --use-strict -p process.execArgv`,
117118

118119
// Regression test for https://github.com/nodejs/node/issues/3574.
119120
{
120-
const emptyFile = path.join(common.fixturesDir, 'empty.js');
121+
const emptyFile = fixtures.path('empty.js');
121122

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

test/parallel/test-cli-syntax.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55
const spawnSync = require('child_process').spawnSync;
6-
const path = require('path');
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) {
@@ -45,7 +45,7 @@ const notFoundRE = /^Error: Cannot find module/m;
4545
'syntax/bad_syntax_shebang.js',
4646
'syntax/bad_syntax_shebang'
4747
].forEach(function(file) {
48-
file = path.join(common.fixturesDir, file);
48+
file = fixtures.path(file);
4949

5050
// loop each possible option, `-c` or `--check`
5151
syntaxArgs.forEach(function(args) {
@@ -67,7 +67,7 @@ const notFoundRE = /^Error: Cannot find module/m;
6767
'syntax/file_not_found.js',
6868
'syntax/file_not_found'
6969
].forEach(function(file) {
70-
file = path.join(common.fixturesDir, file);
70+
file = fixtures.path(file);
7171

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

test/parallel/test-crypto-binary-default.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ if (!common.hasCrypto)
1111
const assert = require('assert');
1212
const crypto = require('crypto');
1313
const fs = require('fs');
14-
const path = require('path');
1514
const tls = require('tls');
15+
const fixtures = require('../common/fixtures');
1616
const DH_NOT_SUITABLE_GENERATOR = crypto.constants.DH_NOT_SUITABLE_GENERATOR;
17-
const fixtDir = common.fixturesDir;
1817

1918
crypto.DEFAULT_ENCODING = 'latin1';
2019

2120
// Test Certificates
22-
const certPem = fs.readFileSync(`${fixtDir}/test_cert.pem`, 'ascii');
23-
const certPfx = fs.readFileSync(`${fixtDir}/test_cert.pfx`);
24-
const keyPem = fs.readFileSync(`${fixtDir}/test_key.pem`, 'ascii');
25-
const rsaPubPem = fs.readFileSync(`${fixtDir}/test_rsa_pubkey.pem`, 'ascii');
26-
const rsaKeyPem = fs.readFileSync(`${fixtDir}/test_rsa_privkey.pem`, 'ascii');
21+
const certPem = fixtures.readSync('test_cert.pem', 'ascii');
22+
const certPfx = fixtures.readSync('test_cert.pfx');
23+
const keyPem = fixtures.readSync('test_key.pem', 'ascii');
24+
const rsaPubPem = fixtures.readSync('test_rsa_pubkey.pem', 'ascii');
25+
const rsaKeyPem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
2726

2827
// PFX tests
2928
assert.doesNotThrow(function() {
@@ -384,7 +383,7 @@ const h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex');
384383
assert.strictEqual(h1, h2, 'multipled updates');
385384

386385
// Test hashing for binary files
387-
const fn = path.join(fixtDir, 'sample.png');
386+
const fn = fixtures.path('sample.png');
388387
const sha1Hash = crypto.createHash('sha1');
389388
const fileStream = fs.createReadStream(fn);
390389
fileStream.on('data', function(data) {
@@ -593,9 +592,8 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
593592
// Test RSA signing and verification
594593
//
595594
{
596-
const privateKey = fs.readFileSync(`${fixtDir}/test_rsa_privkey_2.pem`);
597-
598-
const publicKey = fs.readFileSync(`${fixtDir}/test_rsa_pubkey_2.pem`);
595+
const privateKey = fixtures.readSync('test_rsa_privkey_2.pem');
596+
const publicKey = fixtures.readSync('test_rsa_pubkey_2.pem');
599597

600598
const input = 'I AM THE WALRUS';
601599

@@ -623,9 +621,8 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);
623621
// Test DSA signing and verification
624622
//
625623
{
626-
const privateKey = fs.readFileSync(`${fixtDir}/test_dsa_privkey.pem`);
627-
628-
const publicKey = fs.readFileSync(`${fixtDir}/test_dsa_pubkey.pem`);
624+
const privateKey = fixtures.readSync('test_dsa_privkey.pem');
625+
const publicKey = fixtures.readSync('test_dsa_pubkey.pem');
629626

630627
const input = 'I AM THE WALRUS';
631628

test/parallel/test-crypto-certificate.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ if (!common.hasCrypto)
55

66
const assert = require('assert');
77
const crypto = require('crypto');
8+
const fixtures = require('../common/fixtures');
89

910
crypto.DEFAULT_ENCODING = 'buffer';
1011

11-
const fs = require('fs');
12-
1312
// Test Certificates
14-
const spkacValid = fs.readFileSync(`${common.fixturesDir}/spkac.valid`);
15-
const spkacFail = fs.readFileSync(`${common.fixturesDir}/spkac.fail`);
16-
const spkacPem = fs.readFileSync(`${common.fixturesDir}/spkac.pem`);
13+
const spkacValid = fixtures.readSync('spkac.valid');
14+
const spkacFail = fixtures.readSync('spkac.fail');
15+
const spkacPem = fixtures.readSync('spkac.pem');
1716

1817
const certificate = new crypto.Certificate();
1918

test/parallel/test-crypto-fips.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const spawnSync = require('child_process').spawnSync;
88
const path = require('path');
9+
const fixtures = require('../common/fixtures');
910

1011
const FIPS_ENABLED = 1;
1112
const FIPS_DISABLED = 0;
1213
const FIPS_ERROR_STRING = 'Error: Cannot set FIPS mode';
1314
const OPTION_ERROR_STRING = 'bad option';
14-
const CNF_FIPS_ON = path.join(common.fixturesDir, 'openssl_fips_enabled.cnf');
15-
const CNF_FIPS_OFF = path.join(common.fixturesDir, 'openssl_fips_disabled.cnf');
15+
16+
const CNF_FIPS_ON = fixtures.path('openssl_fips_enabled.cnf');
17+
const CNF_FIPS_OFF = fixtures.path('openssl_fips_disabled.cnf');
18+
1619
let num_children_ok = 0;
1720

1821
function compiledWithFips() {

0 commit comments

Comments
 (0)