Skip to content

Commit 5d5cf00

Browse files
edsadrMylesBorins
authored andcommitted
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>
1 parent ad4bf9e commit 5d5cf00

1 file changed

Lines changed: 51 additions & 66 deletions

File tree

‎test/parallel/test-fs-chmod.js‎

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
2-
varcommon=require('../common');
3-
varassert=require('assert');
4-
varpath=require('path');
5-
varfs=require('fs');
6-
vargot_error=false;
7-
varsuccess_count=0;
8-
varmode_async;
9-
varmode_sync;
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constpath=require('path');
5+
constfs=require('fs');
6+
7+
letmode_async;
8+
letmode_sync;
109

1110
// Need to hijack fs.open/close to make sure that things
1211
// get closed once they're opened.
@@ -19,7 +18,7 @@ fs._closeSync = fs.closeSync;
1918
fs.close=close;
2019
fs.closeSync=closeSync;
2120

22-
varopenCount=0;
21+
letopenCount=0;
2322

2423
functionopen(){
2524
openCount++;
@@ -54,57 +53,49 @@ if (common.isWindows) {
5453
constfile1=path.join(common.fixturesDir,'a.js');
5554
constfile2=path.join(common.fixturesDir,'a1.js');
5655

57-
fs.chmod(file1,mode_async.toString(8),function(err){
58-
if(err){
59-
got_error=true;
56+
fs.chmod(file1,mode_async.toString(8),common.mustCall((err)=>{
57+
assert.ifError(err);
58+
59+
console.log(fs.statSync(file1).mode);
60+
61+
if(common.isWindows){
62+
assert.ok((fs.statSync(file1).mode&0o777)&mode_async);
6063
}else{
61-
console.log(fs.statSync(file1).mode);
64+
assert.strictEqual(mode_async,fs.statSync(file1).mode&0o777);
65+
}
66+
67+
fs.chmodSync(file1,mode_sync);
68+
if(common.isWindows){
69+
assert.ok((fs.statSync(file1).mode&0o777)&mode_sync);
70+
}else{
71+
assert.strictEqual(mode_sync,fs.statSync(file1).mode&0o777);
72+
}
73+
}));
74+
75+
fs.open(file2,'a',common.mustCall((err,fd)=>{
76+
assert.ifError(err);
77+
78+
fs.fchmod(fd,mode_async.toString(8),common.mustCall((err)=>{
79+
assert.ifError(err);
80+
81+
console.log(fs.fstatSync(fd).mode);
6282

6383
if(common.isWindows){
64-
assert.ok((fs.statSync(file1).mode&0o777)&mode_async);
84+
assert.ok((fs.fstatSync(fd).mode&0o777)&mode_async);
6585
}else{
66-
assert.equal(mode_async,fs.statSync(file1).mode&0o777);
86+
assert.strictEqual(mode_async,fs.fstatSync(fd).mode&0o777);
6787
}
6888

69-
fs.chmodSync(file1,mode_sync);
89+
fs.fchmodSync(fd,mode_sync);
7090
if(common.isWindows){
71-
assert.ok((fs.statSync(file1).mode&0o777)&mode_sync);
91+
assert.ok((fs.fstatSync(fd).mode&0o777)&mode_sync);
7292
}else{
73-
assert.equal(mode_sync,fs.statSync(file1).mode&0o777);
93+
assert.strictEqual(mode_sync,fs.fstatSync(fd).mode&0o777);
7494
}
75-
success_count++;
76-
}
77-
});
7895

79-
fs.open(file2,'a',function(err,fd){
80-
if(err){
81-
got_error=true;
82-
console.error(err.stack);
83-
return;
84-
}
85-
fs.fchmod(fd,mode_async.toString(8),function(err){
86-
if(err){
87-
got_error=true;
88-
}else{
89-
console.log(fs.fstatSync(fd).mode);
90-
91-
if(common.isWindows){
92-
assert.ok((fs.fstatSync(fd).mode&0o777)&mode_async);
93-
}else{
94-
assert.equal(mode_async,fs.fstatSync(fd).mode&0o777);
95-
}
96-
97-
fs.fchmodSync(fd,mode_sync);
98-
if(common.isWindows){
99-
assert.ok((fs.fstatSync(fd).mode&0o777)&mode_sync);
100-
}else{
101-
assert.equal(mode_sync,fs.fstatSync(fd).mode&0o777);
102-
}
103-
success_count++;
104-
fs.close(fd);
105-
}
106-
});
107-
});
96+
fs.close(fd);
97+
}));
98+
}));
10899

109100
// lchmod
110101
if(fs.lchmod){
@@ -113,25 +104,19 @@ if (fs.lchmod) {
113104
common.refreshTmpDir();
114105
fs.symlinkSync(file2,link);
115106

116-
fs.lchmod(link,mode_async,function(err){
117-
if(err){
118-
got_error=true;
119-
}else{
120-
console.log(fs.lstatSync(link).mode);
121-
assert.equal(mode_async,fs.lstatSync(link).mode&0o777);
107+
fs.lchmod(link,mode_async,common.mustCall((err)=>{
108+
assert.ifError(err);
122109

123-
fs.lchmodSync(link,mode_sync);
124-
assert.equal(mode_sync,fs.lstatSync(link).mode&0o777);
125-
success_count++;
126-
}
127-
});
128-
}else{
129-
success_count++;
110+
console.log(fs.lstatSync(link).mode);
111+
assert.strictEqual(mode_async,fs.lstatSync(link).mode&0o777);
112+
113+
fs.lchmodSync(link,mode_sync);
114+
assert.strictEqual(mode_sync,fs.lstatSync(link).mode&0o777);
115+
116+
}));
130117
}
131118

132119

133120
process.on('exit',function(){
134-
assert.equal(3,success_count);
135-
assert.equal(0,openCount);
136-
assert.equal(false,got_error);
121+
assert.strictEqual(0,openCount);
137122
});

0 commit comments

Comments
 (0)