diff --git a/lib/workers/repository/init/merge.spec.ts b/lib/workers/repository/init/merge.spec.ts index 0c29c095427f392f15d32c4b3a7895afcc98fb5c..5a03b80b845cede58bddd859aa99787a14f2190b 100644 --- a/lib/workers/repository/init/merge.spec.ts +++ b/lib/workers/repository/init/merge.spec.ts @@ -3,12 +3,16 @@ import { fs, getConfig, git, + logger, mocked, + partial, platform, } from '../../../../test/util'; import * as _migrateAndValidate from '../../../config/migrate-validate'; import * as _migrate from '../../../config/migration'; +import * as repoCache from '../../../util/cache/repository'; import { initRepoCache } from '../../../util/cache/repository/init'; +import type { RepoCacheData } from '../../../util/cache/repository/types'; import { checkForRepoConfigError, detectRepoFileConfig, @@ -45,6 +49,21 @@ describe('workers/repository/init/merge', () => { expect(await detectRepoFileConfig()).toEqual({}); }); + it('returns config if not found - uses cache', async () => { + jest + .spyOn(repoCache, 'getCache') + .mockReturnValueOnce( + partial<RepoCacheData>({ configFileName: 'renovate.json' }) + ); + platform.getRawFile.mockRejectedValueOnce(new Error()); + git.getFileList.mockResolvedValue(['package.json']); + fs.readLocalFile.mockResolvedValue('{}'); + expect(await detectRepoFileConfig()).toEqual({}); + expect(logger.logger.debug).toHaveBeenCalledWith( + 'Existing config file no longer exists' + ); + }); + it('uses package.json config if found', async () => { git.getFileList.mockResolvedValue(['package.json']); const pJson = JSON.stringify({ diff --git a/lib/workers/repository/init/merge.ts b/lib/workers/repository/init/merge.ts index cf4dd8106b9b55723173d73c0209e90637dac754..04c3cba0e61cba46c77369278c05222d4b263e50 100644 --- a/lib/workers/repository/init/merge.ts +++ b/lib/workers/repository/init/merge.ts @@ -17,6 +17,7 @@ import { import { logger } from '../../../logger'; import * as npmApi from '../../../modules/datasource/npm'; import { platform } from '../../../modules/platform'; +import { ExternalHostError } from '../../../types/errors/external-host-error'; import { getCache } from '../../../util/cache/repository'; import { readLocalFile } from '../../../util/fs'; import { getFileList } from '../../../util/git'; @@ -51,7 +52,16 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> { const cache = getCache(); let { configFileName } = cache; if (configFileName) { - const configFileRaw = await platform.getRawFile(configFileName); + let configFileRaw: string | null; + try { + configFileRaw = await platform.getRawFile(configFileName); + } catch (err) { + // istanbul ignore if + if (err instanceof ExternalHostError) { + throw err; + } + configFileRaw = null; + } if (configFileRaw) { let configFileParsed = JSON5.parse(configFileRaw); if (configFileName !== 'package.json') { @@ -61,6 +71,7 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> { return { configFileName, configFileParsed }; // don't return raw 'package.json' } else { logger.debug('Existing config file no longer exists'); + delete cache.configFileName; } } configFileName = (await detectConfigFile()) ?? undefined;