Reworked version parser

* Updated tests
This commit is contained in:
litetex 2020-04-05 15:37:29 +02:00
parent 3426e11858
commit 3eb0d2f3dd
2 changed files with 84 additions and 78 deletions

View File

@ -3,6 +3,7 @@ import fs = require('fs');
import os = require('os'); import os = require('os');
import path = require('path'); import path = require('path');
import hc = require('@actions/http-client'); import hc = require('@actions/http-client');
import each from 'jest-each';
const toolDir = path.join(__dirname, 'runner', 'tools'); const toolDir = path.join(__dirname, 'runner', 'tools');
const tempDir = path.join(__dirname, 'runner', 'temp'); const tempDir = path.join(__dirname, 'runner', 'temp');
@ -14,89 +15,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([
it('Exact normal version', async() => { '3.1.999',
let versInfo = new installer.DotNetVersionInfo('3.1.201'); '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.isExactVersion()).toBe(true);
expect(versInfo.version()).toBe('3.1.201'); expect(versInfo.version()).toBe(vers);
}); })
it('Exact preview version', async() => { each([
let versInfo = new installer.DotNetVersionInfo('3.1.201-preview1'); ['3.1.x', '3.1'],
['1.1.*', '1.1'],
expect(versInfo.isExactVersion()).toBe(true); ['2.0', '2.0']
expect(versInfo.version()).toBe('3.1.201-preview1'); ]).test("Generic version '%s' should be '%s'", (vers, resVers) => {
}); let versInfo = new installer.DotNetVersionInfo(vers);
it('Generic x version', async() => {
let versInfo = new installer.DotNetVersionInfo('3.1.x');
expect(versInfo.isExactVersion()).toBe(false); expect(versInfo.isExactVersion()).toBe(false);
expect(versInfo.version()).toBe('3.1'); expect(versInfo.version()).toBe(resVers);
}); })
it('Generic * version', async() => { each([
let versInfo = new installer.DotNetVersionInfo('1.1.*'); "",
".",
expect(versInfo.isExactVersion()).toBe(false); "..",
expect(versInfo.version()).toBe('1.1'); " . ",
}); ". ",
" .",
it('Generic -no patch- version', async() => { " . . ",
let versInfo = new installer.DotNetVersionInfo('2.0'); " .. ",
" . ",
expect(versInfo.isExactVersion()).toBe(false); "-1.-1",
expect(versInfo.version()).toBe('2.0'); "-1",
}); "-1.-1.-1",
"..3",
it('Generic -no minor- version', async() => { "1..3",
expect(() => { "1..",
new installer.DotNetVersionInfo('2'); ".2.3",
}).toThrow(); ".2.x",
}); "1",
"2.x",
it('empty version', async() => { "*.*.1",
expect(() => { "*.1",
new installer.DotNetVersionInfo(''); "*.",
}).toThrow(); "1.2.",
}); "1.2.-abc",
"a.b",
it('malformed no patch but dot version', async() => { "a.b.c",
expect(() => { "a.b.c-preview",
new installer.DotNetVersionInfo('1.2.'); " 0 . 1 . 2 ",
}).toThrow(); ]).test("Malformed version '%s' should throw", vers => {
}); expect(() => new installer.DotNetVersionInfo(vers)).toThrow();
})
it('malformed generic minor version', async() => {
expect(() => {
new installer.DotNetVersionInfo('1.*.2');
}).toThrow();
});
it('malformed generic major version', async() => {
expect(() => {
new installer.DotNetVersionInfo('*.2.2');
}).toThrow();
});
it('malformed letter version', async() => {
expect(() => {
new installer.DotNetVersionInfo('a.b.c');
}).toThrow();
});
it('malformed letter preview version', async() => {
expect(() => {
new installer.DotNetVersionInfo('a.b.c-preview');
}).toThrow();
});
it('malformed letter -no minor- version', async() => {
expect(() => {
new installer.DotNetVersionInfo('a.b');
}).toThrow();
});
}) })
describe('installer tests', () => { describe('installer tests', () => {

View File

@ -54,6 +54,19 @@ export class DotNetVersionInfo {
} }
//Note: No support for previews when using generic //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 == 3 && parts[2] !== "x" && parts[2] !== "*") {
this.throwInvalidVersionFormat();
}
this.major = this.getVersionNumberOrThrow(parts[0]);
this.minor = this.getVersionNumberOrThrow(parts[1]);
/*
let regexResult = version.match(/^(\d+\.)(\d+)?(\.\*|\.x|)$/); let regexResult = version.match(/^(\d+\.)(\d+)?(\.\*|\.x|)$/);
if(regexResult == null) { if(regexResult == null) {
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.*';
@ -62,11 +75,33 @@ export class DotNetVersionInfo {
let parts : string[] = (regexResult as RegExpMatchArray).slice(1); let parts : string[] = (regexResult as RegExpMatchArray).slice(1);
this.major = +(parts[0].replace('.','')); this.major = +(parts[0].replace('.',''));
this.minor = +(parts[1].replace('.','')); this.minor = +(parts[1].replace('.',''));*/
this.fullversion = this.major + '.' + this.minor; this.fullversion = this.major + '.' + this.minor;
} }
private getVersionNumberOrThrow(input: string) : number {
try
{
if(!input || input.trim() === "")
this.throwInvalidVersionFormat();
let number = Number(input);
if(Number.isNaN(number) || number < 0)
this.throwInvalidVersionFormat();
return number;
} catch {
this.throwInvalidVersionFormat();
return -1;
}
}
private throwInvalidVersionFormat() {
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
*/ */