Browse Source

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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v4.x
Adrian Estrada 8 years ago
committed by Myles Borins
parent
commit
424c86139d
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 81
      test/parallel/test-fs-chmod.js

81
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;
} else {
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 {
assert.equal(mode_async, fs.statSync(file1).mode & 0o777);
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.equal(mode_sync, fs.statSync(file1).mode & 0o777);
assert.strictEqual(mode_sync, fs.statSync(file1).mode & 0o777);
}
success_count++;
}
});
}));
fs.open(file2, 'a', common.mustCall((err, fd) => {
assert.ifError(err);
fs.fchmod(fd, mode_async.toString(8), common.mustCall((err) => {
assert.ifError(err);
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);
assert.strictEqual(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);
assert.strictEqual(mode_sync, fs.fstatSync(fd).mode & 0o777);
}
success_count++;
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 {
fs.lchmod(link, mode_async, common.mustCall((err) => {
assert.ifError(err);
console.log(fs.lstatSync(link).mode);
assert.equal(mode_async, fs.lstatSync(link).mode & 0o777);
assert.strictEqual(mode_async, fs.lstatSync(link).mode & 0o777);
fs.lchmodSync(link, mode_sync);
assert.equal(mode_sync, fs.lstatSync(link).mode & 0o777);
success_count++;
}
});
} else {
success_count++;
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);
});

Loading…
Cancel
Save