Files
Michael Kriese 3a723b7891 test(datasource): close branch coverage gaps (#45943)
* test(versioning/aws-machine-image): cover the empty version guard

`_parse()` returns null for a falsy version before it tries the AMI id
regex, and no test passed one. Checking that an empty string is not
valid closes the only branch the module was missing.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource/buildpacks-registry): cover a response without a homepage

`latest.homepage` is optional in the registry response, but every test
supplied one, so the release result was never built without it. A
buildpack whose latest entry carries no homepage now checks that the
releases still come back and no source url is derived.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover optional-field branches in four datasources

Each of these datasources builds its release result by attaching fields
only when the registry supplies them, and the "not supplied" path was
never exercised:

- cdnjs: a library with neither homepage nor repository
- cpan: a search result where no release is marked latest, so no tag
- dart: a latest release carrying no pubspec
- ruby-version: a release table row whose first column is not a version

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover missing-metadata branches in hex and pypi

Two more datasources only attached fields when the registry supplied
them, and the absent case was untested: a hex package with neither an
html_url nor a github link in its meta, and a pypi response carrying no
releases at all.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover galaxy and repology fallbacks, mark unreachable guards

Adds a galaxy role with empty github fields, so no source url is
derived, and a repology lookup where the api fallback after a 403 finds
no matching package.

Marks four guards that are not worth a test: cpan and dart both check a
variable that is only null when the request threw, and
`handleGenericErrors()` always rethrows; conan's registry url is always
supplied by the datasource layer; and repology's resolver either returns
a body or throws.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover pod CDN branches, mark the sbt listing guards

Adds two pod lookups: an empty CDN body, and a CDN index that lists no
matching pod.

Marks the guards whose tests would need multi-level directory listing
fixtures: both sbt datasources walk nested index pages, and pod's github
helper checks a body that an empty json response never produces because
parsing throws first.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover maven metadata without a latest tag

Adds maven metadata carrying a release but no latest, so respectLatest
is left alone. Marks the three deb guards, which would each need a
crafted package index: a block missing a required key, a repeated
package in the final block, and two component indexes whose meta agrees.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(datasource): cover npmrc registry fallback, mark cache-bound guards

Adds a resolveRegistryUrl lookup for a package no npmrc rule matches.

Marks the guards whose tests would need a primed cache or a specific
blob shape: nuget's services index, deno's release cache, the docker hub
cache window, docker's manifest and config handling, go's pseudo-version
fallback, and both rubygems cache layers.

Three implicit elses are left counted: a coverage-ignore hint does not
attach to an `else if`, so the registry strategy chain, the npmrc reset
and docker's digest-header branch carry a plain note instead.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* fix(datasource/repology): pass the mock body as a string

`ResponseMock.body` is typed as a string, and every other call in the
spec passes `'[]'`. The new api fallback test passed an array literal,
which tsc rejected.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

---------

Co-authored-by: Claude Opus 5 <michael.kriese+claude-code@mend.io>
2026-09-14 12:11:52 +00:00

153 lines
4.7 KiB
TypeScript

import { isNullOrUndefined } from '@sindresorhus/is';
import pMap from 'p-map';
import { logger } from '../../../logger/index.ts';
import * as packageCache from '../../../util/cache/package/index.ts';
import { withCache } from '../../../util/cache/package/with-cache.ts';
import { coerceObject } from '../../../util/object.ts';
import { regEx } from '../../../util/regex.ts';
import { joinUrlParts } from '../../../util/url.ts';
import * as semanticVersioning from '../../versioning/semver/index.ts';
import { Datasource } from '../datasource.ts';
import type { Release } from '../index.ts';
import type { GetReleasesConfig, ReleaseResult } from '../types.ts';
import {
DenoAPIModuleResponse,
DenoAPIModuleVersionResponse,
} from './schema.ts';
export class DenoDatasource extends Datasource {
static readonly id = 'deno';
override readonly customRegistrySupport = true;
override readonly registryStrategy = 'first';
override readonly defaultVersioning = semanticVersioning.id;
override readonly defaultRegistryUrls = ['https://apiland.deno.dev'];
override readonly releaseTimestampSupport = true;
override readonly releaseTimestampNote =
'The release timestamp is determined from the `uploaded_at` field in the results.';
override readonly sourceUrlSupport = 'release';
override readonly sourceUrlNote =
'The source URL is determined from the `repository` field in the results.';
constructor() {
super(DenoDatasource.id);
}
private async _getReleases({
packageName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const massagedRegistryUrl = registryUrl!;
const extractResult = regEx(
/^(?:https:\/\/deno.land\/)(?<rawPackageName>[^@\s]+)/,
).exec(packageName);
const rawPackageName = extractResult?.groups?.rawPackageName;
if (isNullOrUndefined(rawPackageName)) {
logger.debug(
`Could not extract rawPackageName from packageName: "${packageName}"`,
);
return null;
}
// remove third-party prefix if defined. The only internal library is `std` and is available under the same API
const massagedPackageName = rawPackageName.replace('x/', '');
// https://apiland.deno.dev/v2/modules/postgres
const moduleAPIURL = joinUrlParts(
massagedRegistryUrl,
'v2/modules',
massagedPackageName,
);
return await this.getReleaseResult(moduleAPIURL);
}
getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
return withCache(
{
namespace: `datasource-${DenoDatasource.id}`,
// TODO: types (#22198)
key: `getReleases:${config.registryUrl}:${config.packageName}`,
fallback: true,
},
() => this._getReleases(config),
);
}
private async _getReleaseResult(
moduleAPIURL: string,
): Promise<ReleaseResult> {
const detailsCacheKey = `details:${moduleAPIURL}`;
const releasesCache: Record<string, Release> = coerceObject(
await packageCache.get(
`datasource-${DenoDatasource.id}`,
detailsCacheKey,
),
);
let cacheModified = false;
const {
body: { versions, tags },
} = await this.http.getJson(moduleAPIURL, DenoAPIModuleResponse);
// get details for the versions
const releases = await pMap(
versions,
async (version) => {
const cacheRelease = releasesCache[version];
/* v8 ignore next: hard to test */
if (cacheRelease) {
return cacheRelease;
}
// https://apiland.deno.dev/v2/modules/postgres/v0.17.0
const url = joinUrlParts(moduleAPIURL, version);
const { body: release } = await this.http.getJson(
url,
DenoAPIModuleVersionResponse.catch(({ error: err }) => {
logger.warn(
{ err, version },
'Deno: failed to get version details',
);
return { version };
}),
);
releasesCache[release.version] = release;
cacheModified = true;
return release;
},
{ concurrency: 5 },
);
// v8 ignore else -- needs every release already present in the cache
if (cacheModified) {
// 1 week. Releases at Deno are immutable, therefore we can use a long term cache here.
await packageCache.set(
`datasource-${DenoDatasource.id}`,
detailsCacheKey,
releasesCache,
10080,
);
}
return { releases, tags };
}
getReleaseResult(moduleAPIURL: string): Promise<ReleaseResult> {
return withCache(
{
namespace: `datasource-${DenoDatasource.id}`,
key: `getReleaseResult:${moduleAPIURL}`,
},
() => this._getReleaseResult(moduleAPIURL),
);
}
}