Skip to content

Commit

Permalink
test: refactor the code in test-fs-chmod
Browse files Browse the repository at this point in the history
* 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 evanlucas committed Jan 4, 2017
1 parent 797495a commit 1aa3ab1
Showing 1 changed file with 51 additions and 66 deletions.
117 changes: 51 additions & 66 deletions test/parallel/test-fs-chmod.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,7 +18,7 @@ fs._closeSync = fs.closeSync;
fs.close = close;
fs.closeSync = closeSync;

var openCount = 0;
let openCount = 0;

function open() {
openCount++;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 1aa3ab1

Please sign in to comment.