fs, test: add file open mode tests
authorBen Noordhuis <info@bnoordhuis.nl>
Mon, 30 Jan 2012 23:54:40 +0000 (00:54 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Tue, 31 Jan 2012 13:29:27 +0000 (14:29 +0100)
lib/fs.js
test/simple/test-fs-open-flags.js [new file with mode: 0644]

index 2dc11a750d05ac4d4de010a9fc059ffbd69a942a..a44654c8027c6433b54c0a47b03e3b3c7befda41 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -202,6 +202,12 @@ function stringToFlags(flag) {
   }
 }
 
+// exported but hidden, only used by test/simple/test-fs-open-flags.js
+Object.defineProperty(exports, '_stringToFlags', {
+  enumerable: false,
+  value: stringToFlags
+});
+
 function noop() {}
 
 // Yes, the follow could be easily DRYed up but I provide the explicit
diff --git a/test/simple/test-fs-open-flags.js b/test/simple/test-fs-open-flags.js
new file mode 100644 (file)
index 0000000..4e01b11
--- /dev/null
@@ -0,0 +1,50 @@
+// 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 constants = require('constants');
+var fs = require('fs');
+
+var O_APPEND    = constants.O_APPEND    || 0;
+var O_CREAT     = constants.O_CREAT     || 0;
+var O_DIRECTORY = constants.O_DIRECTORY || 0;
+var O_EXCL      = constants.O_EXCL      || 0;
+var O_NOCTTY    = constants.O_NOCTTY    || 0;
+var O_NOFOLLOW  = constants.O_NOFOLLOW  || 0;
+var O_RDONLY    = constants.O_RDONLY    || 0;
+var O_RDWR      = constants.O_RDWR      || 0;
+var O_SYMLINK   = constants.O_SYMLINK   || 0;
+var O_SYNC      = constants.O_SYNC      || 0;
+var O_TRUNC     = constants.O_TRUNC     || 0;
+var O_WRONLY    = constants.O_WRONLY    || 0;
+
+assert.equal(fs._stringToFlags('r'),  O_RDONLY);
+assert.equal(fs._stringToFlags('r+'), O_RDWR);
+assert.equal(fs._stringToFlags('w'),  O_TRUNC|O_CREAT|O_WRONLY);
+assert.equal(fs._stringToFlags('w+'), O_TRUNC|O_CREAT|O_RDWR);
+assert.equal(fs._stringToFlags('a'),  O_APPEND|O_CREAT|O_WRONLY);
+assert.equal(fs._stringToFlags('a+'), O_APPEND|O_CREAT|O_RDWR);
+
+'+ +a +r +w rw wa war raw r++ a++ w++'.split(' ').forEach(function(flags) {
+  assert.throws(function() { fs._stringToFlags(flags); });
+});