|  | 
|  | 1 | +// Copyright io.js 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 | +var common = require('../common'); | 
|  | 23 | +var assert = require('assert'); | 
|  | 24 | +var fs = require('fs'); | 
|  | 25 | +var path = require('path'); | 
|  | 26 | +var doesNotExist = __filename + '__this_should_not_exist'; | 
|  | 27 | +var readOnlyFile = path.join(common.tmpDir, 'read_only_file'); | 
|  | 28 | + | 
|  | 29 | +var removeFile = function(file) { | 
|  | 30 | +  try { | 
|  | 31 | +    fs.unlinkSync(file); | 
|  | 32 | +  } catch (err) { | 
|  | 33 | +    // Ignore error | 
|  | 34 | +  } | 
|  | 35 | +}; | 
|  | 36 | + | 
|  | 37 | +var createReadOnlyFile = function(file) { | 
|  | 38 | +  removeFile(file); | 
|  | 39 | +  fs.writeFileSync(file, ''); | 
|  | 40 | +  fs.chmodSync(file, 0444); | 
|  | 41 | +}; | 
|  | 42 | + | 
|  | 43 | +createReadOnlyFile(readOnlyFile); | 
|  | 44 | + | 
|  | 45 | +assert(typeof fs.F_OK === 'number'); | 
|  | 46 | +assert(typeof fs.R_OK === 'number'); | 
|  | 47 | +assert(typeof fs.W_OK === 'number'); | 
|  | 48 | +assert(typeof fs.X_OK === 'number'); | 
|  | 49 | + | 
|  | 50 | +fs.access(__filename, function(err) { | 
|  | 51 | +  assert.strictEqual(err, null, 'error should not exist'); | 
|  | 52 | +}); | 
|  | 53 | + | 
|  | 54 | +fs.access(__filename, fs.R_OK, function(err) { | 
|  | 55 | +  assert.strictEqual(err, null, 'error should not exist'); | 
|  | 56 | +}); | 
|  | 57 | + | 
|  | 58 | +fs.access(doesNotExist, function(err) { | 
|  | 59 | +  assert.notEqual(err, null, 'error should exist'); | 
|  | 60 | +  assert.strictEqual(err.code, 'ENOENT'); | 
|  | 61 | +  assert.strictEqual(err.path, doesNotExist); | 
|  | 62 | +}); | 
|  | 63 | + | 
|  | 64 | +fs.access(readOnlyFile, fs.F_OK | fs.R_OK, function(err) { | 
|  | 65 | +  assert.strictEqual(err, null, 'error should not exist'); | 
|  | 66 | +}); | 
|  | 67 | + | 
|  | 68 | +fs.access(readOnlyFile, fs.W_OK, function(err) { | 
|  | 69 | +  assert.notEqual(err, null, 'error should exist'); | 
|  | 70 | +  assert.strictEqual(err.path, readOnlyFile); | 
|  | 71 | +}); | 
|  | 72 | + | 
|  | 73 | +assert.throws(function() { | 
|  | 74 | +  fs.access(100, fs.F_OK, function(err) {}); | 
|  | 75 | +}, /path must be a string/); | 
|  | 76 | + | 
|  | 77 | +assert.throws(function() { | 
|  | 78 | +  fs.access(__filename, fs.F_OK); | 
|  | 79 | +}, /callback must be a function/); | 
|  | 80 | + | 
|  | 81 | +assert.doesNotThrow(function() { | 
|  | 82 | +  fs.accessSync(__filename); | 
|  | 83 | +}); | 
|  | 84 | + | 
|  | 85 | +assert.doesNotThrow(function() { | 
|  | 86 | +  var mode = fs.F_OK | fs.R_OK | fs.W_OK; | 
|  | 87 | + | 
|  | 88 | +  fs.accessSync(__filename, mode); | 
|  | 89 | +}); | 
|  | 90 | + | 
|  | 91 | +assert.throws(function() { | 
|  | 92 | +  fs.accessSync(doesNotExist); | 
|  | 93 | +}, function (err) { | 
|  | 94 | +  return err.code === 'ENOENT' && err.path === doesNotExist; | 
|  | 95 | +}); | 
|  | 96 | + | 
|  | 97 | +process.on('exit', function() { | 
|  | 98 | +  removeFile(readOnlyFile); | 
|  | 99 | +}); | 
0 commit comments