Pretty printed code

This commit is contained in:
litetex 2020-04-09 19:38:39 +02:00
parent 3894eede2d
commit 7b228edf43
2 changed files with 107 additions and 91 deletions

View File

@ -16,60 +16,59 @@ import * as installer from '../src/installer';
const IS_WINDOWS = process.platform === 'win32'; const IS_WINDOWS = process.platform === 'win32';
describe('version tests', () => { describe('version tests', () => {
each([ each(['3.1.999', '3.1.101-preview']).test(
'3.1.999', "Exact version '%s' should be the same",
'3.1.101-preview' vers => {
]).test("Exact version '%s' should be the same", vers => {
let versInfo = new installer.DotNetVersionInfo(vers); let versInfo = new installer.DotNetVersionInfo(vers);
expect(versInfo.isExactVersion()).toBe(true); expect(versInfo.isExactVersion()).toBe(true);
expect(versInfo.version()).toBe(vers); expect(versInfo.version()).toBe(vers);
}) }
);
each([ each([['3.1.x', '3.1'], ['1.1.*', '1.1'], ['2.0', '2.0']]).test(
['3.1.x', '3.1'], "Generic version '%s' should be '%s'",
['1.1.*', '1.1'], (vers, resVers) => {
['2.0', '2.0']
]).test("Generic version '%s' should be '%s'", (vers, resVers) => {
let versInfo = new installer.DotNetVersionInfo(vers); let versInfo = new installer.DotNetVersionInfo(vers);
expect(versInfo.isExactVersion()).toBe(false); expect(versInfo.isExactVersion()).toBe(false);
expect(versInfo.version()).toBe(resVers); expect(versInfo.version()).toBe(resVers);
}) }
);
each([ each([
"", '',
".", '.',
"..", '..',
" . ", ' . ',
". ", '. ',
" .", ' .',
" . . ", ' . . ',
" .. ", ' .. ',
" . ", ' . ',
"-1.-1", '-1.-1',
"-1", '-1',
"-1.-1.-1", '-1.-1.-1',
"..3", '..3',
"1..3", '1..3',
"1..", '1..',
".2.3", '.2.3',
".2.x", '.2.x',
"1", '1',
"2.x", '2.x',
"*.*.1", '*.*.1',
"*.1", '*.1',
"*.", '*.',
"1.2.", '1.2.',
"1.2.-abc", '1.2.-abc',
"a.b", 'a.b',
"a.b.c", 'a.b.c',
"a.b.c-preview", 'a.b.c-preview',
" 0 . 1 . 2 ", ' 0 . 1 . 2 '
]).test("Malformed version '%s' should throw", vers => { ]).test("Malformed version '%s' should throw", vers => {
expect(() => new installer.DotNetVersionInfo(vers)).toThrow(); expect(() => new installer.DotNetVersionInfo(vers)).toThrow();
}) });
}) });
describe('installer tests', () => { describe('installer tests', () => {
beforeAll(async () => { beforeAll(async () => {
@ -86,26 +85,35 @@ describe('installer tests', () => {
} }
}, 100000); }, 100000);
it('Resolving a normal generic version works', async() => { it('Resolving a normal generic version works', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.x'); const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.x');
let versInfo = await dotnetInstaller.resolveInfos(["win-x64"],new installer.DotNetVersionInfo('3.1.x')); let versInfo = await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('3.1.x')
);
expect(versInfo.resolvedVersion.startsWith('3.1.')); expect(versInfo.resolvedVersion.startsWith('3.1.'));
}, 100000); }, 100000);
it('Resolving a nonexistent generic version fails', async() => { it('Resolving a nonexistent generic version fails', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x'); const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x');
try{ try {
await dotnetInstaller.resolveInfos(["win-x64"],new installer.DotNetVersionInfo('999.1.x')); await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('999.1.x')
);
fail(); fail();
} catch { } catch {
expect(true); expect(true);
} }
}, 100000); }, 100000);
it('Resolving a exact version works', async() => { it('Resolving a exact version works', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.201'); const dotnetInstaller = new installer.DotnetCoreInstaller('3.1.201');
let versInfo = await dotnetInstaller.resolveInfos(["win-x64"],new installer.DotNetVersionInfo('3.1.201')); let versInfo = await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('3.1.201')
);
expect(versInfo.resolvedVersion).toBe('3.1.201'); expect(versInfo.resolvedVersion).toBe('3.1.201');
}, 100000); }, 100000);

View File

@ -31,14 +31,12 @@ if (!tempDirectory) {
* Represents the inputted version information * Represents the inputted version information
*/ */
export class DotNetVersionInfo { export class DotNetVersionInfo {
private fullversion: string;
private fullversion : string;
private isExactVersionSet: boolean = false; private isExactVersionSet: boolean = false;
constructor(version: string) { constructor(version: string) {
// Check for exact match // Check for exact match
if(semver.valid(semver.clean(version) || '') != null) { if (semver.valid(semver.clean(version) || '') != null) {
this.fullversion = semver.clean(version) as string; this.fullversion = semver.clean(version) as string;
this.isExactVersionSet = true; this.isExactVersionSet = true;
@ -48,10 +46,9 @@ export class DotNetVersionInfo {
//Note: No support for previews when using generic //Note: No support for previews when using generic
let parts: string[] = version.split('.'); let parts: string[] = version.split('.');
if(parts.length < 2 || parts.length > 3) if (parts.length < 2 || parts.length > 3) this.throwInvalidVersionFormat();
this.throwInvalidVersionFormat();
if(parts.length == 3 && parts[2] !== "x" && parts[2] !== "*") { if (parts.length == 3 && parts[2] !== 'x' && parts[2] !== '*') {
this.throwInvalidVersionFormat(); this.throwInvalidVersionFormat();
} }
@ -61,16 +58,13 @@ export class DotNetVersionInfo {
this.fullversion = major + '.' + minor; this.fullversion = major + '.' + minor;
} }
private getVersionNumberOrThrow(input: string) : number { private getVersionNumberOrThrow(input: string): number {
try try {
{ if (!input || input.trim() === '') this.throwInvalidVersionFormat();
if(!input || input.trim() === "")
this.throwInvalidVersionFormat();
let number = Number(input); let number = Number(input);
if(Number.isNaN(number) || number < 0) if (Number.isNaN(number) || number < 0) this.throwInvalidVersionFormat();
this.throwInvalidVersionFormat();
return number; return number;
} catch { } catch {
@ -80,17 +74,17 @@ export class DotNetVersionInfo {
} }
private throwInvalidVersionFormat() { private throwInvalidVersionFormat() {
throw 'Invalid version format! Supported: 1.2.3, 1.2, 1.2.x, 1.2.*' throw 'Invalid version format! Supported: 1.2.3, 1.2, 1.2.x, 1.2.*';
} }
/** /**
* If true exacatly one version should be resolved * If true exacatly one version should be resolved
*/ */
public isExactVersion() : boolean { public isExactVersion(): boolean {
return this.isExactVersionSet; return this.isExactVersionSet;
} }
public version() : string { public version(): string {
return this.fullversion; return this.fullversion;
} }
} }
@ -103,11 +97,11 @@ class ResolvedVersionInfo {
resolvedVersion: string; resolvedVersion: string;
constructor(downloadUrls: string[], resolvedVersion: string) { constructor(downloadUrls: string[], resolvedVersion: string) {
if(downloadUrls.length === 0) { if (downloadUrls.length === 0) {
throw 'DownloadUrls can not be empty'; throw 'DownloadUrls can not be empty';
} }
if(!resolvedVersion) { if (!resolvedVersion) {
throw 'Resolved version is invalid'; throw 'Resolved version is invalid';
} }
@ -125,7 +119,7 @@ export class DotnetCoreInstaller {
public async installDotnet() { public async installDotnet() {
// Check cache // Check cache
let toolPath: string = ""; let toolPath: string = '';
let osSuffixes = await this.detectMachineOS(); let osSuffixes = await this.detectMachineOS();
let parts = osSuffixes[0].split('-'); let parts = osSuffixes[0].split('-');
if (parts.length > 1) { if (parts.length > 1) {
@ -133,17 +127,20 @@ export class DotnetCoreInstaller {
} }
// If version is not generic -> look up cache // If version is not generic -> look up cache
if(this.versionInfo.isExactVersion()) if (this.versionInfo.isExactVersion())
toolPath = this.getLocalTool(this.versionInfo.version()); toolPath = this.getLocalTool(this.versionInfo.version());
if (!toolPath) { if (!toolPath) {
// download, extract, cache // download, extract, cache
console.log('Getting a download url', this.versionInfo.version()); console.log('Getting a download url', this.versionInfo.version());
let resolvedVersionInfo = await this.resolveInfos(osSuffixes, this.versionInfo); let resolvedVersionInfo = await this.resolveInfos(
osSuffixes,
this.versionInfo
);
//Check if cache exists for resolved version //Check if cache exists for resolved version
toolPath = this.getLocalTool(resolvedVersionInfo.resolvedVersion); toolPath = this.getLocalTool(resolvedVersionInfo.resolvedVersion);
if(!toolPath) { if (!toolPath) {
//If not exists install it //If not exists install it
toolPath = await this.downloadAndInstall(resolvedVersionInfo); toolPath = await this.downloadAndInstall(resolvedVersionInfo);
} else { } else {
@ -281,7 +278,6 @@ export class DotnetCoreInstaller {
osSuffixes: string[], osSuffixes: string[],
versionInfo: DotNetVersionInfo versionInfo: DotNetVersionInfo
): Promise<ResolvedVersionInfo> { ): Promise<ResolvedVersionInfo> {
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], { const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
allowRetries: true, allowRetries: true,
maxRetries: 3 maxRetries: 3
@ -297,22 +293,32 @@ export class DotnetCoreInstaller {
let releasesInfo: any[] = releasesResult['releases']; let releasesInfo: any[] = releasesResult['releases'];
releasesInfo = releasesInfo.filter((releaseInfo: any) => { releasesInfo = releasesInfo.filter((releaseInfo: any) => {
return ( return (
semver.satisfies(releaseInfo['sdk']['version'], versionInfo.version()) || semver.satisfies(
semver.satisfies(releaseInfo['sdk']['version-display'], versionInfo.version()) releaseInfo['sdk']['version'],
versionInfo.version()
) ||
semver.satisfies(
releaseInfo['sdk']['version-display'],
versionInfo.version()
)
); );
}); });
// Exclude versions that are newer than the latest if using not exact // Exclude versions that are newer than the latest if using not exact
if(!versionInfo.isExactVersion()) { if (!versionInfo.isExactVersion()) {
let latestSdk : string = releasesResult['latest-sdk']; let latestSdk: string = releasesResult['latest-sdk'];
releasesInfo = releasesInfo.filter((releaseInfo: any) => semver.lte(releaseInfo['sdk']['version'], latestSdk)); releasesInfo = releasesInfo.filter((releaseInfo: any) =>
semver.lte(releaseInfo['sdk']['version'], latestSdk)
);
} }
// Sort for latest version // Sort for latest version
releasesInfo = releasesInfo.sort((a,b) => semver.rcompare(a['sdk']['version'],b['sdk']['version'])); releasesInfo = releasesInfo.sort((a, b) =>
semver.rcompare(a['sdk']['version'], b['sdk']['version'])
);
let downloadedVersion : string = ''; let downloadedVersion: string = '';
let downloadUrls: string[] = []; let downloadUrls: string[] = [];
if (releasesInfo.length != 0) { if (releasesInfo.length != 0) {
@ -341,10 +347,12 @@ export class DotnetCoreInstaller {
`Could not fetch download information for version ${versionInfo.version()}` `Could not fetch download information for version ${versionInfo.version()}`
); );
if(versionInfo.isExactVersion()) { if (versionInfo.isExactVersion()) {
console.log('Using fallback'); console.log('Using fallback');
downloadUrls = await this.getFallbackDownloadUrls(versionInfo.version()); downloadUrls = await this.getFallbackDownloadUrls(
versionInfo.version()
);
downloadedVersion = versionInfo.version(); downloadedVersion = versionInfo.version();
} else { } else {
console.log('Unable to use fallback, version is generic!'); console.log('Unable to use fallback, version is generic!');