Skip to content

Commit

Permalink
fix: cache validation (#6644)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jul 1, 2020
1 parent 7af01c5 commit a8caa9e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 13 additions & 2 deletions lib/util/cache/repository/index.spec.ts
Expand Up @@ -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',
Expand All @@ -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' });
});
});
23 changes: 16 additions & 7 deletions lib/util/cache/repository/index.ts
Expand Up @@ -11,6 +11,7 @@ export interface BaseBranchCache {
}

export interface Cache {
repository?: string;
init?: {
configFile: string;
contents: RenovateConfig;
Expand All @@ -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<void> {
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 {
Expand Down

0 comments on commit a8caa9e

Please sign in to comment.