Skip to content

Commit

Permalink
Add error for incorrect wildcard uses and throw in mergeDependency an…
Browse files Browse the repository at this point in the history
…d lookUpLatestPatchVersion
  • Loading branch information
jafeltra committed Jul 17, 2023
1 parent b53ef0a commit 41ce0c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/errors/IncorrectWildcardVersionFormatError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class IncorrectWildcardVersionFormatError extends Error {
constructor(public packageName: string, version: string) {
super(
`Incorrect version format for package ${packageName}: ${version}. Wildcard should only be used to specify patch versions.`
);
}
}
2 changes: 2 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './CurrentPackageLoadError';
export * from './IncorrectWildcardVersionFormatError';
export * from './LatestVersionUnavailableError';
export * from './PackageLoadError';
13 changes: 11 additions & 2 deletions src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import os from 'os';
import { maxSatisfying } from 'semver';
import tar from 'tar';
import temp from 'temp';
import { PackageLoadError, CurrentPackageLoadError } from './errors';
import {
PackageLoadError,
CurrentPackageLoadError,
IncorrectWildcardVersionFormatError
} from './errors';
import { FHIRDefinitions } from './FHIRDefinitions';
import { LogFunction } from './utils';
import { axiosGet } from './utils/axiosUtils';
Expand Down Expand Up @@ -114,9 +118,11 @@ export async function mergeDependency(
if (version === 'latest') {
// using the exported function here to allow for easier mocking in tests
version = await exports.lookUpLatestVersion(packageName, log);
} else if (/\d+\.\d+\.x/.test(version)) {
} else if (/^\d+\.\d+\.x$/.test(version)) {
// using the exported function here to allow for easier mocking in tests
version = await exports.lookUpLatestPatchVersion(packageName, version, log);
} else if (/^\d+\.x$/.test(version)) {
throw new IncorrectWildcardVersionFormatError(packageName, version);
}
let fullPackageName = `${packageName}#${version}`;
const loadPath = path.join(cachePath, fullPackageName, 'package');
Expand Down Expand Up @@ -401,6 +407,9 @@ export async function lookUpLatestPatchVersion(
version: string,
log: LogFunction = () => {}
): Promise<string> {
if (!/^\d+\.\d+\.x$/.test(version)) {
throw new IncorrectWildcardVersionFormatError(packageName, version);
}
const customRegistry = getCustomRegistry(log);
let res: AxiosResponse;
try {
Expand Down
20 changes: 18 additions & 2 deletions test/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import {
lookUpLatestPatchVersion
} from '../src/load';
import { FHIRDefinitions, Type } from '../src/FHIRDefinitions';
import { PackageLoadError } from '../src/errors';
import {
IncorrectWildcardVersionFormatError,
LatestVersionUnavailableError,
PackageLoadError
} from '../src/errors';
import { loggerSpy } from './testhelpers';
import { LatestVersionUnavailableError } from '../src/errors/LatestVersionUnavailableError';

// Represents a typical response from packages.fhir.org
const TERM_PKG_RESPONSE = {
Expand Down Expand Up @@ -1048,6 +1051,13 @@ describe('#mergeDependency()', () => {
);
});

it('should throw IncorrectWildcardVersionFormatError when the given version uses a non-patch wildcard', async () => {
await expect(mergeDependency('sushi-test', '0.x', defs, 'foo', log)).rejects.toThrow(
'Incorrect version format for package sushi-test: 0.x. Wildcard should only be used to specify patch versions.'
);
expect(axiosSpy.mock.calls.length).toBe(0);
});

it('should throw CurrentPackageLoadError when a current package is not listed', async () => {
await expect(mergeDependency('hl7.fhir.us.core', 'current', defs, 'foo', log)).rejects.toThrow(
'The package hl7.fhir.us.core#current is not available on https://build.fhir.org/ig/qas.json, so no current version can be loaded'
Expand Down Expand Up @@ -1281,4 +1291,10 @@ describe('#lookUpLatestPatchVersion', () => {
LatestVersionUnavailableError
);
});

it('should throw IncorrectWildcardVersionFormatError when a wildcard is used for minor version', async () => {
await expect(lookUpLatestPatchVersion('hl7.terminology.r4', '1.x')).rejects.toThrow(
IncorrectWildcardVersionFormatError
);
});
});

0 comments on commit 41ce0c7

Please sign in to comment.