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: cache validation #6644

Merged
merged 1 commit into from Jul 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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