From 2219620f96e4838592ef1c3f26dd3ce5182b7a4a Mon Sep 17 00:00:00 2001 From: Nikolai Laevskii Date: Fri, 8 Sep 2023 14:45:09 +0200 Subject: [PATCH] Address review comments - add explicit return type for findMatching version function - force version argument to be defined in createVersionArgument - make dotnetVersion and quality properties readonly --- __tests__/dotnet-utils.test.ts | 22 +++++++++++----------- dist/setup/index.js | 27 +++++++++++++-------------- src/dotnet-utils.ts | 12 +++++++----- src/installer.ts | 20 ++++++++++---------- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/__tests__/dotnet-utils.test.ts b/__tests__/dotnet-utils.test.ts index b4d86a5..7305985 100644 --- a/__tests__/dotnet-utils.test.ts +++ b/__tests__/dotnet-utils.test.ts @@ -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); }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index b01f167..77cb395 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -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 @@ -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; /***/ }), @@ -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) { @@ -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) { @@ -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* () { diff --git a/src/dotnet-utils.ts b/src/dotnet-utils.ts index 8a44369..bb15f4d 100644 --- a/src/dotnet-utils.ts +++ b/src/dotnet-utils.ts @@ -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')); @@ -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)); }; diff --git a/src/installer.ts b/src/installer.ts index b31ebd9..365956e 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -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; @@ -36,7 +36,7 @@ export class DotnetVersionResolver { } if (semver.valid(this.inputVersion)) { - this.createVersionArgument(); + this.createVersionArgument(this.inputVersion); return; } @@ -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 { @@ -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() { @@ -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 {