From 3c3cb3e850c1879d1a1db31b07731ba2d378d947 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 17 Nov 2014 15:32:35 -0400 Subject: [PATCH] Create prefix directory when using data.prefix.set --- lib/data/data-prefix.coffee | 7 ++++-- lib/data/data-prefix.spec.coffee | 37 ++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/data/data-prefix.coffee b/lib/data/data-prefix.coffee index f050fd11..482c2cdf 100644 --- a/lib/data/data-prefix.coffee +++ b/lib/data/data-prefix.coffee @@ -1,3 +1,4 @@ +mkdirp = require('mkdirp') fsUtils = require('../fs-utils/fs-utils') prefix = null @@ -9,8 +10,10 @@ exports.set = (newPrefix, callback) -> if not fsUtils.isValidPath(newPrefix) return callback?(new Error('Invalid path')) - prefix = newPrefix - return callback?(null, prefix) + mkdirp newPrefix, (error) -> + return callback?(error) if error? + prefix = newPrefix + return callback?() exports.clear = -> prefix = null diff --git a/lib/data/data-prefix.spec.coffee b/lib/data/data-prefix.spec.coffee index 591f8fc0..7a0359e9 100644 --- a/lib/data/data-prefix.spec.coffee +++ b/lib/data/data-prefix.spec.coffee @@ -1,7 +1,12 @@ expect = require('chai').expect _ = require('lodash') +async = require('async') +fs = require('fs') +fsUtils = require('../fs-utils/fs-utils') +rimraf = require('rimraf') dataPrefix = require('./data-prefix') config = require('../config') +mock = require('../../tests/utils/mock') PREFIXES = main: config.dataPrefix @@ -10,6 +15,12 @@ PREFIXES = describe 'DataPrefix:', -> + beforeEach -> + mock.fs.init() + + afterEach -> + mock.fs.restore() + describe 'given no prefix', -> beforeEach -> @@ -22,6 +33,9 @@ describe 'DataPrefix:', -> describe '#set()', -> + beforeEach (done) -> + rimraf(PREFIXES.main, done) + it 'should be able to set a prefix', (done) -> expect(dataPrefix.get()).to.not.exist dataPrefix.set PREFIXES.main, (error) -> @@ -34,6 +48,29 @@ describe 'DataPrefix:', -> expect(error).to.be.an.instanceof(Error) done() + it 'should create the directory if it doesn\'t exist', (done) -> + + async.waterfall [ + + (callback) -> + fs.exists PREFIXES.main, (exists) -> + return callback(null, exists) + + (exists, callback) -> + expect(exists).to.be.false + dataPrefix.set(PREFIXES.main, callback) + + (callback) -> + fsUtils.isDirectory(PREFIXES.main, callback) + + (isDirectory, callback) -> + expect(isDirectory).to.be.true + return callback() + + ], (error) -> + expect(error).to.not.exist + done() + describe 'given a prefix', -> beforeEach (done) -> diff --git a/package.json b/package.json index 9b24c9a1..7c4b2724 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "lodash": "~2.4.1", "async": "~0.9.0", "rimraf": "~2.2.8", - "commander": "~2.5.0" + "commander": "~2.5.0", + "mkdirp": "~0.5.0" } }