Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for FileFactory #756

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
neilime marked this conversation as resolved.
Show resolved Hide resolved
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
Loading