Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): detect missing RENOVATE_CONFIG_FILE #13196

Merged
merged 9 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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