Skip to content

Commit 520d041

Browse files
cjihrigBethGriggs
authored andcommitted
test: refactor test-fs-write-file-sync.js
This commit reduces shared state by introducing scopes and block scoped variables. It also makes the monkey patching used by the test more robust. PR-URL: #24834 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent b1bbac7 commit 520d041

1 file changed

Lines changed: 58 additions & 62 deletions

File tree

‎test/parallel/test-fs-write-file-sync.js‎

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,87 +21,83 @@
2121

2222
'use strict';
2323
constcommon=require('../common');
24-
constassert=require('assert');
25-
constpath=require('path');
26-
constfs=require('fs');
27-
letopenCount=0;
28-
letmode;
29-
letcontent;
3024

3125
if(!common.isMainThread)
3226
common.skip('process.umask is not available in Workers');
3327

34-
// Need to hijack fs.open/close to make sure that things
35-
// get closed once they're opened.
36-
fs._openSync=fs.openSync;
37-
fs.openSync=openSync;
38-
fs._closeSync=fs.closeSync;
39-
fs.closeSync=closeSync;
40-
41-
// Reset the umask for testing
42-
process.umask(0o000);
28+
constassert=require('assert');
29+
constpath=require('path');
30+
constfs=require('fs');
4331

4432
// On Windows chmod is only able to manipulate read-only bit. Test if creating
4533
// the file in read-only mode works.
46-
if(common.isWindows){
47-
mode=0o444;
48-
}else{
49-
mode=0o755;
50-
}
34+
constmode=common.isWindows ? 0o444 : 0o755;
35+
36+
// Reset the umask for testing
37+
process.umask(0o000);
5138

5239
consttmpdir=require('../common/tmpdir');
5340
tmpdir.refresh();
5441

5542
// Test writeFileSync
56-
constfile1=path.join(tmpdir.path,'testWriteFileSync.txt');
57-
58-
fs.writeFileSync(file1,'123',{ mode });
43+
{
44+
constfile=path.join(tmpdir.path,'testWriteFileSync.txt');
5945

60-
content=fs.readFileSync(file1,{encoding: 'utf8'});
61-
assert.strictEqual(content,'123');
62-
63-
assert.strictEqual(fs.statSync(file1).mode&0o777,mode);
46+
fs.writeFileSync(file,'123',{ mode });
47+
constcontent=fs.readFileSync(file,{encoding: 'utf8'});
48+
assert.strictEqual(content,'123');
49+
assert.strictEqual(fs.statSync(file).mode&0o777,mode);
50+
}
6451

6552
// Test appendFileSync
66-
constfile2=path.join(tmpdir.path,'testAppendFileSync.txt');
67-
68-
fs.appendFileSync(file2,'abc',{ mode });
69-
70-
content=fs.readFileSync(file2,{encoding: 'utf8'});
71-
assert.strictEqual(content,'abc');
72-
73-
assert.strictEqual(fs.statSync(file2).mode&mode,mode);
74-
75-
// Test writeFileSync with file descriptor
76-
constfile3=path.join(tmpdir.path,'testWriteFileSyncFd.txt');
53+
{
54+
constfile=path.join(tmpdir.path,'testAppendFileSync.txt');
7755

78-
constfd=fs.openSync(file3,'w+',mode);
79-
fs.writeFileSync(fd,'123');
80-
fs.closeSync(fd);
81-
82-
content=fs.readFileSync(file3,{encoding: 'utf8'});
83-
assert.strictEqual(content,'123');
84-
85-
assert.strictEqual(fs.statSync(file3).mode&0o777,mode);
86-
87-
// Verify that all opened files were closed.
88-
assert.strictEqual(openCount,0);
89-
90-
functionopenSync(){
91-
openCount++;
92-
returnfs._openSync.apply(fs,arguments);
56+
fs.appendFileSync(file,'abc',{ mode });
57+
constcontent=fs.readFileSync(file,{encoding: 'utf8'});
58+
assert.strictEqual(content,'abc');
59+
assert.strictEqual(fs.statSync(file).mode&mode,mode);
9360
}
9461

95-
functioncloseSync(){
96-
openCount--;
97-
returnfs._closeSync.apply(fs,arguments);
62+
// Test writeFileSync with file descriptor
63+
{
64+
// Need to hijack fs.open/close to make sure that things
65+
// get closed once they're opened.
66+
const_openSync=fs.openSync;
67+
const_closeSync=fs.closeSync;
68+
letopenCount=0;
69+
70+
fs.openSync=(...args)=>{
71+
openCount++;
72+
return_openSync(...args);
73+
};
74+
75+
fs.closeSync=(...args)=>{
76+
openCount--;
77+
return_closeSync(...args);
78+
};
79+
80+
constfile=path.join(tmpdir.path,'testWriteFileSyncFd.txt');
81+
constfd=fs.openSync(file,'w+',mode);
82+
83+
fs.writeFileSync(fd,'123');
84+
fs.closeSync(fd);
85+
constcontent=fs.readFileSync(file,{encoding: 'utf8'});
86+
assert.strictEqual(content,'123');
87+
assert.strictEqual(fs.statSync(file).mode&0o777,mode);
88+
89+
// Verify that all opened files were closed.
90+
assert.strictEqual(openCount,0);
91+
fs.openSync=_openSync;
92+
fs.closeSync=_closeSync;
9893
}
9994

10095
// Test writeFileSync with flags
101-
constfile4=path.join(tmpdir.path,'testWriteFileSyncFlags.txt');
102-
103-
fs.writeFileSync(file4,'hello ',{encoding: 'utf8',flag: 'a'});
104-
fs.writeFileSync(file4,'world!',{encoding: 'utf8',flag: 'a'});
96+
{
97+
constfile=path.join(tmpdir.path,'testWriteFileSyncFlags.txt');
10598

106-
content=fs.readFileSync(file4,{encoding: 'utf8'});
107-
assert.strictEqual(content,'hello world!');
99+
fs.writeFileSync(file,'hello ',{encoding: 'utf8',flag: 'a'});
100+
fs.writeFileSync(file,'world!',{encoding: 'utf8',flag: 'a'});
101+
constcontent=fs.readFileSync(file,{encoding: 'utf8'});
102+
assert.strictEqual(content,'hello world!');
103+
}

0 commit comments

Comments
 (0)