Refactor to use version object #59

Enable writing safer version aware logic.
This commit is contained in:
undergroundwires
2022-02-26 17:15:30 +01:00
parent d6bc33ec86
commit eeb1d5b0c4
14 changed files with 198 additions and 35 deletions

View File

@@ -1,12 +1,14 @@
import { IProjectInformation } from '@/domain/IProjectInformation'; import { IProjectInformation } from '@/domain/IProjectInformation';
import { ProjectInformation } from '@/domain/ProjectInformation'; import { ProjectInformation } from '@/domain/ProjectInformation';
import { Version } from '@/domain/Version';
export function parseProjectInformation( export function parseProjectInformation(
environment: NodeJS.ProcessEnv, environment: NodeJS.ProcessEnv,
): IProjectInformation { ): IProjectInformation {
const version = new Version(environment.VUE_APP_VERSION);
return new ProjectInformation( return new ProjectInformation(
environment.VUE_APP_NAME, environment.VUE_APP_NAME,
environment.VUE_APP_VERSION, version,
environment.VUE_APP_REPOSITORY_URL, environment.VUE_APP_REPOSITORY_URL,
environment.VUE_APP_HOMEPAGE_URL, environment.VUE_APP_HOMEPAGE_URL,
); );

View File

@@ -22,7 +22,7 @@ export class CodeSubstituter implements ICodeSubstituter {
const substitute = (name: string, value: string) => args const substitute = (name: string, value: string) => args
.addArgument(new FunctionCallArgument(name, value)); .addArgument(new FunctionCallArgument(name, value));
substitute('homepage', info.homepage); substitute('homepage', info.homepage);
substitute('version', info.version); substitute('version', info.version.toString());
substitute('date', this.date.toUTCString()); substitute('date', this.date.toUTCString());
const compiledCode = this.compiler.compileExpressions(code, args); const compiledCode = this.compiler.compileExpressions(code, args);
return compiledCode; return compiledCode;

View File

@@ -1,8 +1,9 @@
import { OperatingSystem } from './OperatingSystem'; import { OperatingSystem } from '@/domain/OperatingSystem';
import { Version } from '@/domain/Version';
export interface IProjectInformation { export interface IProjectInformation {
readonly name: string; readonly name: string;
readonly version: string; readonly version: Version;
readonly repositoryUrl: string; readonly repositoryUrl: string;
readonly homepage: string; readonly homepage: string;
readonly feedbackUrl: string; readonly feedbackUrl: string;

View File

@@ -1,21 +1,22 @@
import { assertInRange } from '@/application/Common/Enum'; import { assertInRange } from '@/application/Common/Enum';
import { IProjectInformation } from './IProjectInformation'; import { IProjectInformation } from './IProjectInformation';
import { OperatingSystem } from './OperatingSystem'; import { OperatingSystem } from './OperatingSystem';
import { Version } from './Version';
export class ProjectInformation implements IProjectInformation { export class ProjectInformation implements IProjectInformation {
public readonly repositoryWebUrl: string; public readonly repositoryWebUrl: string;
constructor( constructor(
public readonly name: string, public readonly name: string,
public readonly version: string, public readonly version: Version,
public readonly repositoryUrl: string, public readonly repositoryUrl: string,
public readonly homepage: string, public readonly homepage: string,
) { ) {
if (!name) { if (!name) {
throw new Error('name is undefined'); throw new Error('name is undefined');
} }
if (!version || +version <= 0) { if (!version) {
throw new Error('version should be higher than zero'); throw new Error('undefined version');
} }
if (!repositoryUrl) { if (!repositoryUrl) {
throw new Error('repositoryUrl is undefined'); throw new Error('repositoryUrl is undefined');
@@ -27,7 +28,8 @@ export class ProjectInformation implements IProjectInformation {
} }
public getDownloadUrl(os: OperatingSystem): string { public getDownloadUrl(os: OperatingSystem): string {
return `${this.repositoryWebUrl}/releases/download/${this.version}/${getFileName(os, this.version)}`; const fileName = getFileName(os, this.version.toString());
return `${this.repositoryWebUrl}/releases/download/${this.version}/${fileName}`;
} }
public get feedbackUrl(): string { public get feedbackUrl(): string {

24
src/domain/Version.ts Normal file
View File

@@ -0,0 +1,24 @@
export class Version {
public readonly major: number;
public readonly minor: number;
public readonly patch: number;
public constructor(semanticVersion: string) {
if (!semanticVersion) {
throw new Error('empty version');
}
if (!semanticVersion.match(/^\d+\.\d+\.\d+$/g)) {
throw new Error(`invalid version: ${semanticVersion}`);
}
const [major, minor, patch] = semanticVersion.split('.');
this.major = parseInt(major, 10);
this.minor = parseInt(minor, 10);
this.patch = parseInt(patch, 10);
}
public toString(): string {
return `${this.major}.${this.minor}.${this.patch}`;
}
}

View File

@@ -77,7 +77,7 @@ export default class TheFooter extends Vue {
private initialize(app: IApplication) { private initialize(app: IApplication) {
const { info } = app; const { info } = app;
this.version = info.version; this.version = info.version.toString();
this.homepageUrl = info.homepage; this.homepageUrl = info.homepage;
this.repositoryUrl = info.repositoryWebUrl; this.repositoryUrl = info.repositoryWebUrl;
this.releaseUrl = info.releaseUrl; this.releaseUrl = info.releaseUrl;

View File

@@ -6,6 +6,8 @@ import log from 'electron-log';
import fetch from 'cross-fetch'; import fetch from 'cross-fetch';
import { ProjectInformation } from '@/domain/ProjectInformation'; import { ProjectInformation } from '@/domain/ProjectInformation';
import { OperatingSystem } from '@/domain/OperatingSystem'; import { OperatingSystem } from '@/domain/OperatingSystem';
import { Version } from '@/domain/Version';
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
import { UpdateProgressBar } from './UpdateProgressBar'; import { UpdateProgressBar } from './UpdateProgressBar';
export function requiresManualUpdate(): boolean { export function requiresManualUpdate(): boolean {
@@ -17,12 +19,7 @@ export async function handleManualUpdate(info: UpdateInfo) {
if (result === ManualDownloadDialogResult.NoAction) { if (result === ManualDownloadDialogResult.NoAction) {
return; return;
} }
const project = new ProjectInformation( const project = getTargetProject(info.version);
process.env.VUE_APP_NAME,
info.version,
process.env.VUE_APP_REPOSITORY_URL,
process.env.VUE_APP_HOMEPAGE_URL,
);
if (result === ManualDownloadDialogResult.VisitReleasesPage) { if (result === ManualDownloadDialogResult.VisitReleasesPage) {
await shell.openExternal(project.releaseUrl); await shell.openExternal(project.releaseUrl);
} else if (result === ManualDownloadDialogResult.UpdateNow) { } else if (result === ManualDownloadDialogResult.UpdateNow) {
@@ -30,6 +27,17 @@ export async function handleManualUpdate(info: UpdateInfo) {
} }
} }
function getTargetProject(targetVersion: string) {
const existingProject = parseProjectInformation(process.env);
const targetProject = new ProjectInformation(
existingProject.name,
new Version(targetVersion),
existingProject.repositoryUrl,
existingProject.homepage,
);
return targetProject;
}
enum ManualDownloadDialogResult { enum ManualDownloadDialogResult {
NoAction = 0, NoAction = 0,
UpdateNow = 1, UpdateNow = 1,

View File

@@ -7,13 +7,14 @@ describe('ProjectInformationParser', () => {
describe('parseProjectInformation', () => { describe('parseProjectInformation', () => {
it('parses expected repository version', () => { it('parses expected repository version', () => {
// arrange // arrange
const expected = 'expected-version'; const expected = '0.11.3';
const env = getProcessEnvironmentStub(); const env = getProcessEnvironmentStub();
env.VUE_APP_VERSION = expected; env.VUE_APP_VERSION = expected;
// act // act
const info = parseProjectInformation(env); const info = parseProjectInformation(env);
// assert // assert
expect(info.version).to.be.equal(expected); const actual = info.version.toString();
expect(actual).to.be.equal(expected);
}); });
it('parses expected repository url', () => { it('parses expected repository url', () => {
// arrange // arrange

View File

@@ -43,14 +43,14 @@ describe('CodeSubstituter', () => {
// arrange // arrange
const info = new ProjectInformationStub(); const info = new ProjectInformationStub();
const date = new Date(); const date = new Date();
const testCases = [ const testCases: Array<{ parameter: string, argument: string }> = [
{ {
parameter: 'homepage', parameter: 'homepage',
argument: info.homepage, argument: info.homepage,
}, },
{ {
parameter: 'version', parameter: 'version',
argument: info.version, argument: info.version.toString(),
}, },
{ {
parameter: 'date', parameter: 'date',

View File

@@ -3,12 +3,13 @@ import { expect } from 'chai';
import { ProjectInformation } from '@/domain/ProjectInformation'; import { ProjectInformation } from '@/domain/ProjectInformation';
import { OperatingSystem } from '@/domain/OperatingSystem'; import { OperatingSystem } from '@/domain/OperatingSystem';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner'; import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
import { VersionStub } from '@tests/unit/shared/Stubs/VersionStub';
describe('ProjectInformation', () => { describe('ProjectInformation', () => {
it('sets name as expected', () => { it('sets name as expected', () => {
// arrange // arrange
const expected = 'expected-name'; const expected = 'expected-name';
const sut = new ProjectInformation(expected, 'version', 'repositoryUrl', 'homepage'); const sut = new ProjectInformation(expected, new VersionStub('0.7.2'), 'repositoryUrl', 'homepage');
// act // act
const actual = sut.name; const actual = sut.name;
// assert // assert
@@ -16,17 +17,17 @@ describe('ProjectInformation', () => {
}); });
it('sets version as expected', () => { it('sets version as expected', () => {
// arrange // arrange
const expected = 'expected-version'; const expected = new VersionStub('0.11.3');
const sut = new ProjectInformation('name', expected, 'repositoryUrl', 'homepage'); const sut = new ProjectInformation('name', expected, 'repositoryUrl', 'homepage');
// act // act
const actual = sut.version; const actual = sut.version;
// assert // assert
expect(actual).to.equal(expected); expect(actual).to.deep.equal(expected);
}); });
it('sets repositoryUrl as expected', () => { it('sets repositoryUrl as expected', () => {
// arrange // arrange
const expected = 'expected-repository-url'; const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', expected, 'homepage'); const sut = new ProjectInformation('name', new VersionStub('0.7.2'), expected, 'homepage');
// act // act
const actual = sut.repositoryUrl; const actual = sut.repositoryUrl;
// assert // assert
@@ -36,7 +37,7 @@ describe('ProjectInformation', () => {
it('sets repositoryUrl when it does not end with .git', () => { it('sets repositoryUrl when it does not end with .git', () => {
// arrange // arrange
const expected = 'expected-repository-url'; const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', expected, 'homepage'); const sut = new ProjectInformation('name', new VersionStub('0.7.2'), expected, 'homepage');
// act // act
const actual = sut.repositoryWebUrl; const actual = sut.repositoryWebUrl;
// assert // assert
@@ -45,7 +46,7 @@ describe('ProjectInformation', () => {
it('removes ".git" from the end when it ends with ".git"', () => { it('removes ".git" from the end when it ends with ".git"', () => {
// arrange // arrange
const expected = 'expected-repository-url'; const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', `${expected}.git`, 'homepage'); const sut = new ProjectInformation('name', new VersionStub('0.7.2'), `${expected}.git`, 'homepage');
// act // act
const actual = sut.repositoryWebUrl; const actual = sut.repositoryWebUrl;
// assert // assert
@@ -55,7 +56,7 @@ describe('ProjectInformation', () => {
it('sets homepage as expected', () => { it('sets homepage as expected', () => {
// arrange // arrange
const expected = 'expected-homepage'; const expected = 'expected-homepage';
const sut = new ProjectInformation('name', 'version', 'repositoryUrl', expected); const sut = new ProjectInformation('name', new VersionStub('0.7.2'), 'repositoryUrl', expected);
// act // act
const actual = sut.homepage; const actual = sut.homepage;
// assert // assert
@@ -65,7 +66,7 @@ describe('ProjectInformation', () => {
// arrange // arrange
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git'; const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const expected = 'https://github.com/undergroundwires/privacy.sexy/issues'; const expected = 'https://github.com/undergroundwires/privacy.sexy/issues';
const sut = new ProjectInformation('name', 'version', repositoryUrl, 'homepage'); const sut = new ProjectInformation('name', new VersionStub('0.7.2'), repositoryUrl, 'homepage');
// act // act
const actual = sut.feedbackUrl; const actual = sut.feedbackUrl;
// assert // assert
@@ -74,7 +75,7 @@ describe('ProjectInformation', () => {
it('sets releaseUrl to github releases page', () => { it('sets releaseUrl to github releases page', () => {
// arrange // arrange
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git'; const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2'; const version = new VersionStub('0.7.2');
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/tag/0.7.2'; const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/tag/0.7.2';
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage'); const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act // act
@@ -87,7 +88,7 @@ describe('ProjectInformation', () => {
// arrange // arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.dmg'; const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.dmg';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git'; const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2'; const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage'); const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act // act
const actual = sut.getDownloadUrl(OperatingSystem.macOS); const actual = sut.getDownloadUrl(OperatingSystem.macOS);
@@ -98,7 +99,7 @@ describe('ProjectInformation', () => {
// arrange // arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.AppImage'; const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.AppImage';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git'; const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2'; const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage'); const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act // act
const actual = sut.getDownloadUrl(OperatingSystem.Linux); const actual = sut.getDownloadUrl(OperatingSystem.Linux);
@@ -109,7 +110,7 @@ describe('ProjectInformation', () => {
// arrange // arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-Setup-0.7.2.exe'; const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-Setup-0.7.2.exe';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git'; const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2'; const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage'); const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act // act
const actual = sut.getDownloadUrl(OperatingSystem.Windows); const actual = sut.getDownloadUrl(OperatingSystem.Windows);
@@ -118,7 +119,7 @@ describe('ProjectInformation', () => {
}); });
describe('throws when os is invalid', () => { describe('throws when os is invalid', () => {
// arrange // arrange
const sut = new ProjectInformation('name', 'version', 'repositoryUrl', 'homepage'); const sut = new ProjectInformation('name', new VersionStub(), 'repositoryUrl', 'homepage');
// act // act
const act = (os: OperatingSystem) => sut.getDownloadUrl(os); const act = (os: OperatingSystem) => sut.getDownloadUrl(os);
// assert // assert

View File

@@ -0,0 +1,115 @@
import 'mocha';
import { expect } from 'chai';
import { Version } from '@/domain/Version';
describe('Version', () => {
describe('invalid versions', () => {
describe('throws with invalid semantic version', () => {
// arrange
const invalidVersions = [
'0.1.0.0', '0.1', '0.1..0', '0..1.0', '0..1', '...', '0.10', '0.-5.4',
];
for (const version of invalidVersions) {
const expectedError = `invalid version: ${version}`;
it(`given ${version}`, () => {
// act
const act = () => new Version(version);
//
expect(act).to.throw(expectedError);
});
}
});
describe('throws with empty string', () => {
// arrange
const expectedError = 'empty version';
const testCases = [
{ name: 'empty', value: '' },
{ name: 'undefined', value: undefined },
];
for (const testCase of testCases) {
it(`given ${testCase.name}`, () => {
// act
const act = () => new Version(testCase.value);
//
expect(act).to.throw(expectedError);
});
}
});
});
describe('valid versions', () => {
const validVersions: Array<ValidVersionTestData> = [
{
text: '0.1.0',
parts: { major: 0, minor: 1, patch: 0 },
},
{
text: '3.0.0',
parts: { major: 3, minor: 0, patch: 0 },
},
{
text: '100.1000.10000',
parts: { major: 100, minor: 1000, patch: 10000 },
},
];
function testValidVersions(tester: (data: ValidVersionTestData) => void) {
for (const version of validVersions) {
it(`given ${version.text}`, () => {
tester(version);
});
}
}
describe('major', () => {
testValidVersions((version) => {
// arrange
const expected = version.parts.major;
// act
const sut = new Version(version.text);
const actual = sut.major;
// assert
expect(expected).to.equal(actual);
});
});
describe('minor', () => {
testValidVersions((version) => {
// arrange
const expected = version.parts.minor;
// act
const sut = new Version(version.text);
const actual = sut.minor;
// assert
expect(expected).to.equal(actual);
});
});
describe('patch', () => {
testValidVersions((version) => {
// arrange
const expected = version.parts.patch;
// act
const sut = new Version(version.text);
const actual = sut.patch;
// assert
expect(expected).to.equal(actual);
});
});
describe('toString', () => {
testValidVersions((version) => {
// arrange
const expected = version.text;
// act
const sut = new Version(expected);
const actual = sut.toString();
// assert
expect(expected).to.equal(actual);
});
});
});
});
interface ValidVersionTestData {
text: string;
parts: {
major: number,
minor: number,
patch: number,
},
}

View File

@@ -1,6 +1,6 @@
export function getProcessEnvironmentStub(): NodeJS.ProcessEnv { export function getProcessEnvironmentStub(): NodeJS.ProcessEnv {
return { return {
VUE_APP_VERSION: 'stub-version', VUE_APP_VERSION: '0.11.3',
VUE_APP_NAME: 'stub-name', VUE_APP_NAME: 'stub-name',
VUE_APP_REPOSITORY_URL: 'stub-repository-url', VUE_APP_REPOSITORY_URL: 'stub-repository-url',
VUE_APP_HOMEPAGE_URL: 'stub-homepage-url', VUE_APP_HOMEPAGE_URL: 'stub-homepage-url',

View File

@@ -1,9 +1,11 @@
import { IProjectInformation } from '@/domain/IProjectInformation'; import { IProjectInformation } from '@/domain/IProjectInformation';
import { Version } from '@/domain/Version';
import { VersionStub } from './VersionStub';
export class ProjectInformationStub implements IProjectInformation { export class ProjectInformationStub implements IProjectInformation {
public name = 'name'; public name = 'name';
public version = 'version'; public version = new VersionStub();
public repositoryUrl = 'repositoryUrl'; public repositoryUrl = 'repositoryUrl';
@@ -22,7 +24,7 @@ export class ProjectInformationStub implements IProjectInformation {
return this; return this;
} }
public withVersion(version: string): ProjectInformationStub { public withVersion(version: Version): ProjectInformationStub {
this.version = version; this.version = version;
return this; return this;
} }

View File

@@ -0,0 +1,7 @@
import { Version } from '@/domain/Version';
export class VersionStub extends Version {
constructor(version?: string) {
super(version ?? '0.10.0');
}
}