Skip to content

Commit

Permalink
Match node's layout for access modes on fs, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Jan 3, 2019
1 parent f4ff2e9 commit c0acdb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ fs.mkdir('/home', function(err) {
#### fs.access(path, [mode], callback)<a name="access"></a>
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.
Expand All @@ -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'}`);
});
```
Expand Down
5 changes: 5 additions & 0 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions tests/spec/fs.access.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit c0acdb9

Please sign in to comment.