Skip to content

Commit

Permalink
fix(config): detect missing RENOVATE_CONFIG_FILE (#13196)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
olegkrivtsov and viceice authored Dec 22, 2021
1 parent 7d60807 commit dad3863
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
module.exports = {
endpoint: CI_API_V4_URL
};
24 changes: 24 additions & 0 deletions lib/workers/global/config/parse/file.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import fsExtra from 'fs-extra';
import { DirectoryResult, dir } from 'tmp-promise';
import upath from 'upath';
import { logger } from '../../../../logger';
Expand Down Expand Up @@ -92,6 +93,29 @@ describe('workers/global/config/parse/file', () => {
expect(mockProcessExit).toHaveBeenCalledWith(1);
});

it('fatal error and exit if config.js contains unresolved env var', async () => {
const mockProcessExit = jest
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);

const configFile = upath.resolve(
__dirname,
'./__fixtures__/config-ref-error.js-invalid'
);
const tmpDir = tmp.path;
await fsExtra.ensureDir(tmpDir);

const tmpConfigFile = upath.resolve(tmpDir, 'config-ref-error.js');
await fsExtra.copy(configFile, tmpConfigFile);

await file.getConfig({ RENOVATE_CONFIG_FILE: tmpConfigFile });

expect(logger.fatal).toHaveBeenCalledWith(
`Error parsing config file due to unresolved variable(s): CI_API_V4_URL is not defined`
);
expect(mockProcessExit).toHaveBeenCalledWith(1);
});

it.each([
['invalid config file type', './file.txt'],
['missing config file type', './file'],
Expand Down
15 changes: 15 additions & 0 deletions lib/workers/global/config/parse/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import * as fs from 'fs-extra';
import { load } from 'js-yaml';
import JSON5 from 'json5';
import upath from 'upath';
Expand Down Expand Up @@ -36,6 +37,15 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
if (!upath.isAbsolute(configFile)) {
configFile = `${process.cwd()}/${configFile}`;
}

if (env.RENOVATE_CONFIG_FILE && !(await fs.pathExists(configFile))) {
logger.fatal(
{ configFile },
`Custom config file specified in RENOVATE_CONFIG_FILE must exist`
);
process.exit(1);
}

logger.debug('Checking for config file in ' + configFile);
let config: AllConfig = {};
try {
Expand All @@ -45,6 +55,11 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
if (err instanceof SyntaxError || err instanceof TypeError) {
logger.fatal(`Could not parse config file \n ${err.stack}`);
process.exit(1);
} else if (err instanceof ReferenceError) {
logger.fatal(
`Error parsing config file due to unresolved variable(s): ${err.message}`
);
process.exit(1);
} else if (err.message === 'Unsupported file type') {
logger.fatal(err.message);
process.exit(1);
Expand Down

0 comments on commit dad3863

Please sign in to comment.