diff --git a/src/services/file/FileFactory.spec.ts b/src/services/file/FileFactory.spec.ts new file mode 100644 index 00000000..73293d55 --- /dev/null +++ b/src/services/file/FileFactory.spec.ts @@ -0,0 +1,105 @@ +import { writeFile } from "fs/promises"; +import { resolve } from "path"; + +import container from "../../container"; +import { DirResult, createTmpDir } from "../../tests/tmp-dir"; +import { FileFactory } from "./FileFactory"; +import { JsonFile } from "./JsonFile"; +import { TomlFile } from "./TomlFile"; +import { TypescriptFile } from "./TypescriptFile"; +import { StdFile } from "./StdFile"; + +describe("FileFactory", () => { + let testDir: DirResult; + let fileFactory: FileFactory; + + beforeEach(() => { + // Initialize service before each test to not be confused by cache + container.snapshot(); + fileFactory = container.get(FileFactory); + }); + + afterEach(() => { + container.restore(); + testDir?.removeCallback(); + }); + + describe("fromFile", () => { + it("should create a file from a file path", async () => { + testDir = await createTmpDir(); + + const filepath = resolve(testDir.name, "test.txt"); + await writeFile(filepath, "test"); + + const file = await fileFactory.fromFile(filepath); + expect(file).toBeInstanceOf(StdFile); + + const content = await file.getContent(); + expect(content).toBe("test"); + }); + + it("should create a file from a file path with encoding", async () => { + testDir = await createTmpDir(); + + const filepath = resolve(testDir.name, "test.txt"); + await writeFile(filepath, "test"); + + const file = await fileFactory.fromFile(filepath, "utf8"); + expect(file).toBeInstanceOf(StdFile); + + const content = await file.getContent(); + expect(content).toBe("test"); + }); + + it("should throw an error when the file does not exist", async () => { + await expect(fileFactory.fromFile("non-existing-file")).rejects.toThrow( + 'File "non-existing-file" does not exist' + ); + }); + }); + + describe("fromString", () => { + it("should create a file from a string", async () => { + const file = await fileFactory.fromString("test", "test.txt"); + + expect(file).toBeInstanceOf(StdFile); + expect(await file.getContent()).toBe("test"); + }); + + it("should create a file from a string with encoding", async () => { + const file = await fileFactory.fromString("test", "test.txt", "utf8"); + + expect(file).toBeInstanceOf(StdFile); + expect(await file.getContent()).toBe("test"); + }); + + it("should create a Json file from a string", async () => { + const file = await fileFactory.fromString('{"test": "test"}', "test.json"); + + expect(file).toBeInstanceOf(JsonFile); + const jsonFile = file as JsonFile; + + expect(await jsonFile.getContent()).toBe('{\n "test": "test"\n}'); + expect(await jsonFile.getData()).toEqual({ test: "test" }); + expect(await jsonFile.getData("test")).toEqual("test"); + expect(await jsonFile.getData("unknown")).toBeUndefined(); + }); + + it("should create a Toml file from a string", async () => { + const file = await fileFactory.fromString("test = 'test'", "test.toml"); + + expect(file).toBeInstanceOf(TomlFile); + expect(await file.getContent()).toBe('test = "test"\n'); + expect(await (file as TomlFile).getData()).toEqual({ test: "test" }); + expect(await (file as TomlFile).getData("test")).toEqual("test"); + expect(await (file as TomlFile).getData("unknown")).toBeUndefined(); + }); + + it("should create a Typescript file from a string", async () => { + const file = await fileFactory.fromString("console.log('test')", "test.ts"); + + expect(file).toBeInstanceOf(TypescriptFile); + expect(await file.getContent()).toBe("console.log('test')"); + }); + }); +}); diff --git a/src/services/file/FileFactory.ts b/src/services/file/FileFactory.ts index c53f1cc0..c868a80f 100644 --- a/src/services/file/FileFactory.ts +++ b/src/services/file/FileFactory.ts @@ -61,7 +61,11 @@ export class FileFactory { } } - fromString(content: string, file: string, encoding: BufferEncoding = "utf8"): StdFile { + fromString( + content: string, + file: string, + encoding: BufferEncoding = "utf8" + ): StdFile | JsonFile | TomlFile | TypescriptFile { const args = [ this.cliService, this.directoryService, diff --git a/src/services/file/TomlFile.ts b/src/services/file/TomlFile.ts index 97e3c436..a26dc505 100644 --- a/src/services/file/TomlFile.ts +++ b/src/services/file/TomlFile.ts @@ -29,8 +29,9 @@ export class TomlFile extends StdFile { return this.setContent(stringify(newData)); } - getData(property?: undefined): JsonMap | undefined; - getData(property?: string): AnyJson | undefined { + getData(): JsonMap | undefined; + getData(property: string | undefined): AnyJson | undefined; + getData(property: string | undefined = undefined): AnyJson | undefined { if (!this.data) { return this.data; } diff --git a/src/services/template/adapters/EtaAdapter.ts b/src/services/template/adapters/EtaAdapter.ts index 299a4588..c7f8663c 100644 --- a/src/services/template/adapters/EtaAdapter.ts +++ b/src/services/template/adapters/EtaAdapter.ts @@ -10,20 +10,22 @@ import { TemplateAdapter } from "./TemplateAdapter"; import { TemplateAdapterHelper } from "./TemplateAdapterHelper"; export class EtaAdapter implements TemplateAdapter { - private readonly eta: Eta = new Eta({ - views: this.templateFileService.getTemplateDirectory(), - debug: true, - cache: true, // Make Eta cache templates - autoEscape: false, // Not automatically XML-escape interpolations - autoTrim: false, // automatic whitespace trimming, - }); + private readonly eta: Eta; private readonly compiledTemplates: Map = new Map(); constructor( @inject(TemplateFileService) private readonly templateFileService: TemplateFileService, @inject(TemplateAdapterHelper) private readonly templateAdapterHelper: TemplateAdapterHelper - ) {} + ) { + this.eta = new Eta({ + views: this.templateFileService.getTemplateDirectory(), + debug: true, + cache: true, // Make Eta cache templates + autoEscape: false, // Not automatically XML-escape interpolations + autoTrim: false, // automatic whitespace trimming, + }); + } async renderTemplateString(template: string, context: TemplateContext): Promise { const compiledTemplate = await this.getCompiledTemplateString(template, template);