1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24 25if (!common.isMainThread) 26 common.skip('Setting process.umask is not supported in Workers'); 27 28const assert = require('assert'); 29const path = require('path'); 30const fs = require('fs'); 31 32// On Windows chmod is only able to manipulate read-only bit. Test if creating 33// the file in read-only mode works. 34const mode = common.isWindows ? 0o444 : 0o755; 35 36// Reset the umask for testing 37process.umask(0o000); 38 39const tmpdir = require('../common/tmpdir'); 40tmpdir.refresh(); 41 42// Test writeFileSync 43{ 44 const file = path.join(tmpdir.path, 'testWriteFileSync.txt'); 45 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} 51 52// Test appendFileSync 53{ 54 const file = path.join(tmpdir.path, 'testAppendFileSync.txt'); 55 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); 60} 61 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; 93} 94 95// Test writeFileSync with flags 96{ 97 const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt'); 98 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} 104 105// Test writeFileSync with an object with an own toString function 106{ 107 const file = path.join(tmpdir.path, 'testWriteFileSyncStringify.txt'); 108 const data = { 109 toString() { 110 return 'hello world!'; 111 } 112 }; 113 114 fs.writeFileSync(file, data, { encoding: 'utf8', flag: 'a' }); 115 const content = fs.readFileSync(file, { encoding: 'utf8' }); 116 assert.strictEqual(content, String(data)); 117} 118