Skip to content
Snippets Groups Projects
Unverified Commit f93927af authored by Gabriel-Ladzaretti's avatar Gabriel-Ladzaretti Committed by GitHub
Browse files

refactor(repo/cache): handle data parsing inside repository base class (#17346)

parent f0702b49
No related branches found
No related tags found
No related merge requests found
import { promisify } from 'util';
import zlib from 'zlib';
import is from '@sindresorhus/is';
import hasha from 'hasha';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
......@@ -27,7 +28,14 @@ export abstract class RepoCacheBase implements RepoCache {
async load(): Promise<void> {
try {
const oldCache = await this.read();
const data = await this.read();
if (!is.string(data)) {
logger.debug(
`RepoCacheBase.load() - expecting data of type 'string' received '${typeof data}' instead - skipping`
);
return;
}
const oldCache = JSON.parse(data);
if (isValidRev12(oldCache, this.repository)) {
const compressed = Buffer.from(oldCache.payload, 'base64');
......
......@@ -35,6 +35,14 @@ describe('util/cache/repository/impl/local', () => {
expect(localRepoCache.getData()).toBeEmpty();
});
it('skip when receives non-string data', async () => {
const localRepoCache = CacheFactory.get('some/repo', 'local');
await localRepoCache.load(); // readCacheFile is mocked but has no return value set - therefore returns undefined
expect(logger.debug).toHaveBeenCalledWith(
"RepoCacheBase.load() - expecting data of type 'string' received 'undefined' instead - skipping"
);
});
it('loads previously stored cache from disk', async () => {
const data: RepoCacheData = { semanticCommits: 'enabled' };
const cacheRecord = await createCacheRecord(data);
......
......@@ -12,15 +12,12 @@ export class RepoCacheLocal extends RepoCacheBase {
protected async read(): Promise<unknown> {
const cacheFileName = this.getCacheFileName();
let data: unknown;
try {
const rawCache = await readCacheFile(cacheFileName, 'utf8');
data = JSON.parse(rawCache);
return await readCacheFile(cacheFileName, 'utf8');
} catch (err) {
logger.debug({ cacheFileName }, 'Repository local cache not found');
throw err;
}
return data;
}
protected async write(data: RepoCacheRecord): Promise<void> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment