-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate error when failing to create directories on startup (#1017)
- Loading branch information
Showing
4 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var nconf = require('nconf'); | ||
|
||
function initConfigFile(foreverRoot) { | ||
return new nconf.File({file: path.join(foreverRoot, 'config.json')}); | ||
} | ||
|
||
// | ||
// Synchronously create the `root` directory | ||
// and the `pid` directory for forever. Although there is | ||
// an additional overhead here of the sync action. It simplifies | ||
// the setup of forever dramatically. | ||
// | ||
function tryCreateDir(dir) { | ||
try { | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir, '0755'); | ||
} | ||
} | ||
catch (error) { | ||
throw new Error('Failed to create directory '+dir+":" +error.message); | ||
} | ||
} | ||
|
||
module.exports = { | ||
initConfigFile: initConfigFile, | ||
tryCreateDir: tryCreateDir | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var fs = require('fs'); | ||
var configUtils = require('../../lib/util/config-utils'); | ||
var expect = require('chai').expect; | ||
|
||
describe('config-utils', () => { | ||
describe('tryCreateDir', () => { | ||
it('happy path', () => { | ||
expect(() => { | ||
configUtils.tryCreateDir('happypath'); | ||
}).to.not.throw(); | ||
|
||
expect(fs.existsSync('happypath')).to.equal(true); | ||
fs.rmdirSync('happypath'); | ||
}); | ||
|
||
it('throws an error on invalid directory', () => { | ||
expect(() => { | ||
configUtils.tryCreateDir(''); | ||
}).to.throw(/Failed to create directory :ENOENT: no such file or directory, mkdir/); | ||
}); | ||
|
||
it('does not fail when creating directory that already exists', () => { | ||
expect(() => { | ||
configUtils.tryCreateDir('dummy'); | ||
configUtils.tryCreateDir('dummy'); | ||
}).to.not.throw(); | ||
fs.rmdirSync('dummy'); | ||
}); | ||
}); | ||
}); |