Skip to content

Commit

Permalink
test: add tests for FileFactory
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <[email protected]>
  • Loading branch information
neilime committed Nov 9, 2024
1 parent 73fcd62 commit 9a34bb9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 11 deletions.
105 changes: 105 additions & 0 deletions src/services/file/FileFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -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')");
});
});
});
6 changes: 5 additions & 1 deletion src/services/file/FileFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/services/file/TomlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 10 additions & 8 deletions src/services/template/adapters/EtaAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TemplateFunction> = 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<string> {
const compiledTemplate = await this.getCompiledTemplateString(template, template);
Expand Down

0 comments on commit 9a34bb9

Please sign in to comment.