Skip to content

Commit

Permalink
test: Add tests for datasource index-level caching (#30153)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 12, 2024
1 parent 4aca729 commit 34478a6
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions lib/modules/datasource/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fs from 'fs-extra';
import { logger } from '../../../test/util';
import { GlobalConfig } from '../../config/global';
import {
EXTERNAL_HOST_ERROR,
HOST_DISABLED,
} from '../../constants/error-messages';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as _packageCache from '../../util/cache/package';
import { loadModules } from '../../util/modules';
import datasources from './api';
import { getDefaultVersioning } from './common';
Expand Down Expand Up @@ -129,6 +131,9 @@ jest.mock('./metadata-manual', () => ({
},
}));

jest.mock('../../util/cache/package');
const packageCache = _packageCache as jest.Mocked<typeof _packageCache>;

describe('modules/datasource/index', () => {
afterEach(() => {
datasources.delete(datasource);
Expand Down Expand Up @@ -573,6 +578,86 @@ describe('modules/datasource/index', () => {
});
});

describe('Cache', () => {
afterAll(() => {
GlobalConfig.reset();
});

class CachingDatasource extends DummyDatasource {
override caching = true;
}

it('caches by default', async () => {
const registries = {
'https://reg1.com': {
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
},
} satisfies RegistriesMock;
datasources.set(datasource, new CachingDatasource(registries));

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: ['https://reg1.com'],
});
expect(res).toMatchObject({
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
});
expect(packageCache.set).toHaveBeenCalledWith(
'datasource-releases',
'dummy https://reg1.com package',
{
changelogUrl: 'https://foo.bar/package/CHANGELOG.md',
registryUrl: 'https://reg1.com',
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
sourceUrl: 'https://foo.bar/package',
},
15,
);
});

it('skips cache when isPrivate=true', async () => {
const registries = {
'https://reg1.com': {
isPrivate: true,
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
},
} satisfies RegistriesMock;
datasources.set(datasource, new CachingDatasource(registries));

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: ['https://reg1.com'],
});
expect(res).toMatchObject({
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
});
expect(packageCache.set).not.toHaveBeenCalledWith();
});

it('forces cache via GlobalConfig', async () => {
GlobalConfig.set({ cachePrivatePackages: true });
const registries = {
'https://reg1.com': {
isPrivate: true,
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
},
} satisfies RegistriesMock;
datasources.set(datasource, new CachingDatasource(registries));

const res = await getPkgReleases({
datasource,
packageName,
registryUrls: ['https://reg1.com'],
});
expect(res).toMatchObject({
releases: [{ version: '0.0.1' }, { version: '0.0.2' }],
});
expect(packageCache.set).toHaveBeenCalledOnce();
});
});

it('merges registries and aborts on ExternalHostError', async () => {
await expect(
getPkgReleases({
Expand Down

0 comments on commit 34478a6

Please sign in to comment.