refactor application.yaml to become an os definition #40
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
import { IEntity } from '@/infrastructure/Entity/IEntity';
|
||||
import applicationFile, { YamlCategory, YamlScript, ApplicationYaml } from 'js-yaml-loader!@/application/application.yaml';
|
||||
import applicationFile, { YamlCategory, YamlScript, YamlApplication, YamlScriptingDefinition } from 'js-yaml-loader!@/application/application.yaml';
|
||||
import { parseApplication } from '@/application/Parser/ApplicationParser';
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { parseCategory } from '@/application/Parser/CategoryParser';
|
||||
import { RecommendationLevel } from '@/domain/RecommendationLevel';
|
||||
import { ScriptCompilerStub } from '../../stubs/ScriptCompilerStub';
|
||||
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { parseScriptingDefinition } from '@/application/Parser/ScriptingDefinitionParser';
|
||||
import { mockEnumParser } from '../../stubs/EnumParserStub';
|
||||
|
||||
describe('ApplicationParser', () => {
|
||||
describe('parseApplication', () => {
|
||||
@@ -23,33 +28,50 @@ describe('ApplicationParser', () => {
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
it('throws when undefined actions', () => {
|
||||
// arrange
|
||||
const sut: ApplicationYaml = { actions: undefined, functions: undefined };
|
||||
const expectedError = 'application does not define any action';
|
||||
// act
|
||||
const act = () => parseApplication(sut);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
describe('actions', () => {
|
||||
it('throws when undefined actions', () => {
|
||||
// arrange
|
||||
const app = new YamlApplicationBuilder().withActions(undefined).build();
|
||||
// act
|
||||
const act = () => parseApplication(app);
|
||||
// assert
|
||||
expect(act).to.throw('application does not define any action');
|
||||
});
|
||||
it('throws when has no actions', () => {
|
||||
// arrange
|
||||
const app = new YamlApplicationBuilder().withActions([]).build();
|
||||
// act
|
||||
const act = () => parseApplication(app);
|
||||
// assert
|
||||
expect(act).to.throw('application does not define any action');
|
||||
});
|
||||
it('parses actions', () => {
|
||||
// arrange
|
||||
const actions = [ getTestCategory('test1'), getTestCategory('test2') ];
|
||||
const compiler = new ScriptCompilerStub();
|
||||
const expected = [ parseCategory(actions[0], compiler), parseCategory(actions[1], compiler) ];
|
||||
const app = new YamlApplicationBuilder().withActions(actions).build();
|
||||
// act
|
||||
const actual = parseApplication(app).actions;
|
||||
// assert
|
||||
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
|
||||
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
|
||||
return array.map((obj) => {
|
||||
const { ['id']: omitted, ...rest } = obj;
|
||||
return rest;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
it('throws when has no actions', () => {
|
||||
// arrange
|
||||
const sut: ApplicationYaml = { actions: [], functions: undefined };
|
||||
const expectedError = 'application does not define any action';
|
||||
// act
|
||||
const act = () => parseApplication(sut);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
describe('information', () => {
|
||||
describe('info', () => {
|
||||
it('returns expected repository version', () => {
|
||||
// arrange
|
||||
const expected = 'expected-version';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_VERSION = expected;
|
||||
const sut: ApplicationYaml = { actions: [ getTestCategory() ], functions: undefined };
|
||||
const app = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseApplication(sut, env).info.version;
|
||||
const actual = parseApplication(app, env).info.version;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
@@ -58,9 +80,9 @@ describe('ApplicationParser', () => {
|
||||
const expected = 'https://expected-repository.url';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_REPOSITORY_URL = expected;
|
||||
const sut: ApplicationYaml = { actions: [ getTestCategory() ], functions: undefined };
|
||||
const app = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseApplication(sut, env).info.repositoryUrl;
|
||||
const actual = parseApplication(app, env).info.repositoryUrl;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
@@ -69,9 +91,9 @@ describe('ApplicationParser', () => {
|
||||
const expected = 'expected-app-name';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_NAME = expected;
|
||||
const sut: ApplicationYaml = { actions: [ getTestCategory() ], functions: undefined };
|
||||
const app = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseApplication(sut, env).info.name;
|
||||
const actual = parseApplication(app, env).info.name;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
@@ -80,33 +102,79 @@ describe('ApplicationParser', () => {
|
||||
const expected = 'https://expected.sexy';
|
||||
const env = getProcessEnvironmentStub();
|
||||
env.VUE_APP_HOMEPAGE_URL = expected;
|
||||
const sut: ApplicationYaml = { actions: [ getTestCategory() ], functions: undefined };
|
||||
const app = new YamlApplicationBuilder().build();
|
||||
// act
|
||||
const actual = parseApplication(sut, env).info.homepage;
|
||||
const actual = parseApplication(app, env).info.homepage;
|
||||
// assert
|
||||
expect(actual).to.be.equal(expected);
|
||||
});
|
||||
});
|
||||
it('parses actions', () => {
|
||||
// arrange
|
||||
const actions = [ getTestCategory('test1'), getTestCategory('test2') ];
|
||||
const compiler = new ScriptCompilerStub();
|
||||
const expected = [ parseCategory(actions[0], compiler), parseCategory(actions[1], compiler) ];
|
||||
const sut: ApplicationYaml = { actions, functions: undefined };
|
||||
// act
|
||||
const actual = parseApplication(sut).actions;
|
||||
// assert
|
||||
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
|
||||
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
|
||||
return array.map((obj) => {
|
||||
const { ['id']: omitted, ...rest } = obj;
|
||||
return rest;
|
||||
});
|
||||
}
|
||||
describe('scripting definition', () => {
|
||||
it('parses scripting definition as expected', () => {
|
||||
// arrange
|
||||
const app = new YamlApplicationBuilder().build();
|
||||
const information = parseProjectInformation(process.env);
|
||||
const expected = parseScriptingDefinition(app.scripting, information);
|
||||
// act
|
||||
const actual = parseApplication(app).scripting;
|
||||
// assert
|
||||
expect(expected).to.deep.equal(actual);
|
||||
});
|
||||
});
|
||||
describe('os', () => {
|
||||
it('parses as expected', () => {
|
||||
// arrange
|
||||
const expectedOs = OperatingSystem.macOS;
|
||||
const osText = 'macos';
|
||||
const expectedName = 'os';
|
||||
const app = new YamlApplicationBuilder()
|
||||
.withOs(osText)
|
||||
.build();
|
||||
const parserMock = mockEnumParser(expectedName, osText, expectedOs);
|
||||
const env = getProcessEnvironmentStub();
|
||||
// act
|
||||
const actual = parseApplication(app, env, parserMock);
|
||||
// assert
|
||||
expect(actual.os).to.equal(expectedOs);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class YamlApplicationBuilder {
|
||||
private os = 'windows';
|
||||
private actions: readonly YamlCategory[] = [ getTestCategory() ];
|
||||
private scripting: YamlScriptingDefinition = getTestDefinition();
|
||||
|
||||
public withActions(actions: readonly YamlCategory[]): YamlApplicationBuilder {
|
||||
this.actions = actions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOs(os: string): YamlApplicationBuilder {
|
||||
this.os = os;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withScripting(scripting: YamlScriptingDefinition): YamlApplicationBuilder {
|
||||
this.scripting = scripting;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): YamlApplication {
|
||||
return { os: this.os, scripting: this.scripting, actions: this.actions };
|
||||
}
|
||||
}
|
||||
|
||||
function getTestDefinition(): YamlScriptingDefinition {
|
||||
return {
|
||||
fileExtension: '.bat',
|
||||
language: ScriptingLanguage[ScriptingLanguage.batchfile],
|
||||
startCode: 'start',
|
||||
endCode: 'end',
|
||||
};
|
||||
}
|
||||
|
||||
function getTestCategory(scriptPrefix = 'testScript'): YamlCategory {
|
||||
return {
|
||||
category: 'category name',
|
||||
|
||||
Reference in New Issue
Block a user