From 730f34b214dec8bc6d5d4aa442c47031e65a33ba Mon Sep 17 00:00:00 2001 From: nick spragg Date: Fri, 13 Jan 2017 20:24:04 +0000 Subject: [PATCH] Feature: Add methods for checking permissions --- src/file.js | 20 ++++++++++ test/file.js | 46 ++++++++++++++++++++++ test/fixtures/permissions/executable.sh | 0 test/fixtures/permissions/notExecutable.sh | 0 test/fixtures/permissions/notWritable.json | 0 test/fixtures/permissions/readWrite.json | 0 6 files changed, 66 insertions(+) create mode 100755 test/fixtures/permissions/executable.sh create mode 100644 test/fixtures/permissions/notExecutable.sh create mode 100644 test/fixtures/permissions/notWritable.json create mode 100644 test/fixtures/permissions/readWrite.json diff --git a/src/file.js b/src/file.js index ac50687..77215df 100644 --- a/src/file.js +++ b/src/file.js @@ -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(); } @@ -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) => { diff --git a/test/file.js b/test/file.js index 3518ad2..e19f26f 100644 --- a/test/file.js +++ b/test/file.js @@ -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); + }); + }); + }); }); diff --git a/test/fixtures/permissions/executable.sh b/test/fixtures/permissions/executable.sh new file mode 100755 index 0000000..e69de29 diff --git a/test/fixtures/permissions/notExecutable.sh b/test/fixtures/permissions/notExecutable.sh new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/permissions/notWritable.json b/test/fixtures/permissions/notWritable.json new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/permissions/readWrite.json b/test/fixtures/permissions/readWrite.json new file mode 100644 index 0000000..e69de29