|
21 | 21 |
|
22 | 22 | 'use strict';
|
23 | 23 | const common = require('../common');
|
24 |
| -const assert = require('assert'); |
25 |
| -const path = require('path'); |
26 |
| -const fs = require('fs'); |
27 |
| -let openCount = 0; |
28 |
| -let mode; |
29 |
| -let content; |
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 | +const assert = require('assert'); |
| 29 | +const path = require('path'); |
| 30 | +const fs = 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 | +const mode = common.isWindows ? 0o444 : 0o755; |
| 35 | + |
| 36 | +// Reset the umask for testing |
| 37 | +process.umask(0o000); |
51 | 38 |
|
52 | 39 | const tmpdir = require('../common/tmpdir');
|
53 | 40 | tmpdir.refresh();
|
54 | 41 |
|
55 | 42 | // Test writeFileSync
|
56 |
| -const file1 = path.join(tmpdir.path, 'testWriteFileSync.txt'); |
57 |
| - |
58 |
| -fs.writeFileSync(file1, '123', { mode }); |
| 43 | +{ |
| 44 | + const file = 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 | + const content = 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 |
| -const file2 = 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 |
| -const file3 = path.join(tmpdir.path, 'testWriteFileSyncFd.txt'); |
| 53 | +{ |
| 54 | + const file = path.join(tmpdir.path, 'testAppendFileSync.txt'); |
77 | 55 |
|
78 |
| -const fd = 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 |
| -function openSync() { |
91 |
| - openCount++; |
92 |
| - return fs._openSync.apply(fs, arguments); |
| 56 | + fs.appendFileSync(file, 'abc', { mode }); |
| 57 | + const content = fs.readFileSync(file, { encoding: 'utf8' }); |
| 58 | + assert.strictEqual(content, 'abc'); |
| 59 | + assert.strictEqual(fs.statSync(file).mode & mode, mode); |
93 | 60 | }
|
94 | 61 |
|
95 |
| -function closeSync() { |
96 |
| - openCount--; |
97 |
| - return fs._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 | + let openCount = 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 | + const file = path.join(tmpdir.path, 'testWriteFileSyncFd.txt'); |
| 81 | + const fd = fs.openSync(file, 'w+', mode); |
| 82 | + |
| 83 | + fs.writeFileSync(fd, '123'); |
| 84 | + fs.closeSync(fd); |
| 85 | + const content = 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 |
| -const file4 = 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 | + const file = 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 | + const content = fs.readFileSync(file, { encoding: 'utf8' }); |
| 102 | + assert.strictEqual(content, 'hello world!'); |
| 103 | +} |
0 commit comments