This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support config from cli args and config file (#61)
* feat: support config from cli args and config file * feat: update docs * feat: update config names + cli args * cr: update docs * cr: update README formatting update README formatting * fix: Make the config file keys camelCase, env UPPER_CASE, cli kebab-case Co-authored-by: Jared Rand <jared.rand@tophatmonocle.com> Co-authored-by: Noah <noah.negin-ulster@tophatmonocle.com>
- Loading branch information
1 parent
2f27d7a
commit 39a6549
Showing
11 changed files
with
304 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
_ | ||
_ |
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
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
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
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,6 @@ | ||
import path from 'path' | ||
|
||
export const parseConfigFile = configFileRelativePath => { | ||
const absolutePath = path.resolve(configFileRelativePath) | ||
return require(require.resolve(absolutePath)) | ||
} |
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,111 @@ | ||
import getConfig, { getBaseConfigs } from '../getConfig' | ||
|
||
import CONFIG_FILE_CONFIGS from './mock.config' | ||
|
||
describe('getBaseConfigs', () => { | ||
const oldEnv = process.env | ||
const oldArgv = process.argv | ||
|
||
const ENV_VARIABLE_CONFIGS = { | ||
COMMITWATCH_GITHUB_TOKEN: 'COMMITWATCH_GITHUB_TOKEN_ENV', | ||
CI_REPO_OWNER: 'CI_REPO_OWNER_ENV', | ||
CI_REPO_NAME: 'CI_REPO_NAME_ENV', | ||
CI_COMMIT_SHA: 'CI_COMMIT_SHA_ENV', | ||
CI_BASE_BRANCH: 'CI_BASE_BRANCH_ENV', | ||
COMMIT_WATCH_OUTPUT_DIR: 'COMMIT_WATCH_OUTPUT_DIR_ENV', | ||
COMMIT_WATCH_OUTPUT_FILENAME: 'COMMIT_WATCH_OUTPUT_FILENAME_ENV', | ||
VERBOSE: 'VERBOSE_ENV', | ||
} | ||
|
||
const fileConfigToConfig = fileConfig => | ||
Object.keys(fileConfig).reduce( | ||
(acc, currKey) => ({ | ||
...acc, | ||
[currKey.replace(/([A-Z])/g, '_$1').toUpperCase()]: fileConfig[ | ||
currKey | ||
], | ||
}), | ||
{}, | ||
) | ||
|
||
beforeAll(() => { | ||
process.env = ENV_VARIABLE_CONFIGS | ||
}) | ||
|
||
afterEach(() => { | ||
process.argv = oldArgv | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = oldEnv | ||
}) | ||
|
||
it('Uses environment variables as config \ | ||
if not provided config file or cli configs', () => { | ||
expect(getBaseConfigs()).toEqual({ | ||
GITHUB_TOKEN: ENV_VARIABLE_CONFIGS.COMMITWATCH_GITHUB_TOKEN, | ||
OUTPUT_DIR: ENV_VARIABLE_CONFIGS.COMMIT_WATCH_OUTPUT_DIR, | ||
OUTPUT_FILENAME: ENV_VARIABLE_CONFIGS.COMMIT_WATCH_OUTPUT_FILENAME, | ||
VERBOSE: ENV_VARIABLE_CONFIGS.VERBOSE, | ||
CI_BASE_BRANCH: ENV_VARIABLE_CONFIGS.CI_BASE_BRANCH, | ||
CI_COMMIT_SHA: ENV_VARIABLE_CONFIGS.CI_COMMIT_SHA, | ||
CI_REPO_OWNER: ENV_VARIABLE_CONFIGS.CI_REPO_OWNER, | ||
CI_REPO_NAME: ENV_VARIABLE_CONFIGS.CI_REPO_NAME, | ||
}) | ||
}) | ||
|
||
it('Values from env variables are overwritten when a \ | ||
values from config file are available', () => { | ||
process.argv = ['_', '_', '--config-file', 'src/tests/mock.config.js'] | ||
|
||
expect(getBaseConfigs()).toEqual({ | ||
...fileConfigToConfig(CONFIG_FILE_CONFIGS), | ||
GITHUB_TOKEN: ENV_VARIABLE_CONFIGS.COMMITWATCH_GITHUB_TOKEN, | ||
}) | ||
}) | ||
|
||
it('Config values passed in as cli args overwrite\ | ||
values from config file and values from env variables', () => { | ||
const mockGithubToken = 'MOCK_GITHUB_TOKEN' | ||
const mockOutputDir = 'MOCK_OUTPUT_DIR' | ||
|
||
process.argv = [ | ||
'_', | ||
'_', | ||
'--config-file', | ||
'src/tests/mock.config.js', | ||
'--github-token', | ||
mockGithubToken, | ||
'--output-dir', | ||
mockOutputDir, | ||
] | ||
|
||
expect(getBaseConfigs()).toEqual({ | ||
...fileConfigToConfig(CONFIG_FILE_CONFIGS), | ||
OUTPUT_DIR: mockOutputDir, | ||
GITHUB_TOKEN: mockGithubToken, | ||
}) | ||
}) | ||
|
||
it('Correctly retrieves the repo name and owner from the GitHub URL if\ | ||
no repo name and owner is given', () => { | ||
const mockRepoOwner = 'EXAMPLE_CORP' | ||
const mockRepoName = 'EXAMPLE_REPO' | ||
const mockRepo = `${mockRepoOwner}/${mockRepoName}` | ||
|
||
process.env = { | ||
...ENV_VARIABLE_CONFIGS, | ||
CI_REPO_OWNER: undefined, | ||
CI_REPO_NAME: undefined, | ||
GIT_URL: `git@github.com:${mockRepo}.git`, | ||
} | ||
|
||
expect(getConfig()).toEqual({ | ||
commitSha: ENV_VARIABLE_CONFIGS.CI_COMMIT_SHA, | ||
baseBranch: ENV_VARIABLE_CONFIGS.CI_BASE_BRANCH, | ||
githubAccessToken: ENV_VARIABLE_CONFIGS.COMMITWATCH_GITHUB_TOKEN, | ||
repoOwner: mockRepoOwner, | ||
repoName: mockRepoName, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.