From c0acdb97d6fa007863ee135f60eeea2a87ae09e8 Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Wed, 2 Jan 2019 21:45:53 -0500 Subject: [PATCH] Match node's layout for access modes on fs, with tests --- README.md | 4 ++-- src/filesystem/interface.js | 5 +++++ tests/spec/fs.access.spec.js | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f0dfb72..61e72bc 100644 --- a/README.md +++ b/README.md @@ -750,7 +750,7 @@ fs.mkdir('/home', function(err) { #### fs.access(path, [mode], callback) -Tests a user's permissions for the file or directory supplied in `path` argument. Asynchronous [access(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/access.html). Callback gets no additional arguments. The `mode` argument can be one of the following (constants are available on `fs.constants`): +Tests a user's permissions for the file or directory supplied in `path` argument. Asynchronous [access(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/access.html). Callback gets no additional arguments. The `mode` argument can be one of the following (constants are available on `fs.constants` and `fs`): * `F_OK`: Test for existence of file. * `R_OK`: Test whether the file exists and grants read permission. @@ -763,7 +763,7 @@ Example: ```javascript // Check if the file exists in the current directory. -fs.access(file, fs.constants.F_OK, function(err) { +fs.access(file, fs.F_OK, function(err) { console.log(`${file} ${err ? 'does not exist' : 'exists'}`); }); ``` diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index aa14f74..59f7563 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -151,6 +151,11 @@ function FileSystem(options, callback) { // Expose Node's fs.constants to users fs.constants = Constants.fsConstants; + // Node also forwards the access mode flags onto fs + fs.F_OK = Constants.fsConstants.F_OK; + fs.R_OK = Constants.fsConstants.R_OK; + fs.W_OK = Constants.fsConstants.W_OK; + fs.X_OK = Constants.fsConstants.X_OK; // Expose Shell constructor this.Shell = Shell.bind(undefined, this); diff --git a/tests/spec/fs.access.spec.js b/tests/spec/fs.access.spec.js index 2df29cd..cb3b1e7 100644 --- a/tests/spec/fs.access.spec.js +++ b/tests/spec/fs.access.spec.js @@ -5,6 +5,26 @@ describe('fs.access', function () { beforeEach(util.setup); afterEach(util.cleanup); + it('should expose access mode flags on fs and fs.constants', function() { + var fs = util.fs(); + + // F_OK + expect(fs.F_OK).to.equal(0); + expect(fs.constants.F_OK).to.equal(0); + + // R_OK + expect(fs.R_OK).to.equal(4); + expect(fs.constants.R_OK).to.equal(4); + + // W_OK + expect(fs.W_OK).to.equal(2); + expect(fs.constants.W_OK).to.equal(2); + + // X_OK + expect(fs.X_OK).to.equal(1); + expect(fs.constants.X_OK).to.equal(1); + }); + it('should be a function', function () { var fs = util.fs(); expect(typeof fs.access).to.equal('function');