diff --git a/lib/util/cache/repository/index.spec.ts b/lib/util/cache/repository/index.spec.ts index b1b5d69dedb19c..d609f2d20ca026 100644 --- a/lib/util/cache/repository/index.spec.ts +++ b/lib/util/cache/repository/index.spec.ts @@ -7,6 +7,9 @@ jest.mock('fs-extra'); const fs = mocked(_fs); describe('lib/util/cache/repository', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); const config = { cacheDir: '/tmp/renovate/cache/', platform: 'github', @@ -23,17 +26,25 @@ describe('lib/util/cache/repository', () => { }); expect(fs.readFile.mock.calls).toHaveLength(0); }); - it('reads from cache and finalizes', async () => { + it('resets if invalid', async () => { fs.readFile.mockResolvedValueOnce('{}' as any); await repositoryCache.initialize({ ...config, repositoryCache: 'enabled', }); + expect(repositoryCache.getCache()).toEqual({ repository: 'abc/def' }); + }); + it('reads from cache and finalizes', async () => { + fs.readFile.mockResolvedValueOnce('{"repository":"abc/def"}' as any); + await repositoryCache.initialize({ + ...config, + repositoryCache: 'enabled', + }); await repositoryCache.finalize(); expect(fs.readFile.mock.calls).toHaveLength(1); expect(fs.outputFile.mock.calls).toHaveLength(1); }); it('gets', () => { - expect(repositoryCache.getCache()).toEqual({}); + expect(repositoryCache.getCache()).toEqual({ repository: 'abc/def' }); }); }); diff --git a/lib/util/cache/repository/index.ts b/lib/util/cache/repository/index.ts index 4ca55197b73d35..791017588c80eb 100644 --- a/lib/util/cache/repository/index.ts +++ b/lib/util/cache/repository/index.ts @@ -11,6 +11,7 @@ export interface BaseBranchCache { } export interface Cache { + repository?: string; init?: { configFile: string; contents: RenovateConfig; @@ -31,21 +32,29 @@ export function getCacheFileName(config: RenovateConfig): string { ); } +function validate(config: RenovateConfig, input: any): Cache | null { + if (input?.repository === config.repository) { + return input as Cache; + } + // reset + return null; +} + export async function initialize(config: RenovateConfig): Promise { + cache = null; try { cacheFileName = getCacheFileName(config); repositoryCache = config.repositoryCache; - if (repositoryCache !== 'enabled') { - logger.debug('Skipping repository cache'); - cache = {}; - return; + if (repositoryCache === 'enabled') { + cache = validate( + config, + JSON.parse(await fs.readFile(cacheFileName, 'utf8')) + ); } - cache = JSON.parse(await fs.readFile(cacheFileName, 'utf8')); - logger.debug({ cacheFileName }, 'Read repository cache'); } catch (err) { logger.debug({ cacheFileName }, 'No repository cache found'); - cache = {}; } + cache = cache || { repository: config.repository }; } export function getCache(): Cache {