|
21 | 21 |
|
22 | 22 | 'use strict'; |
23 | 23 | constcommon=require('../common'); |
24 | | -constassert=require('assert'); |
25 | | -constpath=require('path'); |
26 | | -constfs=require('fs'); |
27 | | -letopenCount=0; |
28 | | -letmode; |
29 | | -letcontent; |
30 | 24 |
|
31 | 25 | if(!common.isMainThread) |
32 | 26 | common.skip('process.umask is not available in Workers'); |
33 | 27 |
|
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'); |
43 | 31 |
|
44 | 32 | // On Windows chmod is only able to manipulate read-only bit. Test if creating |
45 | 33 | // 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); |
51 | 38 |
|
52 | 39 | consttmpdir=require('../common/tmpdir'); |
53 | 40 | tmpdir.refresh(); |
54 | 41 |
|
55 | 42 | // 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'); |
59 | 45 |
|
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 | +} |
64 | 51 |
|
65 | 52 | // 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'); |
77 | 55 |
|
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); |
93 | 60 | } |
94 | 61 |
|
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; |
98 | 93 | } |
99 | 94 |
|
100 | 95 | // 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'); |
105 | 98 |
|
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