From 86b3489a488481ec5f20600e7845922cb2fcaf2b Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 14 Nov 2014 12:58:05 -0400 Subject: [PATCH] Implement fsUtils.isDirectory() helper --- lib/fs-utils/fs-utils.coffee | 6 +++++ lib/fs-utils/fs-utils.spec.coffee | 38 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/fs-utils/fs-utils.coffee b/lib/fs-utils/fs-utils.coffee index 709dfb06..efd68c93 100644 --- a/lib/fs-utils/fs-utils.coffee +++ b/lib/fs-utils/fs-utils.coffee @@ -1,5 +1,11 @@ +fs = require('fs') _ = require('lodash') # TODO: There should be more complex checks here exports.isValidPath = (p) -> return _.isString(p) + +exports.isDirectory = (directory, callback) -> + fs.stat directory, (error, stats) -> + return callback?(error) if error? + return callback?(null, stats.isDirectory()) diff --git a/lib/fs-utils/fs-utils.spec.coffee b/lib/fs-utils/fs-utils.spec.coffee index ff6d8ca8..669a8c7d 100644 --- a/lib/fs-utils/fs-utils.spec.coffee +++ b/lib/fs-utils/fs-utils.spec.coffee @@ -1,6 +1,15 @@ expect = require('chai').expect +mockFs = require('mock-fs') fsUtils = require('./fs-utils') +FILESYSTEM = + text: + name: '/tmp/text' + contents: 'Hello World' + directory: + name: '/tmp/directory' + contents: mockFs.directory() + describe 'FsUtils:', -> describe '#isValidPath()', -> @@ -25,3 +34,32 @@ describe 'FsUtils:', -> './file/../file2' ] expect(fsUtils.isValidPath(validPath)).to.be.true + + describe '#isDirectory()', -> + + beforeEach -> + mockFsOptions = {} + for key, value of FILESYSTEM + mockFsOptions[value.name] = value.contents + mockFs(mockFsOptions) + + afterEach -> + mockFs.restore() + + it 'should return true if directory', (done) -> + fsUtils.isDirectory FILESYSTEM.directory.name, (error, isDirectory) -> + expect(error).to.not.exist + expect(isDirectory).to.be.true + done() + + it 'should return false if not a directory', (done) -> + fsUtils.isDirectory FILESYSTEM.text.name, (error, isDirectory) -> + expect(error).to.not.exist + expect(isDirectory).to.be.false + done() + + it 'should return an error if the path doesn\'t exists', (done) -> + fsUtils.isDirectory '/nonexistantpath', (error, isDirectory) -> + expect(error).to.exist + expect(isDirectory).to.be.undefined + done()