From 424c86139d9ecc57f3a2961982941abb5ca8ba1b Mon Sep 17 00:00:00 2001 From: Adrian Estrada Date: Sat, 24 Dec 2016 16:07:52 -0500 Subject: [PATCH] test: refactor the code in test-fs-chmod * use const and let instead of var * use common.mustCall to control functions executions * use assert.strictEqual instead of assert.equal * use assert.ifError to handle errors * use arrow functions * remove unnecessary variables PR-URL: https://github.com/nodejs/node/pull/10440 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- test/parallel/test-fs-chmod.js | 117 ++++++++++++++------------------- 1 file changed, 51 insertions(+), 66 deletions(-) diff --git a/test/parallel/test-fs-chmod.js b/test/parallel/test-fs-chmod.js index 954916cbdbb365..ecba2e27bb2c6e 100644 --- a/test/parallel/test-fs-chmod.js +++ b/test/parallel/test-fs-chmod.js @@ -1,12 +1,11 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); -var path = require('path'); -var fs = require('fs'); -var got_error = false; -var success_count = 0; -var mode_async; -var mode_sync; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); + +let mode_async; +let mode_sync; // Need to hijack fs.open/close to make sure that things // get closed once they're opened. @@ -19,7 +18,7 @@ fs._closeSync = fs.closeSync; fs.close = close; fs.closeSync = closeSync; -var openCount = 0; +let openCount = 0; function open() { openCount++; @@ -54,57 +53,49 @@ if (common.isWindows) { const file1 = path.join(common.fixturesDir, 'a.js'); const file2 = path.join(common.fixturesDir, 'a1.js'); -fs.chmod(file1, mode_async.toString(8), function(err) { - if (err) { - got_error = true; +fs.chmod(file1, mode_async.toString(8), common.mustCall((err) => { + assert.ifError(err); + + console.log(fs.statSync(file1).mode); + + if (common.isWindows) { + assert.ok((fs.statSync(file1).mode & 0o777) & mode_async); } else { - console.log(fs.statSync(file1).mode); + assert.strictEqual(mode_async, fs.statSync(file1).mode & 0o777); + } + + fs.chmodSync(file1, mode_sync); + if (common.isWindows) { + assert.ok((fs.statSync(file1).mode & 0o777) & mode_sync); + } else { + assert.strictEqual(mode_sync, fs.statSync(file1).mode & 0o777); + } +})); + +fs.open(file2, 'a', common.mustCall((err, fd) => { + assert.ifError(err); + + fs.fchmod(fd, mode_async.toString(8), common.mustCall((err) => { + assert.ifError(err); + + console.log(fs.fstatSync(fd).mode); if (common.isWindows) { - assert.ok((fs.statSync(file1).mode & 0o777) & mode_async); + assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_async); } else { - assert.equal(mode_async, fs.statSync(file1).mode & 0o777); + assert.strictEqual(mode_async, fs.fstatSync(fd).mode & 0o777); } - fs.chmodSync(file1, mode_sync); + fs.fchmodSync(fd, mode_sync); if (common.isWindows) { - assert.ok((fs.statSync(file1).mode & 0o777) & mode_sync); + assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_sync); } else { - assert.equal(mode_sync, fs.statSync(file1).mode & 0o777); + assert.strictEqual(mode_sync, fs.fstatSync(fd).mode & 0o777); } - success_count++; - } -}); -fs.open(file2, 'a', function(err, fd) { - if (err) { - got_error = true; - console.error(err.stack); - return; - } - fs.fchmod(fd, mode_async.toString(8), function(err) { - if (err) { - got_error = true; - } else { - console.log(fs.fstatSync(fd).mode); - - if (common.isWindows) { - assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_async); - } else { - assert.equal(mode_async, fs.fstatSync(fd).mode & 0o777); - } - - fs.fchmodSync(fd, mode_sync); - if (common.isWindows) { - assert.ok((fs.fstatSync(fd).mode & 0o777) & mode_sync); - } else { - assert.equal(mode_sync, fs.fstatSync(fd).mode & 0o777); - } - success_count++; - fs.close(fd); - } - }); -}); + fs.close(fd); + })); +})); // lchmod if (fs.lchmod) { @@ -113,26 +104,20 @@ if (fs.lchmod) { common.refreshTmpDir(); fs.symlinkSync(file2, link); - fs.lchmod(link, mode_async, function(err) { - if (err) { - got_error = true; - } else { - console.log(fs.lstatSync(link).mode); - assert.equal(mode_async, fs.lstatSync(link).mode & 0o777); + fs.lchmod(link, mode_async, common.mustCall((err) => { + assert.ifError(err); - fs.lchmodSync(link, mode_sync); - assert.equal(mode_sync, fs.lstatSync(link).mode & 0o777); - success_count++; - } - }); -} else { - success_count++; + console.log(fs.lstatSync(link).mode); + assert.strictEqual(mode_async, fs.lstatSync(link).mode & 0o777); + + fs.lchmodSync(link, mode_sync); + assert.strictEqual(mode_sync, fs.lstatSync(link).mode & 0o777); + + })); } process.on('exit', function() { - assert.equal(3, success_count); - assert.equal(0, openCount); - assert.equal(false, got_error); + assert.strictEqual(0, openCount); });