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';
describe('version tests', () => {
each([
'3.1.999',
'3.1.101-preview'
]).test("Exact version '%s' should be the same", vers => {
each(['3.1.999', '3.1.101-preview']).test(
"Exact version '%s' should be the same",
vers => {
let versInfo = new installer.DotNetVersionInfo(vers);
expect(versInfo.isExactVersion()).toBe(true);
expect(versInfo.version()).toBe(vers);
})
}
);
each([
['3.1.x', '3.1'],
['1.1.*', '1.1'],
['2.0', '2.0']
]).test("Generic version '%s' should be '%s'", (vers, resVers) => {
each([['3.1.x', '3.1'], ['1.1.*', '1.1'], ['2.0', '2.0']]).test(
"Generic version '%s' should be '%s'",
(vers, resVers) => {
let versInfo = new installer.DotNetVersionInfo(vers);
expect(versInfo.isExactVersion()).toBe(false);
expect(versInfo.version()).toBe(resVers);
})
}
);
each([
"",
".",
"..",
" . ",
". ",
" .",
" . . ",
" .. ",
" . ",
"-1.-1",
"-1",
"-1.-1.-1",
"..3",
"1..3",
"1..",
".2.3",
".2.x",
"1",
"2.x",
"*.*.1",
"*.1",
"*.",
"1.2.",
"1.2.-abc",
"a.b",
"a.b.c",
"a.b.c-preview",
" 0 . 1 . 2 ",
'',
'.',
'..',
' . ',
'. ',
' .',
' . . ',
' .. ',
' . ',
'-1.-1',
'-1',
'-1.-1.-1',
'..3',
'1..3',
'1..',
'.2.3',
'.2.x',
'1',
'2.x',
'*.*.1',
'*.1',
'*.',
'1.2.',
'1.2.-abc',
'a.b',
'a.b.c',
'a.b.c-preview',
' 0 . 1 . 2 '
]).test("Malformed version '%s' should throw", vers => {
expect(() => new installer.DotNetVersionInfo(vers)).toThrow();
})
})
});
});
describe('installer tests', () => {
beforeAll(async () => {
@ -88,7 +87,10 @@ describe('installer tests', () => {
it('Resolving a normal generic version works', async () => {
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.'));
}, 100000);
@ -96,7 +98,10 @@ describe('installer tests', () => {
it('Resolving a nonexistent generic version fails', async () => {
const dotnetInstaller = new installer.DotnetCoreInstaller('999.1.x');
try {
await dotnetInstaller.resolveInfos(["win-x64"],new installer.DotNetVersionInfo('999.1.x'));
await dotnetInstaller.resolveInfos(
['win-x64'],
new installer.DotNetVersionInfo('999.1.x')
);
fail();
} catch {
expect(true);
@ -105,7 +110,10 @@ describe('installer tests', () => {
it('Resolving a exact version works', async () => {
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');
}, 100000);

View File

@ -31,12 +31,10 @@ if (!tempDirectory) {
* Represents the inputted version information
*/
export class DotNetVersionInfo {
private fullversion: string;
private isExactVersionSet: boolean = false;
constructor(version: string) {
// Check for exact match
if (semver.valid(semver.clean(version) || '') != null) {
this.fullversion = semver.clean(version) as string;
@ -48,10 +46,9 @@ export class DotNetVersionInfo {
//Note: No support for previews when using generic
let parts: string[] = version.split('.');
if(parts.length < 2 || parts.length > 3)
this.throwInvalidVersionFormat();
if (parts.length < 2 || parts.length > 3) this.throwInvalidVersionFormat();
if(parts.length == 3 && parts[2] !== "x" && parts[2] !== "*") {
if (parts.length == 3 && parts[2] !== 'x' && parts[2] !== '*') {
this.throwInvalidVersionFormat();
}
@ -62,15 +59,12 @@ export class DotNetVersionInfo {
}
private getVersionNumberOrThrow(input: string): number {
try
{
if(!input || input.trim() === "")
this.throwInvalidVersionFormat();
try {
if (!input || input.trim() === '') this.throwInvalidVersionFormat();
let number = Number(input);
if(Number.isNaN(number) || number < 0)
this.throwInvalidVersionFormat();
if (Number.isNaN(number) || number < 0) this.throwInvalidVersionFormat();
return number;
} catch {
@ -80,7 +74,7 @@ export class DotNetVersionInfo {
}
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.*';
}
/**
@ -125,7 +119,7 @@ export class DotnetCoreInstaller {
public async installDotnet() {
// Check cache
let toolPath: string = "";
let toolPath: string = '';
let osSuffixes = await this.detectMachineOS();
let parts = osSuffixes[0].split('-');
if (parts.length > 1) {
@ -139,7 +133,10 @@ export class DotnetCoreInstaller {
if (!toolPath) {
// download, extract, cache
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
toolPath = this.getLocalTool(resolvedVersionInfo.resolvedVersion);
@ -281,7 +278,6 @@ export class DotnetCoreInstaller {
osSuffixes: string[],
versionInfo: DotNetVersionInfo
): Promise<ResolvedVersionInfo> {
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
allowRetries: true,
maxRetries: 3
@ -297,8 +293,14 @@ export class DotnetCoreInstaller {
let releasesInfo: any[] = releasesResult['releases'];
releasesInfo = releasesInfo.filter((releaseInfo: any) => {
return (
semver.satisfies(releaseInfo['sdk']['version'], versionInfo.version()) ||
semver.satisfies(releaseInfo['sdk']['version-display'], versionInfo.version())
semver.satisfies(
releaseInfo['sdk']['version'],
versionInfo.version()
) ||
semver.satisfies(
releaseInfo['sdk']['version-display'],
versionInfo.version()
)
);
});
@ -306,11 +308,15 @@ export class DotnetCoreInstaller {
if (!versionInfo.isExactVersion()) {
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
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 downloadUrls: string[] = [];
@ -344,7 +350,9 @@ export class DotnetCoreInstaller {
if (versionInfo.isExactVersion()) {
console.log('Using fallback');
downloadUrls = await this.getFallbackDownloadUrls(versionInfo.version());
downloadUrls = await this.getFallbackDownloadUrls(
versionInfo.version()
);
downloadedVersion = versionInfo.version();
} else {
console.log('Unable to use fallback, version is generic!');