From e288d3e85f6ffcf41175090e63b7d5b01815e275 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 14 Nov 2014 13:34:04 -0400 Subject: [PATCH] Implement data.has() --- lib/data/data.coffee | 4 ++++ lib/data/data.spec.coffee | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/data/data.coffee b/lib/data/data.coffee index cc5a41be..ad3b7ef0 100644 --- a/lib/data/data.coffee +++ b/lib/data/data.coffee @@ -27,6 +27,10 @@ exports.set = haltIfNoPrefix (key, value, options, callback) -> keyPath = constructPath(key) fs.writeFile(keyPath, value, options, callback) +exports.has = haltIfNoPrefix (key, callback) -> + keyPath = constructPath(key) + fs.exists(keyPath, callback) + exports.remove = haltIfNoPrefix (key, callback) -> keyPath = constructPath(key) diff --git a/lib/data/data.spec.coffee b/lib/data/data.spec.coffee index 3f34aaff..593f6789 100644 --- a/lib/data/data.spec.coffee +++ b/lib/data/data.spec.coffee @@ -1,7 +1,6 @@ expect = require('chai').expect _ = require('lodash') fsUtils = require('../fs-utils/fs-utils') -fs = require('fs') async = require('async') mockFs = require('mock-fs') data = require('./data') @@ -52,6 +51,12 @@ describe 'Data:', -> removeDataKey = _.partial(data.remove, 'foobar') expect(removeDataKey).to.throw(Error) + describe '#has()', -> + + it 'should throw an error', -> + hasDataKey = _.partial(data.has, 'foobar') + expect(hasDataKey).to.throw(Error) + describe 'given a prefix', -> beforeEach -> @@ -92,6 +97,23 @@ describe 'Data:', -> expect(value).to.not.exist done() + describe '#has()', -> + + it 'should return true if a file exists', (done) -> + data.has FILESYSTEM.text.key, (hasKey) -> + expect(hasKey).to.be.true + done() + + it 'should return true if a directory exists', (done) -> + data.has FILESYSTEM.directory.key, (hasKey) -> + expect(hasKey).to.be.true + done() + + it 'should return false if the file doesn\'t exists', (done) -> + data.has 'foobar', (hasKey) -> + expect(hasKey).to.be.false + done() + describe '#set()', -> writeAndCheckFixture = (fixture) -> @@ -166,10 +188,8 @@ describe 'Data:', -> data.remove(directory.key, callback) (callback) -> - - # TODO: Implement data.has() to abstract this - fs.exists directory.name, (exists) -> - expect(exists).to.be.false + data.has directory.key, (hasKey) -> + expect(hasKey).to.be.false return callback() ], (error) ->