Skip to content

Commit

Permalink
feat: fatal error config not found (#11462)
Browse files Browse the repository at this point in the history
* fatal error and exit when config not found

* update file.ts

* fix fatal error and exit only for custom config file

* update test name to be clearer for custom file only

* revert changes for no config file found

* update test name to be clearer

* add space between test as suggested

* fix test due to invalid file

* fix typo

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
weikangchia and viceice committed Aug 29, 2021
1 parent 73a6b18 commit 5d9eeb0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/workers/global/config/parse/__fixtures__/default.js
@@ -0,0 +1,2 @@
// @ts-ignore
module.exports = {};
38 changes: 23 additions & 15 deletions lib/workers/global/config/parse/file.spec.ts
Expand Up @@ -16,11 +16,6 @@ describe('workers/global/config/parse/file', () => {
});

describe('.getConfig()', () => {
it('returns empty env', () => {
expect(file.getConfig({ RENOVATE_CONFIG_FILE: 'dummylocation' })).toEqual(
{}
);
});
it('parses custom config file', () => {
const configFile = upath.resolve(__dirname, './__fixtures__/file.js');
expect(file.getConfig({ RENOVATE_CONFIG_FILE: configFile })).toEqual(
Expand All @@ -33,7 +28,15 @@ describe('workers/global/config/parse/file', () => {
expect(res).toMatchSnapshot();
expect(res.rangeStrategy).toEqual('bump');
});
it('informs user when error in parsing config.js', () => {

it('parse and returns empty config if there is no RENOVATE_CONFIG_FILE in env', () => {
expect(file.getConfig({})).toEqual({});
});

it('fatal error and exit if error in parsing config.js', () => {
const mockProcessExit = jest
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
const configFile = upath.resolve(tmp.path, './file3.js');
const fileContent = `module.exports = {
"platform": "github",
Expand All @@ -47,16 +50,21 @@ describe('workers/global/config/parse/file', () => {
"repositories": [ "test/test" ],
};`;
fs.writeFileSync(configFile, fileContent, { encoding: 'utf8' });
expect(
file.getConfig({ RENOVATE_CONFIG_FILE: configFile })
).toStrictEqual({});
file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
expect(mockProcessExit).toHaveBeenCalledWith(1);

fs.unlinkSync(configFile);
});
});
it('handles when invalid file location is provided', () => {
const configFile = upath.resolve(tmp.path, './file4.js');
expect(file.getConfig({ RENOVATE_CONFIG_FILE: configFile })).toStrictEqual(
{}
);

it('fatal error and exit if custom config file does not exist', () => {
const mockProcessExit = jest
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);

const configFile = upath.resolve(tmp.path, './file4.js');
file.getConfig({ RENOVATE_CONFIG_FILE: configFile });

expect(mockProcessExit).toHaveBeenCalledWith(1);
});
});
});
8 changes: 6 additions & 2 deletions lib/workers/global/config/parse/file.ts
Expand Up @@ -18,9 +18,13 @@ export function getConfig(env: NodeJS.ProcessEnv): AllConfig {
if (err instanceof SyntaxError || err instanceof TypeError) {
logger.fatal(`Could not parse config file \n ${err.stack}`);
process.exit(1);
} else if (env.RENOVATE_CONFIG_FILE) {
logger.fatal('No custom config file found on disk');
process.exit(1);
} else {
// Do nothing
logger.debug('No config file found on disk - skipping');
}
// Do nothing
logger.debug('No config file found on disk - skipping');
}
const { isMigrated, migratedConfig } = migrateConfig(config);
if (isMigrated) {
Expand Down
9 changes: 7 additions & 2 deletions lib/workers/global/config/parse/index.spec.ts
Expand Up @@ -18,7 +18,12 @@ describe('workers/global/config/parse/index', () => {
jest.resetModules();
configParser = await import('./index');
defaultArgv = getArgv();
defaultEnv = { RENOVATE_CONFIG_FILE: 'abc' };
defaultEnv = {
RENOVATE_CONFIG_FILE: upath.resolve(
__dirname,
'./__fixtures__/default.js'
),
};
jest.mock('delay', () => Promise.resolve());
});
it('supports token in env', async () => {
Expand Down Expand Up @@ -85,7 +90,7 @@ describe('workers/global/config/parse/index', () => {

expect(parsedConfig).toContainEntries([['privateKey', expected]]);
});
it('supports Bitbucket username/passwod', async () => {
it('supports Bitbucket username/password', async () => {
defaultArgv = defaultArgv.concat([
'--platform=bitbucket',
'--username=user',
Expand Down

0 comments on commit 5d9eeb0

Please sign in to comment.