diff options
author | Raymond Feng <raymond@strongloop.com> | 2013-03-20 01:37:03 +0100 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2013-03-20 01:37:43 +0100 |
commit | 25eaacad9a14c27465dc65e15e3a5eb5544031e6 (patch) | |
tree | 8e560bf4c7bd7264e5f84fadf54a1277eceb27e4 /test | |
parent | 2f4a62c5e13ed7ebe608fc7c861b0bd600401537 (diff) | |
download | node-new-25eaacad9a14c27465dc65e15e3a5eb5544031e6.tar.gz |
fs: make write/appendFileSync correctly set file mode
Diffstat (limited to 'test')
-rw-r--r-- | test/simple/test-fs-write-file-sync.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test/simple/test-fs-write-file-sync.js b/test/simple/test-fs-write-file-sync.js new file mode 100644 index 0000000000..3e723425ac --- /dev/null +++ b/test/simple/test-fs-write-file-sync.js @@ -0,0 +1,98 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var path = require('path'); +var fs = require('fs'); +var isWindows = process.platform === 'win32'; +var openCount = 0; +var mode; +var content; + +// Need to hijack fs.open/close to make sure that things +// get closed once they're opened. +fs._openSync = fs.openSync; +fs.openSync = openSync; +fs._closeSync = fs.closeSync; +fs.closeSync = closeSync; + +// Reset the umask for testing +var mask = process.umask(0000); + +// On Windows chmod is only able to manipulate read-only bit. Test if creating +// the file in read-only mode works. +if (isWindows) { + mode = 0444; +} else { + mode = 0755; +} + +// Test writeFileSync +var file1 = path.join(common.tmpDir, 'testWriteFileSync.txt'); +removeFile(file1); + +fs.writeFileSync(file1, '123', {mode: mode}); + +content = fs.readFileSync(file1, {encoding: 'utf8'}); +assert.equal('123', content); + +assert.equal(mode, fs.statSync(file1).mode & 0777); + +removeFile(file1); + +// Test appendFileSync +var file2 = path.join(common.tmpDir, 'testAppendFileSync.txt'); +removeFile(file2); + +fs.appendFileSync(file2, 'abc', {mode: mode}); + +content = fs.readFileSync(file2, {encoding: 'utf8'}); +assert.equal('abc', content); + +assert.equal(mode, fs.statSync(file2).mode & mode); + +removeFile(file2); + +// Verify that all opened files were closed. +assert.equal(0, openCount); + +// Removes a file if it exists. +function removeFile(file) { + try { + if (isWindows) + fs.chmodSync(file, 0666); + fs.unlinkSync(file); + } catch (err) { + if (err && err.code !== 'ENOENT') + throw err; + } +} + +function openSync() { + openCount++; + return fs._openSync.apply(fs, arguments); +} + +function closeSync() { + openCount--; + return fs._closeSync.apply(fs, arguments); +} |