Skip to content

Commit

Permalink
Feature: Add methods for checking permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
nick spragg committed Jan 13, 2017
1 parent 1bb85ed commit 730f34b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class File {
return pathname.split(path.sep).length - 1;
}

_access(permission) {
let hasPermission = true;

return fsp.accessAsync(this._pathname, permission)
.catch(() => hasPermission = false)
.then(() => hasPermission);
}

isDirectorySync() {
return this._getStatsSync().isDirectory();
}
Expand Down Expand Up @@ -120,6 +128,18 @@ class File {
sizeSync() {
return this._getStatsSync().size;
}

isWritable() {
return this._access(fs.W_OK);
}

isReadable() {
return this._access(fs.R_OK);
}

isExecutable() {
return this._access(fs.X_OK);
}
}

module.exports.create = (filename) => {
Expand Down
46 changes: 46 additions & 0 deletions test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,50 @@ describe('File', () => {
assert.equal(file.getName(), getAbsolutePath('dates/a.txt'));
});
});

describe('.isWritable', () => {
it('returns true when the file has write permission', () => {
const file = File.create(getAbsolutePath('justFiles/a.json'));
file.isWritable()
.then((isWritable) => {
assert.strictEqual(isWritable, true);
});
});

it('returns false when the file does not have write permission', () => {
const file = File.create(getAbsolutePath('permissions/notWritable.json'));
file.isWritable()
.then((isWritable) => {
assert.strictEqual(isWritable, false);
});
});
});

describe('.isReadable', () => {
it('returns true when the file has read permission', () => {
const file = File.create(getAbsolutePath('permissions/readWrite.json'));
file.isReadable()
.then((isReadable) => {
assert.strictEqual(isReadable, true);
});
});
});

describe('.isExecutable', () => {
it('returns true when the file has read permission', () => {
const file = File.create(getAbsolutePath('permissions/executable.sh'));
file.isReadable()
.then((isExecutable) => {
assert.strictEqual(isExecutable, true);
});
});

it('returns false when the file does not have read permission', () => {
const file = File.create(getAbsolutePath('permissions/notExecutable.sh'));
file.isExecutable()
.then((isExecutable) => {
assert.strictEqual(isExecutable, false);
});
});
});
});
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 730f34b

Please sign in to comment.