Skip to content

Commit

Permalink
test (#508): Implemented proper tests for the @kipper/config function…
Browse files Browse the repository at this point in the history
…ality
  • Loading branch information
Luna-Klatzer committed Feb 13, 2024
1 parent 9a37144 commit 78a216b
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 5 deletions.
15 changes: 15 additions & 0 deletions test/config-files/errors/incompatible-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": ">=99.99.99",
"target": "ts"
},
"files": [
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png:icon.png"
]
}
15 changes: 15 additions & 0 deletions test/config-files/errors/invalid-array-element-type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "*",
"target": "ts"
},
"files": [
true
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png:icon.png"
]
}
1 change: 1 addition & 0 deletions test/config-files/errors/invalid-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is invalid JSON
15 changes: 15 additions & 0 deletions test/config-files/errors/invalid-mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "*",
"target": "ts"
},
"files": [
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png::"
]
}
10 changes: 10 additions & 0 deletions test/config-files/errors/invalid-type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"outDir": true,
"srcDir": true,
"compiler": {
"version": true,
"target": true
},
"files": true,
"resources": true
}
15 changes: 15 additions & 0 deletions test/config-files/errors/invalid-version-syntax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "02xi2ka018ncx",
"target": "ts"
},
"files": [
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png:icon.png"
]
}
3 changes: 3 additions & 0 deletions test/config-files/errors/missing-required-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
16 changes: 16 additions & 0 deletions test/config-files/errors/unknown-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "*",
"target": "ts"
},
"files": [
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png:icon.png"
],
"i-am-new": true
}
3 changes: 2 additions & 1 deletion test/config-files/kip-config.basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png"
"img/icon.png",
"img/Kipper-Logo-without-head.png:new-img-folder/icon.png"
]
}
16 changes: 16 additions & 0 deletions test/config-files/kip-config.extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./test/config-files/kip-config.extension.json",
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"version": "*",
"target": "ts"
},
"files": [
"test/kipper-files/main.kip"
],
"resources": [
"img/icon.png",
"img/Kipper-Logo-without-head.png:new-img-folder/icon.png"
]
}
14 changes: 14 additions & 0 deletions test/config-files/kip-config.extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./test/config-files/kip-config.sec-extension.json",
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"target": "js"
},
"files": [
"test/kipper-files/multi-function-call.kip"
],
"resources": [
"img/Kipper-Icon.png"
]
}
13 changes: 13 additions & 0 deletions test/config-files/kip-config.sec-extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"outDir": "dist",
"srcDir": "test/kipper-files",
"compiler": {
"target": "ext"
},
"files": [
"test/kipper-files/multi-function-definition.kip"
],
"resources": [
"img/Kipper-Logo-with-head.png"
]
}
79 changes: 75 additions & 4 deletions test/module/config/kipper-config-file-interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { getFileName } from "./utils/utils";
import { assert } from "chai";
import { KipperConfigFile, KipperConfigInterpreter, version as kipConfigVersion } from "@kipper/config";
import {
KipperConfigFile,
KipperConfigInterpreter,
version as kipConfigVersion
} from "@kipper/config";
import * as semver from "semver";
import * as path from "node:path";

const basicKipConfig = getFileName("kip-config.basic.json");
const extendsKipConfig = getFileName("kip-config.extended.json");

describe("KipperConfigInterpreter", () => {
describe("loadConfig", () => {
describe("no extends", () => {
const interpreter = new KipperConfigInterpreter();
const interpreter = new KipperConfigInterpreter();
const errorTestCases =[
{ file: getFileName("errors/does-not-exist.json"), error: "FileNotFoundError" },
{ file: getFileName("errors/invalid-json.json"), error: "JSONSyntaxError" },
{ file: getFileName("errors/invalid-version-syntax.json"), error: "InvalidVersionSyntaxError" },
{ file: getFileName("errors/incompatible-version.json"), error: "IncompatibleVersionError" },
{ file: getFileName("errors/invalid-mapping.json"), error: "InvalidMappingSyntaxError" },
{ file: getFileName("errors/unknown-field.json"), error: "UnknownFieldError" },
{ file: getFileName("errors/missing-required-field.json"), error: "ConfigValidationError" },
{ file: getFileName("errors/invalid-type.json"), error: "ConfigValidationError" },
{ file: getFileName("errors/invalid-array-element-type.json"), error: "ConfigValidationError" },
];

describe("no extends", () => {
it("should load a basic config", async () => {
const kipperConfigFile = await KipperConfigFile.fromFile(basicKipConfig, "utf8");
const config = await interpreter.loadConfig(kipperConfigFile);
Expand All @@ -27,10 +44,64 @@ describe("KipperConfigInterpreter", () => {
{ src: `${pwd}/test/kipper-files/main.kip`, outDir: `${pwd}/dist` }
],
resources: [
{ src: `${pwd}/img/icon.png`, out: `${pwd}/dist/img/icon.png` }
{ src: `${pwd}/img/icon.png`, out: `${pwd}/dist/img/icon.png` },
{ src: `${pwd}/img/Kipper-Logo-without-head.png`, out: `${pwd}/dist/new-img-folder/icon.png` },
]
});
});

errorTestCases.forEach(({ file, error }) => {
it(`should throw '${error}' if the file is '${path.basename(file)}'`, async () => {
try {
const kipperConfigFile = await KipperConfigFile.fromFile(file, "utf8");
await interpreter.loadConfig(kipperConfigFile);
assert.fail("Expected an error to be thrown");
} catch (err) {
assert.equal((<Error>err).constructor.name, error);
}
});
});
});

describe("with extends", () => {
it("should load a config with extends", async () => {
const kipperConfigFile = await KipperConfigFile.fromFile(extendsKipConfig, "utf8");
const config = await interpreter.loadConfig(kipperConfigFile);

const pwd = process.cwd();
assert.deepEqual(config.raw, {
basePath: pwd,
srcDir: `${pwd}/test/kipper-files`,
outDir: `${pwd}/dist`,
compiler: {
version: semver.parse(semver.clean(kipConfigVersion))!,
target: "ts"
},
files: [
{ src: `${pwd}/test/kipper-files/multi-function-definition.kip`, outDir: `${pwd}/dist` },
{ src: `${pwd}/test/kipper-files/multi-function-call.kip`, outDir: `${pwd}/dist` },
{ src: `${pwd}/test/kipper-files/main.kip`, outDir: `${pwd}/dist` },
],
resources: [
{ src: `${pwd}/img/Kipper-Logo-with-head.png`, out: `${pwd}/dist/img/Kipper-Logo-with-head.png` },
{ src: `${pwd}/img/Kipper-Icon.png`, out: `${pwd}/dist/img/Kipper-Icon.png` },
{ src: `${pwd}/img/icon.png`, out: `${pwd}/dist/img/icon.png` },
{ src: `${pwd}/img/Kipper-Logo-without-head.png`, out: `${pwd}/dist/new-img-folder/icon.png` },
],
});
});

errorTestCases.forEach(({ file, error }) => {
it(`should throw '${error}' if the file is '${path.basename(file)}'`, async () => {
try {
const kipperConfigFile = await KipperConfigFile.fromFile(file, "utf8");
await interpreter.loadConfig(kipperConfigFile);
assert.fail("Expected an error to be thrown");
} catch (err) {
assert.equal((<Error>err).constructor.name, error);
}
});
});
});
});
});

0 comments on commit 78a216b

Please sign in to comment.