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: better branch code coverage #24274

Merged
merged 1 commit into from
Sep 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ jobs:
- name: Check coverage threshold
run: |
pnpm nyc check-coverage -t ./coverage/nyc \
--branches 98.99 \
--branches 99.4 \
--functions 100 \
--lines 100 \
--statements 100
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/datasource/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ export class GoDatasource extends Datasource {

switch (source.datasource) {
case GitTagsDatasource.id: {
return this.direct.git.getDigest?.(source, tag) ?? null;
return this.direct.git.getDigest(source, tag);
}
case GithubTagsDatasource.id: {
return this.direct.github.getDigest(source, tag);
}
case BitbucketTagsDatasource.id: {
return this.direct.bitbucket.getDigest?.(source, tag) ?? null;
return this.direct.bitbucket.getDigest(source, tag);
}
case GitlabTagsDatasource.id: {
return this.direct.gitlab.getDigest?.(source, tag) ?? null;
return this.direct.gitlab.getDigest(source, tag);
}
/* istanbul ignore next: can never happen, makes lint happy */
default: {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/hexpm-bob/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class HexpmBobDatasource extends Datasource {
@cache({
namespace: `datasource-${datasource}`,
key: ({ registryUrl, packageName }: GetReleasesConfig) =>
`${registryUrl ?? defaultRegistryUrl}:${packageName}`,
`${registryUrl}:${packageName}`,
})
async getReleases({
registryUrl,
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/nuget/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function getReleases(
return null;
}

// istanbul ignore if: only happens when no stable version exists
// istanbul ignore next: only happens when no stable version exists
if (latestStable === null && catalogPages.length) {
const last = catalogEntries.pop()!;
latestStable = removeBuildMeta(last.version);
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/datasource/packagist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export class PackagistDatasource extends Datasource {
regFile: RegistryFile
): string {
const { key, hash } = regFile;
const fileName = hash ? key.replace('%hash%', hash) : key;
const fileName = hash
? key.replace('%hash%', hash)
: /* istanbul ignore next: hard to test */ key;
const url = resolveBaseUrl(regUrl, fileName);
return url;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/modules/datasource/pypi/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import url from 'node:url';
import changelogFilenameRegex from 'changelog-filename-regex';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { parse } from '../../../util/html';
import { regEx } from '../../../util/regex';
import { ensureTrailingSlash } from '../../../util/url';
Expand Down Expand Up @@ -148,7 +149,7 @@ export class PypiDatasource extends Datasource {
if (dep.releases) {
const versions = Object.keys(dep.releases);
dependency.releases = versions.map((version) => {
const releases = dep.releases?.[version] ?? [];
const releases = coerceArray(dep.releases?.[version]);
const { upload_time: releaseTimestamp } = releases[0] || {};
const isDeprecated = releases.some(({ yanked }) => yanked);
const result: Release = {
Expand Down Expand Up @@ -262,7 +263,7 @@ export class PypiDatasource extends Datasource {
}
const versions = Object.keys(releases);
dependency.releases = versions.map((version) => {
const versionReleases = releases[version] ?? [];
const versionReleases = coerceArray(releases[version]);
const isDeprecated = versionReleases.some(({ yanked }) => yanked);
const result: Release = { version };
if (isDeprecated) {
Expand Down
16 changes: 16 additions & 0 deletions lib/modules/datasource/repology/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getPkgReleases } from '..';
import { Fixtures } from '../../../../test/fixtures';
import * as httpMock from '../../../../test/http-mock';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import * as hostRules from '../../../util/host-rules';
import { id as versioning } from '../../versioning/loose';
import type { RepologyPackage } from './types';
import { RepologyDatasource } from './index';
Expand Down Expand Up @@ -56,6 +57,10 @@ const fixtureJdk = Fixtures.get(`openjdk.json`);
const fixturePython = Fixtures.get(`python.json`);

describe('modules/datasource/repology/index', () => {
beforeEach(() => {
hostRules.clear();
});

describe('getReleases', () => {
it('returns null for empty result', async () => {
mockResolverCall('debian_stable', 'nginx', 'binname', {
Expand Down Expand Up @@ -202,6 +207,17 @@ describe('modules/datasource/repology/index', () => {
).rejects.toThrow(EXTERNAL_HOST_ERROR);
});

it('throws on disabled host', async () => {
hostRules.add({ matchHost: repologyHost, enabled: false });
expect(
await getPkgReleases({
datasource,
versioning,
packageName: 'debian_stable/nginx',
})
).toBeNull();
});

it('returns correct version for binary package', async () => {
mockResolverCall('debian_stable', 'nginx', 'binname', {
status: 200,
Expand Down
1 change: 0 additions & 1 deletion lib/modules/datasource/repology/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ export class RepologyDatasource extends Datasource {
return { releases };
} catch (err) {
if (err.message === HOST_DISABLED) {
// istanbul ignore next
logger.trace({ packageName, err }, 'Host disabled');
} else {
logger.warn(
Expand Down
8 changes: 6 additions & 2 deletions lib/modules/manager/ansible-galaxy/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function interpretLine(
if (value?.startsWith('git@')) {
localDependency.packageName = value;
} else {
localDependency.registryUrls = value ? [value] : [];
localDependency.registryUrls = value
? [value]
: /* istanbul ignore next: should have test */ [];
}
break;
}
Expand Down Expand Up @@ -83,7 +85,9 @@ function handleGitDep(
function handleGalaxyDep(dep: AnsibleGalaxyPackageDependency): void {
dep.datasource = GalaxyCollectionDatasource.id;
dep.depName = dep.managerData.name;
dep.registryUrls = dep.managerData.source ? [dep.managerData.source] : [];
dep.registryUrls = dep.managerData.source
? /* istanbul ignore next: should have test */ [dep.managerData.source]
: [];
dep.currentValue = dep.managerData.version;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/cocoapods/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { quote } from 'shlex';
import upath from 'upath';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { exec } from '../../../util/exec';
import type { ExecOptions } from '../../../util/exec/types';
import {
Expand Down Expand Up @@ -134,7 +135,7 @@ export async function updateArtifacts({
});
}
}
for (const f of status.deleted || []) {
for (const f of coerceArray(status.deleted)) {
res.push({
file: {
type: 'deletion',
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/cocoapods/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from '../../../logger';
import { getSiblingFileName, localPathExists } from '../../../util/fs';
import { newlineRegex, regEx } from '../../../util/regex';
import { coerceString } from '../../../util/string';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
Expand Down Expand Up @@ -54,7 +55,7 @@ export function gitDep(parsedLine: ParsedLine): PackageDependency | null {

const platformMatch = regEx(
/[@/](?<platform>github|gitlab)\.com[:/](?<account>[^/]+)\/(?<repo>[^/]+)/
).exec(git ?? '');
).exec(coerceString(git));

if (platformMatch?.groups) {
const { account, repo, platform } = platformMatch.groups;
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/manager/gradle/extract/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ function extractDependency({
: null;
if (isArtifactDescriptor(descriptor)) {
const { group, name } = descriptor;
const groupName = is.nullOrUndefined(versionRef) ? group : versionRef; // usage of common variable should have higher priority than other values
const groupName = is.nullOrUndefined(versionRef)
? group
: /* istanbul ignore next: hard to test */ versionRef; // usage of common variable should have higher priority than other values
return {
depName: `${group}:${name}`,
groupName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from '../../../../logger';
import * as fs from '../../../../util/fs';
import { newlineRegex, regEx } from '../../../../util/regex';
import { coerceString } from '../../../../util/string';
import type { PackageDependency } from '../../types';
import type { GradleManagerData } from '../types';
import { isDependencyString, versionLikeSubstring } from '../utils';
Expand Down Expand Up @@ -59,9 +60,9 @@ export function parseGcv(
propsFileName: string,
fileContents: Record<string, string | null>
): PackageDependency<GradleManagerData>[] {
const propsFileContent = fileContents[propsFileName] ?? '';
const propsFileContent = coerceString(fileContents[propsFileName]);
const lockFileName = fs.getSiblingFileName(propsFileName, VERSIONS_LOCK);
const lockFileContent = fileContents[lockFileName] ?? '';
const lockFileContent = coerceString(fileContents[lockFileName]);
const lockFileMap = parseLockFile(lockFileContent);
const [propsFileExactMap, propsFileRegexMap] =
parsePropsFile(propsFileContent);
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/maven-wrapper/extract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { newlineRegex, regEx } from '../../../util/regex';
import { MavenDatasource } from '../../datasource/maven';
import { id as versioning } from '../../versioning/maven';
Expand All @@ -15,7 +16,7 @@ const WRAPPER_URL_REGEX = regEx(
);

function extractVersions(fileContent: string): MavenVersionExtract {
const lines = fileContent?.split(newlineRegex) ?? [];
const lines = coerceArray(fileContent?.split(newlineRegex));
const maven = extractLineInfo(lines, DISTRIBUTION_URL_REGEX) ?? undefined;
const wrapper = extractLineInfo(lines, WRAPPER_URL_REGEX) ?? undefined;
return { maven, wrapper };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export async function updateLockedDependency(
logger.debug(
`${depName} can be updated to ${newVersion} in-range with matching constraint "${constraint}" in ${
// TODO: types (#22198)
parentDepName ? `${parentDepName}@${parentVersion!}` : packageFile
parentDepName
? `${parentDepName}@${parentVersion!}`
: /* istanbul ignore next: hard to test */ packageFile
}`
);
} else if (parentDepName && parentVersion) {
Expand Down Expand Up @@ -242,7 +244,8 @@ export async function updateLockedDependency(
newPackageJsonContent =
parentUpdateResult.files[packageFile] || newPackageJsonContent;
newLockFileContent =
parentUpdateResult.files[lockFile] || newLockFileContent;
parentUpdateResult.files[lockFile] ||
/* istanbul ignore next: hard to test */ newLockFileContent;
}
const files: Record<string, string> = {};
if (newLockFileContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,22 @@ provider "registry.terraform.io/hashicorp/azurerm" {
}

provider "registry.terraform.io/hashicorp/random" {
version = "2.2.2"
version = "2.2.1"
constraints = "~> 2.2"
hashes = [
"h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=",
"h1:6zB2hX7YIOW26OrKsLJn0uLMnjqbPNxcz9RhlWEuuSY=",
"h1:Zg1Bpi6vr7b0H6no8kVDfEucn5pvNALivdrVKVHarGs=",
"zh:072ce92b0138ee65df2e4e2e6e5f6632fa12a7e6453b91399bad89291855d426",
"zh:5731987fe61051515f449033e456ee55207caf17ef41096eb82247810585f53b",
"zh:6f18b10175708bb5839e1f2082dcc02651b876786cd54ec415a091f3821807c3",
"zh:7fa7737661380d18cba3cdc71c4ec6f2fd281b9d61112f6b48d06ca8bbf97771",
"zh:8466cb8fbb4de887b23039082a6e3dc85aeabce86dd808e2a7a65e4e1c51dbae",
"zh:888c63417701c13bbe785ab11dc690d4803e6a2156318cf188970b7b6400b99e",
"zh:a231df55d36fbad1a6705f5d3be4f7459a73ec76117d13f22aa83c10fc610278",
"zh:b62d9a4cd64a2d229070260f4abfef476ebbd7c5511b43e9cdccf23ce938f630",
"zh:b6bd1a325f909bb93f7c9bef00eb306bef1e406cbdf557901d755a3e7a4a5448",
"zh:b9f59afc23cc5567075f76313214baa1e5ce909325229e23c9a4666f7b26e7f7",
"zh:d040220c09b8d9d6bd937572bd5b14bc069af2b883185a873460530d8a1de6e6",
"zh:f254c1f943eb016ae07ebe91b23f813dc79f2064616c65f98c8f64ce23be90c4",
]
}
",
Expand All @@ -114,11 +125,6 @@ exports[`modules/manager/terraform/lockfile/index do full lock file maintenance
"hashicorp/azurerm",
"2.56.0",
],
[
"https://registry.terraform.io",
"hashicorp/random",
"2.2.2",
],
]
`;

Expand Down
18 changes: 4 additions & 14 deletions lib/modules/manager/terraform/lockfile/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,20 +439,10 @@ describe('modules/manager/terraform/lockfile/index', () => {
},
],
})
.mockResolvedValueOnce({
.mockResolvedValueOnce(
// random
releases: [
{
version: '2.2.1',
},
{
version: '2.2.2',
},
{
version: '3.0.0',
},
],
});
null
);
mockHash.mockResolvedValue([
'h1:lDsKRxDRXPEzA4AxkK4t+lJd3IQIP2UoaplJGjQSp2s=',
'h1:6zB2hX7YIOW26OrKsLJn0uLMnjqbPNxcz9RhlWEuuSY=',
Expand Down Expand Up @@ -480,7 +470,7 @@ describe('modules/manager/terraform/lockfile/index', () => {
})
);

expect(mockHash.mock.calls).toBeArrayOfSize(2);
expect(mockHash.mock.calls).toBeArrayOfSize(1);
expect(mockHash.mock.calls).toMatchSnapshot();
});

Expand Down
1 change: 0 additions & 1 deletion lib/modules/manager/terraform/lockfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async function updateAllLocks(
packageName: lock.packageName,
};
const { releases } = (await getPkgReleases(updateConfig)) ?? {};
// istanbul ignore if: needs test
if (!releases) {
return null;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/modules/manager/terraform/lockfile/update-locked.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../../../../logger';
import { coerceString } from '../../../../util/string';
import type { UpdateLockedConfig, UpdateLockedResult } from '../../types';
import { extractLocks } from './util';

Expand All @@ -12,8 +13,10 @@ export function updateLockedDependency(
`terraform.updateLockedDependency: ${depName}@${currentVersion} -> ${newVersion} [${lockFile}]`
);
try {
const locked = extractLocks(lockFileContent ?? '');
const lockedDep = locked?.find((dep) => dep.packageName === depName ?? '');
const locked = extractLocks(coerceString(lockFileContent));
const lockedDep = locked?.find(
(dep) => dep.packageName === coerceString(depName)
);
if (lockedDep?.version === newVersion) {
return { status: 'already-updated' };
}
Expand Down
7 changes: 4 additions & 3 deletions lib/modules/platform/github/massage-markdown-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Content } from 'mdast';
import remark from 'remark';
import type { Plugin, Transformer } from 'unified';
import { logger } from '../../../logger';
import { coerceNumber } from '../../../util/number';
import { regEx } from '../../../util/regex';

interface UrlMatch {
Expand All @@ -20,8 +21,8 @@ function massageLink(input: string): string {

function collectLinkPosition(input: string, matches: UrlMatch[]): Plugin {
const transformer = (tree: Content): void => {
const startOffset: number = tree.position?.start.offset ?? 0;
const endOffset: number = tree.position?.end.offset ?? 0;
const startOffset = coerceNumber(tree.position?.start.offset);
const endOffset = coerceNumber(tree.position?.end.offset);

if (tree.type === 'link') {
const substr = input.slice(startOffset, endOffset);
Expand All @@ -39,7 +40,7 @@ function collectLinkPosition(input: string, matches: UrlMatch[]): Plugin {
const urlMatches = [...tree.value.matchAll(globalUrlReg)];
for (const match of urlMatches) {
const [url] = match;
const start = startOffset + (match.index ?? 0);
const start = startOffset + coerceNumber(match.index);
const end = start + url.length;
const newUrl = massageLink(url);
matches.push({ start, end, replaceTo: `[${url}](${newUrl})` });
Expand Down
Loading