Skip to content

Commit f255541

Browse files
lucamaraschigibfahn
authored andcommitted
test: extended test to makeCallback cb type check
makeCallback and makeStatsCallback are both tested intedependently. PR-URL: #12140 Backport-PR-URL: #13785 Fixes: #12136 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 9256c18 commit f255541

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed
+18-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
5+
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
6+
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
57

6-
function test(cb) {
8+
const { sep } = require('path');
9+
10+
common.refreshTmpDir();
11+
12+
function testMakeCallback(cb) {
713
return function() {
8-
// fs.stat() calls makeCallback() on its second argument
9-
fs.stat(__filename, cb);
14+
// fs.mkdtemp() calls makeCallback() on its third argument
15+
fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb);
1016
};
1117
}
1218

13-
// Verify the case where a callback function is provided
14-
assert.doesNotThrow(test(function() {}));
19+
// Passing undefined/nothing calls rethrow() internally
20+
assert.doesNotThrow(testMakeCallback());
1521

16-
// Passing undefined calls rethrow() internally, which is fine
17-
assert.doesNotThrow(test(undefined));
22+
function invalidCallbackThrowsTests() {
23+
callbackThrowValues.forEach((value) => {
24+
assert.throws(testMakeCallback(value), cbTypeError);
25+
});
26+
}
1827

19-
// Anything else should throw
20-
assert.throws(test(null));
21-
assert.throws(test(true));
22-
assert.throws(test(false));
23-
assert.throws(test(1));
24-
assert.throws(test(0));
25-
assert.throws(test('foo'));
26-
assert.throws(test(/foo/));
27-
assert.throws(test([]));
28-
assert.throws(test({}));
28+
invalidCallbackThrowsTests();
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
6+
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
7+
8+
function testMakeStatsCallback(cb) {
9+
return function() {
10+
// fs.stat() calls makeStatsCallback() on its second argument
11+
fs.stat(__filename, cb);
12+
};
13+
}
14+
15+
// Verify the case where a callback function is provided
16+
assert.doesNotThrow(testMakeStatsCallback(common.noop));
17+
18+
// Passing undefined/nothing calls rethrow() internally
19+
assert.doesNotThrow(testMakeStatsCallback());
20+
21+
function invalidCallbackThrowsTests() {
22+
callbackThrowValues.forEach((value) => {
23+
assert.throws(testMakeStatsCallback(value), cbTypeError);
24+
});
25+
}
26+
27+
invalidCallbackThrowsTests();

0 commit comments

Comments
 (0)