-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from igneel64/feature/use-process-cwd-when-env…
…-unavailable Fallback to process.cwd() for finding the cloudinary config file
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
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,19 @@ | ||
/** | ||
* Load the cloudinaryrc configuration from the top level path | ||
* @throws Will throw if the file is not or the error faced during the `require` call | ||
* @returns {NodeRequire} JSON configuration module | ||
*/ | ||
function requireConfig() { | ||
const configFileLocation = `${process.env.INIT_CWD || process.cwd()}/cloudinaryrc.json`; | ||
|
||
try { | ||
return require(configFileLocation); | ||
} catch (e) { | ||
if (e.code !== "MODULE_NOT_FOUND") { | ||
throw e; | ||
} | ||
throw Error(`Cloudinary config could not be found at ${configFileLocation}`); | ||
} | ||
} | ||
|
||
module.exports = requireConfig; |
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,25 @@ | ||
const requireConfig = require("../lib/helpers/requireConfig"); | ||
|
||
describe("requireConfig", () => { | ||
it("throws when config not found at expected location", () => { | ||
expect(requireConfig).toThrow(`Cloudinary config could not be found at ${process.env.INIT_CWD || process.cwd()}`); | ||
}); | ||
|
||
it("loads the required file", () => { | ||
const mockConfig = JSON.stringify({ test: 1 }); | ||
|
||
jest.mock( | ||
`${process.cwd()}/cloudinaryrc.json`, | ||
() => { | ||
return mockConfig; | ||
}, | ||
{ virtual: true } | ||
); | ||
|
||
const loadedConfig = requireConfig(); | ||
|
||
expect(loadedConfig).toBe(mockConfig); | ||
}); | ||
|
||
afterAll(() => jest.resetAllMocks()); | ||
}); |