Skip to content

Commit a721d23

Browse files
jasnellrefack
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: nodejs#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 3996e8a commit a721d23

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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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) {
Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 5 deletions
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;

0 commit comments

Comments
 (0)