From 1e83f841a383dfa23c15e13614dc2d9b2798a456 Mon Sep 17 00:00:00 2001 From: Arun N Kumar Date: Sat, 18 Nov 2023 14:10:32 -0500 Subject: [PATCH 1/2] Fixes for #265 - Enable support for default extensions. Added unit test coverage to the two new possibilities and it works for existing .eta formats as well. --- src/config.ts | 4 ++ src/file-handling.ts | 3 +- test/file-handling.spec.ts | 18 +++++++ test/file-handling.spec.ts.bak | 70 ++++++++++++++++++++++++++++ test/templates/template-extn.tmpl | 1 + test/templates/template-without-extn | 1 + 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/file-handling.spec.ts.bak create mode 100644 test/templates/template-extn.tmpl create mode 100644 test/templates/template-without-extn diff --git a/src/config.ts b/src/config.ts index dbb180d6..1c49d163 100644 --- a/src/config.ts +++ b/src/config.ts @@ -69,6 +69,9 @@ export interface EtaConfig { /** Directory that contains templates */ views?: string; + + /** Control template file extension defaults. Default `.eta` */ + defaultExtension?: string; } /* END TYPES */ @@ -95,6 +98,7 @@ const defaultConfig: EtaConfig = { tags: ["<%", "%>"], useWith: false, varName: "it", + defaultExtension: ".eta", }; export { defaultConfig }; diff --git a/src/file-handling.ts b/src/file-handling.ts index 816aa364..4508bee6 100644 --- a/src/file-handling.ts +++ b/src/file-handling.ts @@ -40,6 +40,7 @@ export function resolvePath( } const baseFilePath = options && options.filepath; + const defaultExtension = this.config.defaultExtension === undefined ? ".eta" : this.config.defaultExtension; // how we index cached template paths const cacheIndex = JSON.stringify({ @@ -48,7 +49,7 @@ export function resolvePath( views: this.config.views, }); - templatePath += path.extname(templatePath) ? "" : ".eta"; + templatePath += path.extname(templatePath) ? "" : defaultExtension; // if the file was included from another template if (baseFilePath) { diff --git a/test/file-handling.spec.ts b/test/file-handling.spec.ts index 9421c44e..66ae623c 100644 --- a/test/file-handling.spec.ts +++ b/test/file-handling.spec.ts @@ -68,3 +68,21 @@ describe("file handling errors", () => { }).toThrow(); }); }); + +describe("filepath default extension tests", () => { + console.log("Templates are in ", viewsDir) + + it("works with defaultExtension", async () => { + const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: ".tmpl" }); + const templateResult = await eta.render("template-extn", { name: "TMPL Run" }); + + expect(templateResult).toEqual(`Hi TMPL Run`); + }); + + it("works with no extension", async () => { + const eta = new Eta({ views: viewsDir, cache: true, defaultExtension: "" }); + const templateResult = await eta.render("template-without-extn", { name: "TMPL Run" }); + + expect(templateResult).toEqual(`Hi TMPL Run`); + }); +}) diff --git a/test/file-handling.spec.ts.bak b/test/file-handling.spec.ts.bak new file mode 100644 index 00000000..9421c44e --- /dev/null +++ b/test/file-handling.spec.ts.bak @@ -0,0 +1,70 @@ +/* global it, expect, describe */ + +import path from "node:path"; + +import { Eta } from "../src/index"; + +const viewsDir = path.join(__dirname, "templates"); + +const eta = new Eta({ views: viewsDir, cache: true }); + +describe("Filepath caching", () => { + it("Filepath caching works as expected", async () => { + // This test renders templates/has-include.eta with caching enabled, then checks to make sure + // `config.filepathCache` contains the expected result afterward + + const templateResult = await eta.render("has-include", {}); + + expect(templateResult).toEqual( + `This is the outermost template. Now we'll include a partial + +=========================================================== +This is a partial. +Hi Test Runner` + ); + + // The cache is indexed by {filename, path, root, views} (JSON.stringify ignores keys with undefined as their value) + + // Filepath caching is based on the premise that given the same path, includer filename, root directory, and views directory (or directories) + // the getPath function will always return the same result (assuming that caching is enabled and we're not expecting the templates to change) + + const pathToPartial = `{"filename":"${viewsDir}/has-include.eta","path":"./partial","views":"${viewsDir}"}`; + + const pathToSimple = `{"filename":"${viewsDir}/partial.eta","path":"./simple","views":"${viewsDir}"}`; + + expect(eta.filepathCache).toEqual({ + [pathToPartial]: `${viewsDir}/partial.eta`, + [pathToSimple]: `${viewsDir}/simple.eta`, + }); + }); +}); + +describe("file handling errors", () => { + const eta = new Eta({ views: viewsDir }); + + it("throws if accessing a file outside the views directory", () => { + expect(() => { + eta.render("../../sensitive-file.json", {}); + }).toThrow(); + }); + + it("throws if accessing a partial outside the views directory", () => { + expect(() => { + eta.render("out-of-views-dir", {}); + }).toThrow(); + }); + + it("throws if template doesn't exist", () => { + expect(() => { + eta.render("nonexistent.eta", {}); + }).toThrow(/Could not find template/); + }); + + it("throws if views directory isn't defined", () => { + const testEta = new Eta(); + + expect(() => { + testEta.render("simple.eta", {}); + }).toThrow(); + }); +}); diff --git a/test/templates/template-extn.tmpl b/test/templates/template-extn.tmpl new file mode 100644 index 00000000..3d352ecb --- /dev/null +++ b/test/templates/template-extn.tmpl @@ -0,0 +1 @@ +Hi <%= it.name %> \ No newline at end of file diff --git a/test/templates/template-without-extn b/test/templates/template-without-extn new file mode 100644 index 00000000..3d352ecb --- /dev/null +++ b/test/templates/template-without-extn @@ -0,0 +1 @@ +Hi <%= it.name %> \ No newline at end of file From 74f45b9204f45f9c4ee76adfb2c16ca7d8211210 Mon Sep 17 00:00:00 2001 From: Arun N Kumar Date: Mon, 27 Nov 2023 21:34:52 -0500 Subject: [PATCH 2/2] Removed .bak file generated from a diff tool --- test/file-handling.spec.ts.bak | 70 ---------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 test/file-handling.spec.ts.bak diff --git a/test/file-handling.spec.ts.bak b/test/file-handling.spec.ts.bak deleted file mode 100644 index 9421c44e..00000000 --- a/test/file-handling.spec.ts.bak +++ /dev/null @@ -1,70 +0,0 @@ -/* global it, expect, describe */ - -import path from "node:path"; - -import { Eta } from "../src/index"; - -const viewsDir = path.join(__dirname, "templates"); - -const eta = new Eta({ views: viewsDir, cache: true }); - -describe("Filepath caching", () => { - it("Filepath caching works as expected", async () => { - // This test renders templates/has-include.eta with caching enabled, then checks to make sure - // `config.filepathCache` contains the expected result afterward - - const templateResult = await eta.render("has-include", {}); - - expect(templateResult).toEqual( - `This is the outermost template. Now we'll include a partial - -=========================================================== -This is a partial. -Hi Test Runner` - ); - - // The cache is indexed by {filename, path, root, views} (JSON.stringify ignores keys with undefined as their value) - - // Filepath caching is based on the premise that given the same path, includer filename, root directory, and views directory (or directories) - // the getPath function will always return the same result (assuming that caching is enabled and we're not expecting the templates to change) - - const pathToPartial = `{"filename":"${viewsDir}/has-include.eta","path":"./partial","views":"${viewsDir}"}`; - - const pathToSimple = `{"filename":"${viewsDir}/partial.eta","path":"./simple","views":"${viewsDir}"}`; - - expect(eta.filepathCache).toEqual({ - [pathToPartial]: `${viewsDir}/partial.eta`, - [pathToSimple]: `${viewsDir}/simple.eta`, - }); - }); -}); - -describe("file handling errors", () => { - const eta = new Eta({ views: viewsDir }); - - it("throws if accessing a file outside the views directory", () => { - expect(() => { - eta.render("../../sensitive-file.json", {}); - }).toThrow(); - }); - - it("throws if accessing a partial outside the views directory", () => { - expect(() => { - eta.render("out-of-views-dir", {}); - }).toThrow(); - }); - - it("throws if template doesn't exist", () => { - expect(() => { - eta.render("nonexistent.eta", {}); - }).toThrow(/Could not find template/); - }); - - it("throws if views directory isn't defined", () => { - const testEta = new Eta(); - - expect(() => { - testEta.render("simple.eta", {}); - }).toThrow(); - }); -});