Skip to content

Commit 3fb1992

Browse files
committed
test: enforce use of assert.fail via a lint rule
1 parent 0ffa607 commit 3fb1992

14 files changed

+21
-23
lines changed

test/abort/test-abort-fatal-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exec(...cmdline, common.mustCall((err, stdout, stderr) => {
3636
if (!err) {
3737
console.log(stdout);
3838
console.log(stderr);
39-
assert(false, 'this test should fail');
39+
assert.fail('this test should fail');
4040
}
4141

4242
assert(common.nodeProcessAborted(err.code, err.signal));

test/eslint.config_partial.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export default [
6565
selector: "CallExpression[callee.name='assert'][arguments.1.type='Literal']:not([arguments.1.raw=/['\"`].*/])",
6666
message: 'Do not use a non-string literal for the second argument of assert()',
6767
},
68+
{
69+
selector: 'CallExpression:matches([callee.type="Identifier"][callee.name="assert"], [callee.type="MemberExpression"][callee.object.type="Identifier"][callee.object.name="assert"][callee.property.type="Identifier"][callee.property.name="ok"])[arguments.0.type="Literal"]',
70+
message: 'Do not use a literal for the first argument of assert(), use assert.fail() instead or remove the call',
71+
},
6872
{
6973
selector: "CallExpression:matches([callee.name='throws'], [callee.property.name='throws'])[arguments.1.type='Literal']:not([arguments.1.regex])",
7074
message: 'Use an object as second argument of `assert.throws()`.',

test/message/internal_assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
require('../common');
55

66
const assert = require('internal/assert');
7-
assert(false);
7+
assert(2 + 2 > 5);

test/parallel/test-cli-node-options-docs.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) {
8787
// CLI options
8888
if (!isV8Option && !hasTrueAsDefaultValue) {
8989
if (new RegExp(`###.*\`${envVar}[[=\\s\\b\`]`).test(cliText) === false) {
90-
assert(false, `Should have option ${envVar} documented`);
90+
assert.fail(`Should have option ${envVar} documented`);
9191
} else {
9292
manPagesOptions.delete(envVar.slice(1));
9393
}
9494
}
9595

9696
if (!hasTrueAsDefaultValue && new RegExp(`###.*\`--no${envVar.slice(1)}[[=\\s\\b\`]`).test(cliText) === true) {
97-
assert(false, `Should not have option --no${envVar.slice(1)} documented`);
97+
assert.fail(`Should not have option --no${envVar.slice(1)} documented`);
9898
}
9999

100100
if (!isV8Option && hasTrueAsDefaultValue) {
101101
if (new RegExp(`###.*\`--no${envVar.slice(1)}[[=\\s\\b\`]`).test(cliText) === false) {
102-
assert(false, `Should have option --no${envVar.slice(1)} documented`);
102+
assert.fail(`Should have option --no${envVar.slice(1)} documented`);
103103
} else {
104104
manPagesOptions.delete(`-no${envVar.slice(1)}`);
105105
}
@@ -108,21 +108,21 @@ for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) {
108108
// NODE_OPTIONS
109109
if (isInNodeOption && !hasTrueAsDefaultValue &&
110110
new RegExp(`\`${envVar}\``).test(nodeOptionsText) === false) {
111-
assert(false, `Should have option ${envVar} in NODE_OPTIONS documented`);
111+
assert.fail(`Should have option ${envVar} in NODE_OPTIONS documented`);
112112
}
113113

114114
if (isInNodeOption && hasTrueAsDefaultValue && new RegExp(`\`--no${envVar.slice(1)}\``).test(cliText) === false) {
115-
assert(false, `Should have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
115+
assert.fail(`Should have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
116116
}
117117

118118
if (!hasTrueAsDefaultValue && new RegExp(`\`--no${envVar.slice(1)}\``).test(cliText) === true) {
119-
assert(false, `Should not have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
119+
assert.fail(`Should not have option --no${envVar.slice(1)} in NODE_OPTIONS documented`);
120120
}
121121

122122
// V8 options
123123
if (isV8Option) {
124124
if (new RegExp(`###.*\`${envVar}[[=\\s\\b\`]`).test(v8OptionsText) === false) {
125-
assert(false, `Should have option ${envVar} in V8 options documented`);
125+
assert.fail(`Should have option ${envVar} in V8 options documented`);
126126
} else {
127127
manPagesOptions.delete(envVar.slice(1));
128128
}

test/parallel/test-cluster-bind-twice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ if (!id) {
102102
}));
103103
}, 2));
104104
} else {
105-
assert(0); // Bad command line argument
105+
assert.fail('Bad command line argument');
106106
}
107107

108108
function startWorker() {

test/parallel/test-cluster-eaddrinuse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ if (id === 'undefined') {
5858
}));
5959
}));
6060
} else {
61-
assert(0); // Bad argument.
61+
assert.fail('Bad argument');
6262
}

test/parallel/test-fastutf8stream-sync.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@ assert.throws(() => {
181181
assert.ok(stream.write('hello world 👀\n'));
182182
assert.ok(stream.write('another line 👀\n'));
183183

184-
// Check internal buffer length (may not be available in Utf8Stream)
185-
// This is implementation-specific, so we just verify writes succeeded
186-
assert.ok(true, 'writes completed successfully');
187-
188184
stream.end();
189185
}
190186

@@ -200,7 +196,6 @@ assert.throws(() => {
200196
}
201197

202198
// Check internal buffer length (implementation-specific)
203-
assert.ok(true, 'writes completed successfully');
204199
readFile(dest, 'utf8', common.mustSucceed((data) => {
205200
assert.strictEqual(data, str);
206201
}));

test/parallel/test-fs-glob.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ describe('glob - with restricted directory', function() {
529529
for await (const match of asyncGlob('*', { cwd: restrictedDir })) {
530530
results.push(match);
531531
}
532-
assert.ok(true, 'glob completed without throwing on readdir error');
533532
} finally {
534533
try {
535534
chmodSync(restrictedDir, 0o755);

test/parallel/test-http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const server = http.Server(common.mustCall((req, res) => {
4646
assert.strictEqual(req.headers.cookie, 'abc=123; def=456; ghi=789');
4747
break;
4848
default:
49-
assert(false, `Unexpected request for ${req.url}`);
49+
assert.fail(`Unexpected request for ${req.url}`);
5050
}
5151

5252
if (expectedRequests.length === 0)

test/parallel/test-inspector-network-http2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const handleStream = common.mustCallAtLeast((stream, headers) => {
9696
}));
9797
break;
9898
default:
99-
assert(false, `Unexpected path: ${path}`);
99+
assert.fail(`Unexpected path: ${path}`);
100100
}
101101
});
102102

0 commit comments

Comments
 (0)