Skip to content

Commit 1fdbaed

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 88b9572 commit 1fdbaed

File tree

113 files changed

+466
-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

+466
-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+
constpath=require('path');
5+
constfs=require('fs');
6+
7+
constfixturesDir=path.join(__dirname,'..','fixtures');
8+
9+
functionfixturesPath(...args){
10+
returnpath.join(fixturesDir, ...args);
11+
}
12+
13+
functionreadFixtureSync(args,enc){
14+
if(Array.isArray(args))
15+
returnfs.readFileSync(fixturesPath(...args),enc);
16+
returnfs.readFileSync(fixturesPath(args),enc);
17+
}
18+
19+
functionreadFixtureKey(name,enc){
20+
returnfs.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
conststream=require('stream');
99
constutil=require('util');
1010
constTimer=process.binding('timer_wrap').Timer;
11+
const{ fixturesDir }=require('./fixtures');
1112

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

1516
constnoop=()=>{};
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-
constcommon=require('../common');
2+
require('../common');
33
constassert=require('assert');
4-
constpath=require('path');
4+
constfixtures=require('../common/fixtures');
55

66
constspawn=require('child_process').spawn;
7-
constchildPath=path.join(common.fixturesDir,
8-
'parent-process-nonpersistent.js');
7+
constchildPath=fixtures.path('parent-process-nonpersistent.js');
98
letpersistentPid=-1;
109

1110
constchild=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
constcommon=require('../common');
33
constassert=require('assert');
44
constexecFile=require('child_process').execFile;
5-
constpath=require('path');
65
constuv=process.binding('uv');
6+
constfixtures=require('../common/fixtures');
77

8-
constfixture=path.join(common.fixturesDir,'exit.js');
8+
constfixture=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
constcommon=require('../common');
33
constassert=require('assert');
44
constspawn=require('child_process').spawn;
5-
constpath=require('path');
5+
constfixtures=require('../common/fixtures');
66

7-
constexitScript=path.join(common.fixturesDir,'exit.js');
7+
constexitScript=fixtures.path('exit.js');
88
constexitChild=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-
consterrorScript=path.join(common.fixturesDir,
16-
'child_process_should_emit_error.js');
15+
consterrorScript=fixtures.path('child_process_should_emit_error.js');
1716
consterrorChild=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
constcommon=require('../common');
33
constassert=require('assert');
44
constfork=require('child_process').fork;
5+
constfixtures=require('../common/fixtures');
56

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

89
letgotMessage=false;
910
letgotExit=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
constassert=require('assert');
44
constfork=require('child_process').fork;
55
constargs=['foo','bar'];
6+
constfixtures=require('../common/fixtures');
67

7-
constn=fork(`${common.fixturesDir}/child-process-spawn-node.js`,args);
8+
constn=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-
constcommon=require('../common');
2+
require('../common');
33
constchild_process=require('child_process');
4+
constfixtures=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-
constcommon=require('../common');
3+
require('../common');
44
constassert=require('assert');
55

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

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

1211
letgotHelloWorld=false;
1312
letgotEcho=false;

0 commit comments

Comments
(0)