Skip to content

Commit

Permalink
tools,test: add linter rule for mustSucceed
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Oct 14, 2020
1 parent c275bf0 commit e43195b
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 97 deletions.
1 change: 1 addition & 0 deletions test/.eslintrc.yaml
Expand Up @@ -50,6 +50,7 @@ rules:
node-core/prefer-assert-iferror: error
node-core/prefer-assert-methods: error
node-core/prefer-common-mustnotcall: error
node-core/prefer-common-mustsucceed: error
node-core/crypto-check: error
node-core/eslint-check: error
node-core/async-iife-no-unused-result: error
Expand Down
18 changes: 6 additions & 12 deletions test/parallel/test-crypto-hkdf.js
Expand Up @@ -123,8 +123,7 @@ const {
assert(syncResult instanceof ArrayBuffer);
let is_async = false;
hkdf(hash, secret, salt, info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert(is_async);
assert(asyncResult instanceof ArrayBuffer);
assert.deepStrictEqual(syncResult, asyncResult);
Expand All @@ -141,8 +140,7 @@ const {

const syncResult = hkdfSync(hash, buf_secret, buf_salt, buf_info, length);
hkdf(hash, buf_secret, buf_salt, buf_info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
Expand All @@ -154,8 +152,7 @@ const {

const syncResult = hkdfSync(hash, key_secret, buf_salt, buf_info, length);
hkdf(hash, key_secret, buf_salt, buf_info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
Expand All @@ -167,8 +164,7 @@ const {

const syncResult = hkdfSync(hash, ta_secret, ta_salt, ta_info, length);
hkdf(hash, ta_secret, ta_salt, ta_info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
Expand All @@ -185,8 +181,7 @@ const {
ta_info.buffer,
length);
hkdf(hash, ta_secret, ta_salt, ta_info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
Expand All @@ -203,8 +198,7 @@ const {
sa_info,
length);
hkdf(hash, ta_secret, sa_salt, sa_info, length,
common.mustCall((err, asyncResult) => {
assert.ifError(err);
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(syncResult, asyncResult);
}));
}
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-crypto-random.js
Expand Up @@ -166,8 +166,7 @@ common.expectWarning('DeprecationWarning',
new SharedArrayBuffer(10)
].forEach((buf) => {
const before = Buffer.from(buf).toString('hex');
crypto.randomFill(buf, common.mustCall((err, buf) => {
assert.ifError(err);
crypto.randomFill(buf, common.mustSucceed((buf) => {
const after = Buffer.from(buf).toString('hex');
assert.notStrictEqual(before, after);
}));
Expand Down
9 changes: 3 additions & 6 deletions test/parallel/test-crypto-secret-keygen.js
Expand Up @@ -76,8 +76,7 @@ assert.throws(() => generateKeySync('aes', { length: 123 }), {
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, 128 / 8);

generateKey('aes', { length: 128 }, common.mustCall((err, key) => {
assert.ifError(err);
generateKey('aes', { length: 128 }, common.mustSucceed((key) => {
assert(key);
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, 128 / 8);
Expand All @@ -90,8 +89,7 @@ assert.throws(() => generateKeySync('aes', { length: 123 }), {
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, 256 / 8);

generateKey('aes', { length: 256 }, common.mustCall((err, key) => {
assert.ifError(err);
generateKey('aes', { length: 256 }, common.mustSucceed((key) => {
assert(key);
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, 256 / 8);
Expand All @@ -104,8 +102,7 @@ assert.throws(() => generateKeySync('aes', { length: 123 }), {
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, Math.floor(123 / 8));

generateKey('hmac', { length: 123 }, common.mustCall((err, key) => {
assert.ifError(err);
generateKey('hmac', { length: 123 }, common.mustSucceed((key) => {
assert(key);
const keybuf = key.export();
assert.strictEqual(keybuf.byteLength, Math.floor(123 / 8));
Expand Down
Expand Up @@ -10,8 +10,7 @@ const buf = Buffer.allocUnsafe(256);
const offset = 20;
const len = buf.length - offset;

const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.ifError(err);
const messageSent = common.mustSucceed(function messageSent(bytes) {
assert.notStrictEqual(bytes, buf.length);
assert.strictEqual(bytes, buf.length - offset);
client.close();
Expand Down
Expand Up @@ -10,8 +10,7 @@ const buf = Buffer.alloc(256, 'x');
const offset = 20;
const len = buf.length - offset;

const onMessage = common.mustCall(function messageSent(err, bytes) {
assert.ifError(err);
const onMessage = common.mustSucceed(function messageSent(bytes) {
assert.notStrictEqual(bytes, buf.length);
assert.strictEqual(bytes, buf.length - offset);
client.close();
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-dgram-send-callback-buffer-length.js
Expand Up @@ -31,8 +31,7 @@ const buf = Buffer.allocUnsafe(256);
const offset = 20;
const len = buf.length - offset;

const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.ifError(err);
const messageSent = common.mustSucceed(function messageSent(bytes) {
assert.notStrictEqual(bytes, buf.length);
assert.strictEqual(bytes, buf.length - offset);
client.close();
Expand Down
Expand Up @@ -6,8 +6,7 @@ const dgram = require('dgram');

const client = dgram.createSocket('udp4');

const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.ifError(err);
const messageSent = common.mustSucceed(function messageSent(bytes) {
assert.strictEqual(bytes, buf1.length + buf2.length);
});

Expand Down
52 changes: 52 additions & 0 deletions test/parallel/test-eslint-prefer-common-mustsucceed.js
@@ -0,0 +1,52 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

common.skipIfEslintMissing();

const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
const rule = require('../../tools/eslint-rules/prefer-common-mustsucceed');

const msg1 = 'Please use common.mustSucceed instead of ' +
'common.mustCall(assert.ifError).';
const msg2 = 'Please use common.mustSucceed instead of ' +
'common.mustCall with assert.ifError.';

new RuleTester({
parserOptions: { ecmaVersion: 2015 }
}).run('prefer-common-mustsucceed', rule, {
valid: [
'foo((err) => assert.ifError(err))',
'foo(function(err) { assert.ifError(err) })',
'foo(assert.ifError)',
'common.mustCall((err) => err)'
],
invalid: [
{
code: 'common.mustCall(assert.ifError)',
errors: [{ message: msg1 }]
},
{
code: 'common.mustCall((err) => assert.ifError(err))',
errors: [{ message: msg2 }]
},
{
code: 'common.mustCall((e) => assert.ifError(e))',
errors: [{ message: msg2 }]
},
{
code: 'common.mustCall(function(e) { assert.ifError(e); })',
errors: [{ message: msg2 }]
},
{
code: 'common.mustCall(function(e) { return assert.ifError(e); })',
errors: [{ message: msg2 }]
},
{
code: 'common.mustCall(function(e) {{ assert.ifError(e); }})',
errors: [{ message: msg2 }]
}
]
});
36 changes: 10 additions & 26 deletions test/parallel/test-fs-append-file.js
Expand Up @@ -45,11 +45,8 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
{
const filename = join(tmpdir.path, 'append.txt');

fs.appendFile(filename, s, common.mustCall(function(e) {
assert.ifError(e);

fs.readFile(filename, common.mustCall(function(e, buffer) {
assert.ifError(e);
fs.appendFile(filename, s, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
assert.strictEqual(Buffer.byteLength(s), buffer.length);
}));
}));
Expand All @@ -72,11 +69,8 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
const filename = join(tmpdir.path, 'append-non-empty.txt');
fs.writeFileSync(filename, currentFileData);

fs.appendFile(filename, s, common.mustCall(function(e) {
assert.ifError(e);

fs.readFile(filename, common.mustCall(function(e, buffer) {
assert.ifError(e);
fs.appendFile(filename, s, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
buffer.length);
}));
Expand Down Expand Up @@ -104,11 +98,8 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

const buf = Buffer.from(s, 'utf8');

fs.appendFile(filename, buf, common.mustCall((e) => {
assert.ifError(e);

fs.readFile(filename, common.mustCall((e, buffer) => {
assert.ifError(e);
fs.appendFile(filename, buf, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
}));
}));
Expand Down Expand Up @@ -166,17 +157,10 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
const filename = join(tmpdir.path, 'append-descriptors.txt');
fs.writeFileSync(filename, currentFileData);

fs.open(filename, 'a+', common.mustCall((e, fd) => {
assert.ifError(e);

fs.appendFile(fd, s, common.mustCall((e) => {
assert.ifError(e);

fs.close(fd, common.mustCall((e) => {
assert.ifError(e);

fs.readFile(filename, common.mustCall((e, buffer) => {
assert.ifError(e);
fs.open(filename, 'a+', common.mustSucceed((fd) => {
fs.appendFile(fd, s, common.mustSucceed(() => {
fs.close(fd, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
buffer.length);
}));
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-lchown.js
Expand Up @@ -58,8 +58,7 @@ if (!common.isWindows) {
tmpdir.refresh();
fs.copyFileSync(__filename, testFile);
fs.lchownSync(testFile, uid, gid);
fs.lchown(testFile, uid, gid, common.mustCall(async (err) => {
assert.ifError(err);
fs.lchown(testFile, uid, gid, common.mustSucceed(async (err) => {
await promises.lchown(testFile, uid, gid);
}));
}
3 changes: 1 addition & 2 deletions test/parallel/test-fs-readfile-pipe.js
Expand Up @@ -31,8 +31,7 @@ const assert = require('assert');
const fs = require('fs');

if (process.argv[2] === 'child') {
fs.readFile('/dev/stdin', common.mustCall(function(er, data) {
assert.ifError(er);
fs.readFile('/dev/stdin', common.mustSucceed((data) => {
process.stdout.write(data);
}));
return;
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-rm.js
Expand Up @@ -72,8 +72,7 @@ function removeAsync(dir) {
assert.strictEqual(err.syscall, 'rm');

// Recursive removal should succeed.
fs.rm(dir, { recursive: true }, common.mustCall((err) => {
assert.ifError(err);
fs.rm(dir, { recursive: true }, common.mustSucceed(() => {

// Attempted removal should fail now because the directory is gone.
fs.rm(dir, common.mustCall((err) => {
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-fs-truncate.js
Expand Up @@ -71,8 +71,7 @@ fs.truncateSync(fd);
fs.closeSync(fd);

// Async tests
testTruncate(common.mustCall(function(er) {
assert.ifError(er);
testTruncate(common.mustSucceed(() => {
testFtruncate(common.mustSucceed());
}));

Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-fs-write-file-typedarrays.js
Expand Up @@ -33,9 +33,7 @@ for (const expectView of common.getArrayBufferViews(inputBuffer)) {
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
console.log('Async test for ', expectView[Symbol.toStringTag]);
const file = `${filename}-${expectView[Symbol.toStringTag]}`;
fs.writeFile(file, expectView, common.mustCall((e) => {
assert.ifError(e);

fs.writeFile(file, expectView, common.mustSucceed(() => {
fs.readFile(file, 'utf8', common.mustSucceed((data) => {
assert.strictEqual(data, inputBuffer.toString('utf8'));
}));
Expand Down
31 changes: 8 additions & 23 deletions test/parallel/test-fs-write-file.js
Expand Up @@ -38,11 +38,8 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';

fs.writeFile(filename, s, common.mustCall(function(e) {
assert.ifError(e);

fs.readFile(filename, common.mustCall(function(e, buffer) {
assert.ifError(e);
fs.writeFile(filename, s, common.mustSucceed(() => {
fs.readFile(filename, common.mustSucceed((buffer) => {
assert.strictEqual(Buffer.byteLength(s), buffer.length);
}));
}));
Expand All @@ -51,31 +48,19 @@ fs.writeFile(filename, s, common.mustCall(function(e) {
const filename2 = join(tmpdir.path, 'test2.txt');
const buf = Buffer.from(s, 'utf8');

fs.writeFile(filename2, buf, common.mustCall(function(e) {
assert.ifError(e);

fs.readFile(filename2, common.mustCall(function(e, buffer) {
assert.ifError(e);

fs.writeFile(filename2, buf, common.mustSucceed(() => {
fs.readFile(filename2, common.mustSucceed((buffer) => {
assert.strictEqual(buf.length, buffer.length);
}));
}));

// Test that writeFile accepts file descriptors.
const filename4 = join(tmpdir.path, 'test4.txt');

fs.open(filename4, 'w+', common.mustCall(function(e, fd) {
assert.ifError(e);

fs.writeFile(fd, s, common.mustCall(function(e) {
assert.ifError(e);

fs.close(fd, common.mustCall(function(e) {
assert.ifError(e);

fs.readFile(filename4, common.mustCall(function(e, buffer) {
assert.ifError(e);

fs.open(filename4, 'w+', common.mustSucceed((fd) => {
fs.writeFile(fd, s, common.mustSucceed(() => {
fs.close(fd, common.mustSucceed(() => {
fs.readFile(filename4, common.mustSucceed((buffer) => {
assert.strictEqual(Buffer.byteLength(s), buffer.length);
}));
}));
Expand Down
7 changes: 2 additions & 5 deletions test/parallel/test-fs-write.js
Expand Up @@ -124,11 +124,8 @@ fs.open(fn3, 'w', 0o644, common.mustSucceed((fd) => {
fs.write(fd, expected, done);
}));

fs.open(fn4, 'w', 0o644, common.mustCall((err, fd) => {
assert.ifError(err);

const done = common.mustCall((err, written) => {
assert.ifError(err);
fs.open(fn4, 'w', 0o644, common.mustSucceed((fd) => {
const done = common.mustSucceed((written) => {
assert.strictEqual(written, Buffer.byteLength(expected));
fs.closeSync(fd);
});
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-inspector-heapdump.js
Expand Up @@ -18,8 +18,7 @@ session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
chunks.push(m.params.chunk);
});

session.post('HeapProfiler.takeHeapSnapshot', null, common.mustCall((e, r) => {
assert.ifError(e);
session.post('HeapProfiler.takeHeapSnapshot', null, common.mustSucceed((r) => {
assert.deepStrictEqual(r, {});
session.disconnect();

Expand Down

0 comments on commit e43195b

Please sign in to comment.