Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: extended test to makeCallback cb type check #12140

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions test/parallel/test-fs-make-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,29 @@ const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];

function test(cb) {
const { sep } = require('path');
const warn = 'Calling an asynchronous function without callback is deprecated.';

common.refreshTmpDir();

function testMakeCallback(cb) {
return function() {
// fs.stat() calls makeCallback() on its second argument
fs.stat(__filename, cb);
// fs.mkdtemp() calls makeCallback() on its third argument
fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This throws an error if the test is run in isolation node test/parallel/test-fs-make-callback.js and common.tmpDir has not been created.

I think it makes sense to run common.refreshTmpDir() at the beginning of the test.

};
}

// Verify the case where a callback function is provided
assert.doesNotThrow(test(common.noop));

process.once('warning', common.mustCall((warning) => {
assert.strictEqual(
warning.message,
'Calling an asynchronous function without callback is deprecated.'
);

invalidArgumentsTests();
}));
common.expectWarning('DeprecationWarning', warn);

// Passing undefined/nothing calls rethrow() internally, which emits a warning
assert.doesNotThrow(test());
assert.doesNotThrow(testMakeCallback());

function invalidArgumentsTests() {
assert.throws(test(null), cbTypeError);
assert.throws(test(true), cbTypeError);
assert.throws(test(false), cbTypeError);
assert.throws(test(1), cbTypeError);
assert.throws(test(0), cbTypeError);
assert.throws(test('foo'), cbTypeError);
assert.throws(test(/foo/), cbTypeError);
assert.throws(test([]), cbTypeError);
assert.throws(test({}), cbTypeError);
function invalidCallbackThrowsTests() {
callbackThrowValues.forEach((value) => {
assert.throws(testMakeCallback(value), cbTypeError);
});
}

invalidCallbackThrowsTests();
30 changes: 30 additions & 0 deletions test/parallel/test-fs-makeStatsCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const cbTypeError = /^TypeError: "callback" argument must be a function$/;
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
const warn = 'Calling an asynchronous function without callback is deprecated.';

function testMakeStatsCallback(cb) {
return function() {
// fs.stat() calls makeStatsCallback() on its second argument
fs.stat(__filename, cb);
};
}

common.expectWarning('DeprecationWarning', warn);

// Verify the case where a callback function is provided
assert.doesNotThrow(testMakeStatsCallback(common.noop));

// Passing undefined/nothing calls rethrow() internally, which emits a warning
assert.doesNotThrow(testMakeStatsCallback());

function invalidCallbackThrowsTests() {
callbackThrowValues.forEach((value) => {
assert.throws(testMakeStatsCallback(value), cbTypeError);
});
}

invalidCallbackThrowsTests();