Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- add explicit return type for findMatching version function
- force version argument to be defined in createVersionArgument
- make dotnetVersion and quality properties readonly
  • Loading branch information
nikolai-laevskii committed Sep 8, 2023
1 parent cd87e99 commit 2219620
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
22 changes: 11 additions & 11 deletions __tests__/dotnet-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ import * as dotnetUtils from '../src/dotnet-utils';
import * as exec from '@actions/exec';

describe('dotnet-utils', () => {
describe('matchVersionToList', () => {
describe('findMatchingVersion', () => {
it('matches all versions with all syntaxes correctly', () => {
expect(
dotnetUtils.matchVersionToList('3.1', ['3.1.201', '6.0.402'])
dotnetUtils.findMatchingVersion('3.1', ['3.1.201', '6.0.402'])
).toEqual('3.1.201');
expect(
dotnetUtils.matchVersionToList('3.1.x', ['3.1.201', '6.0.402'])
dotnetUtils.findMatchingVersion('3.1.x', ['3.1.201', '6.0.402'])
).toEqual('3.1.201');
expect(
dotnetUtils.matchVersionToList('3', ['3.1.201', '6.0.402'])
dotnetUtils.findMatchingVersion('3', ['3.1.201', '6.0.402'])
).toEqual('3.1.201');
expect(
dotnetUtils.matchVersionToList('3.x', ['3.1.201', '6.0.402'])
dotnetUtils.findMatchingVersion('3.x', ['3.1.201', '6.0.402'])
).toEqual('3.1.201');
expect(
dotnetUtils.matchVersionToList('6.0.4xx', ['3.1.201', '6.0.402'])
dotnetUtils.findMatchingVersion('6.0.4xx', ['3.1.201', '6.0.402'])
).toEqual('6.0.402');
});

it('returns undefined if no version is matched', () => {
expect(
dotnetUtils.matchVersionToList('6.0.5xx', ['3.1.201', '6.0.403'])
dotnetUtils.findMatchingVersion('6.0.5xx', ['3.1.201', '6.0.403'])
).toEqual(undefined);
expect(dotnetUtils.matchVersionToList('6.0.5xx', [])).toEqual(undefined);
expect(dotnetUtils.findMatchingVersion('6.0.5xx', [])).toEqual(undefined);
});

it("returns the first version if 'x' or '*' version is provided", () => {
expect(
dotnetUtils.matchVersionToList('x', ['3.1.201', '6.0.403'])
dotnetUtils.findMatchingVersion('x', ['3.1.201', '6.0.403'])
).toEqual('3.1.201');
expect(
dotnetUtils.matchVersionToList('*', ['3.1.201', '6.0.403'])
dotnetUtils.findMatchingVersion('*', ['3.1.201', '6.0.403'])
).toEqual('3.1.201');
});

it('returns undefined if empty version list is provided', () => {
expect(dotnetUtils.matchVersionToList('6.0.4xx', [])).toEqual(undefined);
expect(dotnetUtils.findMatchingVersion('6.0.4xx', [])).toEqual(undefined);
});
});

Expand Down
27 changes: 13 additions & 14 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72785,7 +72785,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.matchVersionToList = exports.listSdks = void 0;
exports.findMatchingVersion = exports.listSdks = void 0;
const exec = __importStar(__nccwpck_require__(1514));
const listSdks = () => __awaiter(void 0, void 0, void 0, function* () {
const { stdout, exitCode } = yield exec
Expand All @@ -72810,20 +72810,19 @@ exports.listSdks = listSdks;
* '3.1', '3.1.x', '3', '3.x', '6.0.4xx' to
* correct version number like '3.1.201', '3.1.201', '3.1.201', '3.1.201', '6.0.402'
*/
const matchVersionToList = (version, versions) => {
if (!version || version === 'x' || version === '*') {
const findMatchingVersion = (versionPattern, versions) => {
if (!versionPattern || versionPattern === 'x' || versionPattern === '*') {
return versions.at(0);
}
const versionArray = version.split('.');
const versionArray = versionPattern.split('.');
if (versionArray.length < 3) {
versionArray.push(...Array(3 - versionArray.length).fill('x'));
}
const normalizedVersion = versionArray.join('.');
const versionRegex = new RegExp(`^${normalizedVersion.replace(/x/g, '\\d+')}`);
const matchedVersion = versions.find(v => versionRegex.test(v));
return matchedVersion;
return versions.find(v => versionRegex.test(v));
};
exports.matchVersionToList = matchVersionToList;
exports.findMatchingVersion = findMatchingVersion;


/***/ }),
Expand Down Expand Up @@ -72895,7 +72894,7 @@ class DotnetVersionResolver {
throw new Error(`The 'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x, A.B.Cxx`);
}
if (semver_1.default.valid(this.inputVersion)) {
this.createVersionArgument();
this.createVersionArgument(this.inputVersion);
return;
}
if (!this.preferInstalled) {
Expand All @@ -72904,12 +72903,12 @@ class DotnetVersionResolver {
}
const requestedVersion = this.inputVersion;
const installedVersions = yield (0, dotnet_utils_1.listSdks)();
const matchingInstalledVersion = (0, dotnet_utils_1.matchVersionToList)(requestedVersion, installedVersions);
if (matchingInstalledVersion === undefined) {
this.createChannelArgument();
const matchingInstalledVersion = (0, dotnet_utils_1.findMatchingVersion)(requestedVersion, installedVersions);
if (matchingInstalledVersion) {
this.createVersionArgument(matchingInstalledVersion);
return;
}
this.createVersionArgument(matchingInstalledVersion);
this.createChannelArgument();
});
}
isNumericTag(versionTag) {
Expand All @@ -72924,9 +72923,9 @@ class DotnetVersionResolver {
}
return majorTag ? true : false;
}
createVersionArgument(updatedVersion) {
createVersionArgument(version) {
this.resolvedArgument.type = 'version';
this.resolvedArgument.value = updatedVersion !== null && updatedVersion !== void 0 ? updatedVersion : this.inputVersion;
this.resolvedArgument.value = version;
}
createChannelArgument() {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
12 changes: 7 additions & 5 deletions src/dotnet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ export const listSdks = async () => {
* '3.1', '3.1.x', '3', '3.x', '6.0.4xx' to
* correct version number like '3.1.201', '3.1.201', '3.1.201', '3.1.201', '6.0.402'
*/
export const matchVersionToList = (version: string, versions: string[]) => {
if (!version || version === 'x' || version === '*') {
export const findMatchingVersion = (
versionPattern: string,
versions: string[]
): string | undefined => {
if (!versionPattern || versionPattern === 'x' || versionPattern === '*') {
return versions.at(0);
}

const versionArray = version.split('.');
const versionArray = versionPattern.split('.');

if (versionArray.length < 3) {
versionArray.push(...Array(3 - versionArray.length).fill('x'));
Expand All @@ -43,7 +46,6 @@ export const matchVersionToList = (version: string, versions: string[]) => {
const versionRegex = new RegExp(
`^${normalizedVersion.replace(/x/g, '\\d+')}`
);
const matchedVersion = versions.find(v => versionRegex.test(v));

return matchedVersion;
return versions.find(v => versionRegex.test(v));
};
20 changes: 10 additions & 10 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import os from 'os';
import semver from 'semver';
import {IS_WINDOWS, PLATFORM} from './utils';
import {QualityOptions} from './setup-dotnet';
import {listSdks, matchVersionToList} from './dotnet-utils';
import {listSdks, findMatchingVersion} from './dotnet-utils';

export interface DotnetVersion {
type: string;
Expand All @@ -36,7 +36,7 @@ export class DotnetVersionResolver {
}

if (semver.valid(this.inputVersion)) {
this.createVersionArgument();
this.createVersionArgument(this.inputVersion);
return;
}

Expand All @@ -47,17 +47,17 @@ export class DotnetVersionResolver {

const requestedVersion = this.inputVersion;
const installedVersions = await listSdks();
const matchingInstalledVersion = matchVersionToList(
const matchingInstalledVersion = findMatchingVersion(
requestedVersion,
installedVersions
);

if (matchingInstalledVersion === undefined) {
this.createChannelArgument();
if (matchingInstalledVersion) {
this.createVersionArgument(matchingInstalledVersion);
return;
}

this.createVersionArgument(matchingInstalledVersion);
this.createChannelArgument();
}

private isNumericTag(versionTag): boolean {
Expand All @@ -79,9 +79,9 @@ export class DotnetVersionResolver {
return majorTag ? true : false;
}

private createVersionArgument(updatedVersion?: string) {
private createVersionArgument(version: string) {
this.resolvedArgument.type = 'version';
this.resolvedArgument.value = updatedVersion ?? this.inputVersion;
this.resolvedArgument.value = version;
}

private async createChannelArgument() {
Expand Down Expand Up @@ -274,8 +274,8 @@ export class DotnetCoreInstaller {
}

constructor(
private dotnetVersion: DotnetVersion,
private quality: QualityOptions
private readonly dotnetVersion: DotnetVersion,
private readonly quality: QualityOptions
) {}

public async installDotnet(): Promise<string | null> {
Expand Down

0 comments on commit 2219620

Please sign in to comment.