Permalink
Browse files

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: #10440
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information...
edsadr authored and jasnell committed Dec 24, 2016
1 parent 13a4887 commit b85f9fec2fe3a3388f984052e8f1f218f6d7c1e6
Showing with 51 additions and 66 deletions.
  1. +51 −66 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,25 +104,19 @@ 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);
});

0 comments on commit b85f9fe

Please sign in to comment.