Skip to content

Commit 81e35b4

Browse files
committed
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.
1 parent a974753 commit 81e35b4

File tree

125 files changed

+496
-507
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

+496
-507
lines changed

test/common/README.md

Lines changed: 31 additions & 0 deletions
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+
* [<String>]
385+
386+
The absolute path to the `test/fixtures/` directory.
387+
388+
### fixtures.path(...args)
389+
390+
* `...args` [<String>]
391+
392+
Returns the result of `path.join(fixtures.fixturesDir, ...args)`.
393+
394+
### fixtures.readSync(args[, enc])
395+
396+
* `args` [<String>] | [<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` [<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

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
@@ -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

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

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

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

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
@@ -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

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

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

0 commit comments

Comments
 (0)