Skip to content

Commit

Permalink
feat(cache/repository): add modification query function to the reposi…
Browse files Browse the repository at this point in the history
…tory cache interface (#17529)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
Gabriel-Ladzaretti and viceice committed Sep 3, 2022
1 parent 0ed0ece commit 4ccd085
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/util/cache/repository/impl/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,12 @@ export abstract class RepoCacheBase implements RepoCache {
getData(): RepoCacheData {
return this.data;
}

isModified(): boolean | undefined {
const jsonStr = JSON.stringify(this.data);
if (!this.oldHash) {
return undefined;
}
return hasha(jsonStr) !== this.oldHash;
}
}
11 changes: 10 additions & 1 deletion lib/util/cache/repository/impl/local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('util/cache/repository/impl/local', () => {
'local'
);
expect(localRepoCache.getData()).toBeEmpty();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('skip when receives non-string data', async () => {
Expand All @@ -61,6 +62,7 @@ describe('util/cache/repository/impl/local', () => {
expect(logger.debug).toHaveBeenCalledWith(
"RepoCacheBase.load() - expecting data of type 'string' received 'undefined' instead - skipping"
);
expect(localRepoCache.isModified()).toBeUndefined();
});

it('skip when not found', async () => {
Expand All @@ -71,6 +73,7 @@ describe('util/cache/repository/impl/local', () => {
);
await localRepoCache.load(); // readCacheFile is mocked but has no return value set - therefore returns undefined
expect(logger.debug).not.toHaveBeenCalledWith();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('loads previously stored cache from disk', async () => {
Expand All @@ -86,6 +89,7 @@ describe('util/cache/repository/impl/local', () => {
await localRepoCache.load();

expect(localRepoCache.getData()).toEqual(data);
expect(localRepoCache.isModified()).toBeFalse();
});

it('resets if fingerprint does not match', async () => {
Expand Down Expand Up @@ -210,8 +214,8 @@ describe('util/cache/repository/impl/local', () => {
);

await localRepoCache.load();

expect(localRepoCache.getData()).toBeEmpty();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('handles file read error', async () => {
Expand All @@ -226,6 +230,7 @@ describe('util/cache/repository/impl/local', () => {

const data = localRepoCache.getData();
expect(data).toBeEmpty();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('handles invalid json', async () => {
Expand All @@ -239,6 +244,7 @@ describe('util/cache/repository/impl/local', () => {
await localRepoCache.load();

expect(localRepoCache.getData()).toBeEmpty();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('resets if repository does not match', async () => {
Expand All @@ -253,6 +259,7 @@ describe('util/cache/repository/impl/local', () => {
await localRepoCache.load();

expect(localRepoCache.getData()).toBeEmpty();
expect(localRepoCache.isModified()).toBeUndefined();
});

it('saves modified cache data to file', async () => {
Expand All @@ -275,6 +282,7 @@ describe('util/cache/repository/impl/local', () => {
semanticCommits: 'disabled',
});
expect(localRepoCache instanceof RepoCacheLocal).toBeTrue();
expect(localRepoCache.isModified()).toBeTrue();
expect(logger.warn).toHaveBeenCalledWith(
{ cacheType },
`Repository cache type not supported using type "local" instead`
Expand All @@ -299,6 +307,7 @@ describe('util/cache/repository/impl/local', () => {

await localRepoCache.load();
expect(localRepoCache.getData()).toEqual({ semanticCommits: 'enabled' });
expect(localRepoCache.isModified()).toBeFalse();

await localRepoCache.save();

Expand Down
4 changes: 4 additions & 0 deletions lib/util/cache/repository/impl/null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export class RepoCacheNull implements RepoCache {
getData(): RepoCacheData {
return this.data;
}

isModified(): boolean | undefined {
return undefined;
}
}
5 changes: 4 additions & 1 deletion lib/util/cache/repository/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GlobalConfig } from '../../../config/global';
import * as _fs from '../../fs';
import { initRepoCache } from './init';
import type { RepoCacheConfig } from './types';
import { getCache, resetCache, saveCache } from '.';
import { getCache, isCacheModified, resetCache, saveCache } from '.';

jest.mock('../../fs');

Expand All @@ -26,18 +26,21 @@ describe('util/cache/repository/index', () => {
await initRepoCache({ ...config, repositoryCache: 'disabled' });
expect(fs.readCacheFile).not.toHaveBeenCalled();
expect(getCache()).toBeEmpty();
expect(isCacheModified()).toBeUndefined();
});

it('saves cache', async () => {
await initRepoCache({ ...config, repositoryCache: 'enabled' });
await saveCache();
expect(fs.outputCacheFile).toHaveBeenCalled();
expect(isCacheModified()).toBeUndefined();
});

it('resets cache', async () => {
await initRepoCache({ ...config, repositoryCache: 'reset' });
expect(fs.readCacheFile).not.toHaveBeenCalled();
expect(fs.outputCacheFile).toHaveBeenCalled();
expect(getCache()).toBeEmpty();
expect(isCacheModified()).toBeUndefined();
});
});
4 changes: 4 additions & 0 deletions lib/util/cache/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export function getCache(): RepoCacheData {
export async function saveCache(): Promise<void> {
await repoCache.save();
}

export function isCacheModified(): boolean | undefined {
return repoCache.isModified();
}
1 change: 1 addition & 0 deletions lib/util/cache/repository/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface RepoCache {
load(): Promise<void>;
save(): Promise<void>;
getData(): RepoCacheData;
isModified(): boolean | undefined;
}

export interface RepoCacheConfig {
Expand Down

0 comments on commit 4ccd085

Please sign in to comment.