diff --git a/test/config-files/kip-config.basic.json b/test/config-files/kip-config.basic.json index aec9826e5..1ef8ec835 100644 --- a/test/config-files/kip-config.basic.json +++ b/test/config-files/kip-config.basic.json @@ -1,12 +1,14 @@ { + "outDir": "dist", + "srcDir": "test/kipper-files", "compiler": { "version": "*", "target": "ts" }, "files": [ - "src/index.kip" + "test/kipper-files/main.kip" ], "resources": [ - "my-image.png" + "img/icon.png" ] } diff --git a/test/module/config/kipper-config-file-interpreter.ts b/test/module/config/kipper-config-file-interpreter.ts index d5846399b..e2a04614e 100644 --- a/test/module/config/kipper-config-file-interpreter.ts +++ b/test/module/config/kipper-config-file-interpreter.ts @@ -1,3 +1,36 @@ +import { getFileName } from "./utils/utils"; +import { assert } from "chai"; +import { KipperConfigFile, KipperConfigInterpreter, version as kipConfigVersion } from "@kipper/config"; +import * as semver from "semver"; + +const basicKipConfig = getFileName("kip-config.basic.json"); + describe("KipperConfigInterpreter", () => { + describe("loadConfig", () => { + describe("no extends", () => { + const interpreter = new KipperConfigInterpreter(); + + it("should load a basic config", async () => { + const kipperConfigFile = await KipperConfigFile.fromFile(basicKipConfig, "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/main.kip`, outDir: `${pwd}/dist` } + ], + resources: [ + { src: `${pwd}/img/icon.png`, out: `${pwd}/dist/img/icon.png` } + ] + }); + }); + }); + }); }); diff --git a/test/module/config/kipper-config-file.test.ts b/test/module/config/kipper-config-file.test.ts index f3ab4ea87..628deb864 100644 --- a/test/module/config/kipper-config-file.test.ts +++ b/test/module/config/kipper-config-file.test.ts @@ -1,3 +1,45 @@ +import { FileNotFoundError, KipperConfigFile } from "@kipper/config"; +import { getFileName } from "./utils/utils"; +import { assert } from "chai"; +import * as path from "path"; +import * as fs from "node:fs/promises"; + +const basicKipConfig = getFileName("kip-config.basic.json"); + describe("KipperConfigFile", () => { + const encoding = "utf8"; + + describe("fromString", () => { + it("should return a KipperConfigFile instance", () => { + const kipConfig = KipperConfigFile.fromString("{}", encoding); + assert.instanceOf(kipConfig, KipperConfigFile); + assert.strictEqual(kipConfig.content, "{}"); + assert.deepEqual(kipConfig.parsedJSON, {}); + assert.strictEqual(kipConfig.fileName, ""); + assert.strictEqual(kipConfig.encoding, encoding); + }); + }); + + describe("fromFile", () => { + it("should return a KipperConfigFile instance", async () => { + // First get the file content to check what we are expecting + const fileContent = await fs.readFile(basicKipConfig, { encoding }); + + const kipConfig = await KipperConfigFile.fromFile(basicKipConfig, encoding); + assert.instanceOf(kipConfig, KipperConfigFile); + assert.strictEqual(kipConfig.content, fileContent); + assert.deepEqual(kipConfig.parsedJSON, JSON.parse(fileContent)); + assert.strictEqual(kipConfig.fileName, path.basename(basicKipConfig)); + assert.strictEqual(kipConfig.encoding, encoding); + }); + it("should throw an error if the file does not exist", async () => { + try { + await KipperConfigFile.fromFile(getFileName("does-not-exist.json"), encoding); + assert.fail("Expected an error to be thrown"); + } catch (error) { + assert.instanceOf(error, FileNotFoundError); + } + }); + }); }); diff --git a/test/module/config/utils/utils.ts b/test/module/config/utils/utils.ts new file mode 100644 index 000000000..3a57b02a0 --- /dev/null +++ b/test/module/config/utils/utils.ts @@ -0,0 +1,5 @@ +import * as path from "path"; + +export function getFileName(pathString: string): string { + return path.resolve(`${__dirname}/../../../config-files/${pathString}`); +}