From 5d9eeb030cc253ef4f84f418a094726024d002d2 Mon Sep 17 00:00:00 2001
From: Wei Kang <weikangchia@gmail.com>
Date: Sun, 29 Aug 2021 19:23:49 +0800
Subject: [PATCH] feat: fatal error config not found (#11462)

* 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>
---
 .../config/parse/__fixtures__/default.js      |  2 +
 lib/workers/global/config/parse/file.spec.ts  | 38 +++++++++++--------
 lib/workers/global/config/parse/file.ts       |  8 +++-
 lib/workers/global/config/parse/index.spec.ts |  9 ++++-
 4 files changed, 38 insertions(+), 19 deletions(-)
 create mode 100644 lib/workers/global/config/parse/__fixtures__/default.js

diff --git a/lib/workers/global/config/parse/__fixtures__/default.js b/lib/workers/global/config/parse/__fixtures__/default.js
new file mode 100644
index 0000000000..3a4722d840
--- /dev/null
+++ b/lib/workers/global/config/parse/__fixtures__/default.js
@@ -0,0 +1,2 @@
+// @ts-ignore
+module.exports = {};
diff --git a/lib/workers/global/config/parse/file.spec.ts b/lib/workers/global/config/parse/file.spec.ts
index f0c3464807..a05c6f3032 100644
--- a/lib/workers/global/config/parse/file.spec.ts
+++ b/lib/workers/global/config/parse/file.spec.ts
@@ -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(
@@ -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",
@@ -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);
+    });
   });
 });
diff --git a/lib/workers/global/config/parse/file.ts b/lib/workers/global/config/parse/file.ts
index c43544d868..bf0592ec02 100644
--- a/lib/workers/global/config/parse/file.ts
+++ b/lib/workers/global/config/parse/file.ts
@@ -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) {
diff --git a/lib/workers/global/config/parse/index.spec.ts b/lib/workers/global/config/parse/index.spec.ts
index cb27212318..8dd3942f23 100644
--- a/lib/workers/global/config/parse/index.spec.ts
+++ b/lib/workers/global/config/parse/index.spec.ts
@@ -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 () => {
@@ -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',
-- 
GitLab