Skip to content

Commit

Permalink
fix: improve branch coverage (#24282)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people committed Sep 8, 2023
1 parent 34a6e70 commit 8aeb64e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -404,7 +404,7 @@ jobs:
- name: Check coverage threshold
run: |
pnpm nyc check-coverage -t ./coverage/nyc \
--branches 99.57 \
--branches 99.61 \
--functions 100 \
--lines 100 \
--statements 100
Expand Down
3 changes: 0 additions & 3 deletions lib/config/migration.spec.ts
Expand Up @@ -669,9 +669,6 @@ describe('config/migration', () => {

it('it migrates gradle-lite', () => {
const config: RenovateConfig = {
gradle: {
enabled: false,
},
'gradle-lite': {
enabled: true,
fileMatch: ['foo'],
Expand Down
11 changes: 6 additions & 5 deletions lib/config/migrations/custom/package-rules-migration.ts
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import type { PackageRule } from '../../types';
import { AbstractMigration } from '../base/abstract-migration';

Expand Down Expand Up @@ -30,11 +31,11 @@ export class PackageRulesMigration extends AbstractMigration {
override readonly propertyName = 'packageRules';

override run(value: unknown): void {
let packageRules = (this.get('packageRules') as PackageRule[]) ?? [];
packageRules = Array.isArray(packageRules) ? [...packageRules] : [];
let packageRules = this.get('packageRules') as PackageRule[];
if (is.nonEmptyArray(packageRules)) {
packageRules = packageRules.map(renameKeys);

packageRules = packageRules.map(renameKeys);

this.rewrite(packageRules);
this.rewrite(packageRules);
}
}
}
10 changes: 10 additions & 0 deletions lib/modules/datasource/datasource.spec.ts
Expand Up @@ -33,4 +33,14 @@ describe('modules/datasource/datasource', () => {
testDatasource.getReleases(partial<GetReleasesConfig>())
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});

it('should throw on statusCode >=500 && <600', async () => {
const testDatasource = new TestDatasource();

httpMock.scope(exampleUrl).get('/').reply(504);

await expect(
testDatasource.getReleases(partial<GetReleasesConfig>())
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});
});
6 changes: 1 addition & 5 deletions lib/modules/datasource/datasource.ts
Expand Up @@ -46,11 +46,7 @@ export abstract class Datasource implements DatasourceApi {

const statusCode = err.response?.statusCode;
if (statusCode) {
if (statusCode === 429) {
throw new ExternalHostError(err);
}

if (statusCode >= 500 && statusCode < 600) {
if (statusCode === 429 || (statusCode >= 500 && statusCode < 600)) {
throw new ExternalHostError(err);
}
}
Expand Down
89 changes: 87 additions & 2 deletions lib/modules/datasource/index.spec.ts
Expand Up @@ -94,6 +94,28 @@ class DummyDatasource3 extends Datasource {
}
}

class DummyDatasource4 extends DummyDatasource3 {
override defaultRegistryUrls = undefined as never;
}

class DummyDatasource5 extends Datasource {
override registryStrategy = undefined as never;

constructor(private registriesMock: RegistriesMock = defaultRegistriesMock) {
super(datasource);
}

override getReleases({
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const fn = this.registriesMock[registryUrl!];
if (typeof fn === 'function') {
return Promise.resolve(fn());
}
return Promise.resolve(fn ?? null);
}
}

jest.mock('./metadata-manual', () => ({
manualChangelogUrls: {
dummy: {
Expand Down Expand Up @@ -313,6 +335,16 @@ describe('modules/datasource/index', () => {
});
});

// for coverage
it('undefined defaultRegistryUrls with customRegistrySupport works', async () => {
datasources.set(datasource, new DummyDatasource4());
const res = await getPkgReleases({
datasource,
packageName,
});
expect(res).toBeNull();
});

it('applies extractVersion', async () => {
const registries: RegistriesMock = {
'https://reg1.com': {
Expand Down Expand Up @@ -454,6 +486,7 @@ describe('modules/datasource/index', () => {
describe('merge', () => {
class MergeRegistriesDatasource extends DummyDatasource {
override readonly registryStrategy = 'merge';
override caching = true;
override readonly defaultRegistryUrls = [
'https://reg1.com',
'https://reg2.com',
Expand All @@ -472,6 +505,10 @@ describe('modules/datasource/index', () => {
'https://reg5.com': () => {
throw new Error('b');
},
// for coverage
'https://reg6.com': null,
// has the same result as reg1 url, to test de-deplication of releases
'https://reg7.com': () => ({ releases: [{ version: '1.0.0' }] }),
};

beforeEach(() => {
Expand All @@ -492,7 +529,7 @@ describe('modules/datasource/index', () => {
});
});

it('ignores custom defaultRegistryUrls if registrUrls are set', async () => {
it('ignores custom defaultRegistryUrls if registryUrls are set', async () => {
const res = await getPkgReleases({
datasource,
packageName,
Expand Down Expand Up @@ -522,6 +559,20 @@ describe('modules/datasource/index', () => {
});
});

it('filters out duplicate releases', async () => {
const res = await getPkgReleases({
datasource,
packageName,
registryUrls: ['https://reg1.com', 'https://reg7.com'],
});
expect(res).toMatchObject({
releases: [
{ registryUrl: 'https://reg1.com', version: '1.0.0' },
// { registryUrl: 'https://reg2.com', version: '1.0.0' },
],
});
});

it('merges registries and aborts on ExternalHostError', async () => {
await expect(
getPkgReleases({
Expand Down Expand Up @@ -614,11 +665,20 @@ describe('modules/datasource/index', () => {
it('returns null if no releases are found', async () => {
const registries: RegistriesMock = {
'https://reg1.com': () => {
throw new Error('a');
throw { statusCode: '404' };
},
'https://reg2.com': () => {
throw { statusCode: '401' };
},
'https://reg3.com': () => {
throw { statusCode: '403' };
},
'https://reg4.com': () => {
throw new Error('b');
},
'https://reg5.com': () => {
throw { code: '403' };
},
};
const registryUrls = Object.keys(registries);
datasources.set(datasource, new HuntRegistriyDatasource(registries));
Expand All @@ -631,6 +691,31 @@ describe('modules/datasource/index', () => {

expect(res).toBeNull();
});

it('defaults to hunt strategy', async () => {
const registries: RegistriesMock = {
'https://reg1.com': null,
'https://reg2.com': () => {
throw new Error('unknown');
},
'https://reg3.com': { releases: [{ version: '1.0.0' }] },
'https://reg4.com': { releases: [{ version: '2.0.0' }] },
'https://reg5.com': { releases: [{ version: '3.0.0' }] },
};
const registryUrls = Object.keys(registries);
datasources.set(datasource, new DummyDatasource5(registries));

const res = await getPkgReleases({
datasource,
packageName,
registryUrls,
});

expect(res).toMatchObject({
registryUrl: 'https://reg3.com',
releases: [{ version: '1.0.0' }],
});
});
});

describe('relaseConstraintFiltering', () => {
Expand Down
36 changes: 36 additions & 0 deletions lib/modules/datasource/metadata.spec.ts
@@ -1,9 +1,11 @@
import { partial } from '../../../test/util';
import { HelmDatasource } from './helm';
import { MavenDatasource } from './maven';
import {
addMetaData,
massageGithubUrl,
massageUrl,
normalizeDate,
shouldDeleteHomepage,
} from './metadata';
import { NpmDatasource } from './npm';
Expand Down Expand Up @@ -502,4 +504,38 @@ describe('modules/datasource/metadata', () => {
expect(shouldDeleteHomepage(sourceUrl, homepage)).toBe(expected);
}
);

// for coverage
it('should handle dep with no releases', () => {
const dep = partial<ReleaseResult>({});

const datasource = PypiDatasource.id;
const packageName = 'django';

addMetaData(dep, datasource, packageName);
expect(dep).toEqual({
changelogUrl:
'https://github.com/django/django/tree/master/docs/releases',
sourceUrl: 'https://github.com/django/django',
});
});

describe('normalizeDate()', () => {
it('works for number input', () => {
const now = Date.now();
expect(normalizeDate(now)).toBe(new Date(now).toISOString());
});

it('works for string input', () => {
expect(normalizeDate('2021-01-01')).toBe(
new Date('2021-01-01').toISOString()
);
});

it('works for Date instance', () => {
expect(normalizeDate(new Date('2021-01-01'))).toBe(
new Date('2021-01-01').toISOString()
);
});
});
});

0 comments on commit 8aeb64e

Please sign in to comment.