karma-ng-hamlet2js-preprocessor
Version:
A karma plugin to compile Hamlet HTML templates to JavaScript on the fly.
152 lines (125 loc) • 5.95 kB
JavaScript
var child_proc = require("child_process");
var util = require("util");
var spies = require("./spies");
var defTemplate =
"import qualified Data.ByteString.Lazy as BL\n" +
"import Yesod.EmbeddedStatic.AngularJavascript (hamletTestTemplate)\n" +
"\n" +
"main = BL.putStr $(hamletTestTemplate \"%s\")";
var sandbox = ["-package-db","--ghc-arg=--ghc-arg=/home/wuzzeb/projects/sandbox-yesod/i386-linux-ghc-7.6.3-packages.conf.d"];
describe("converts hamlet to javascript", function() {
var curDir = process.cwd();
//Reset current directory before each test
beforeEach(function() {process.chdir(curDir);});
it("processes template with default config", function() {
var stdinSpy = spies.mockExec(null, "The result", "The error");
var doneSpy = spies.callHamlet(undefined, "file1", "origpath");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("The result");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "origpath"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("detects the cabal sandbox", function() {
process.chdir("test/spec");
var stdinSpy = spies.mockExec(null, "Output of process", "The error");
var doneSpy = spies.callHamlet(undefined, "file1", "origpath");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell"].concat(sandbox),
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Output of process");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "origpath"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("uses extra ghc arguments", function() {
var cfg = { extraGhcArgs: ["--some-argument", "--another-arg"] };
var stdinSpy = spies.mockExec(null, "Hello world", "The error");
var doneSpy = spies.callHamlet(cfg, "foo", "bar");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["--some-argument", "--another-arg", "-XTemplateHaskell"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Hello world");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "bar"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("uses a custom runghc", function() {
var cfg = { runghc: "mycustomrunghc" };
var stdinSpy = spies.mockExec(null, "Hello world", "The error");
var doneSpy = spies.callHamlet(cfg, "foo", "bar");
expect(child_proc.execFile).toHaveBeenCalledWith(
"mycustomrunghc",
["-XTemplateHaskell"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Hello world");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "bar"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("uses a custom haskell program", function() {
var cfg = { hsProg: "a custom program processing '%s' file" };
var stdinSpy = spies.mockExec(null, "Result of custom prog", "The error");
var doneSpy = spies.callHamlet(cfg, "abc", "def");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Result of custom prog");
expect(stdinSpy.write).toHaveBeenCalledWith("a custom program processing 'def' file");
expect(stdinSpy.end).toHaveBeenCalled();
});
it("handles an exec error", function() {
var stdinSpy = spies.mockExec(new Error("Test error"), "The output", "The error");
var doneSpy = spies.callHamlet(undefined, "abc", "def");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith(""); // error calls empty template
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "def"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("uses a custom package DB", function() {
var cfg = {packageDb : "/path/to/package/db" };
var stdinSpy = spies.mockExec(null, "Hello world", "The error");
var doneSpy = spies.callHamlet(cfg, "foo", "bar");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell", "-package-db", "--ghc-arg=--ghc-arg=/path/to/package/db"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Hello world");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "bar"));
expect(stdinSpy.end).toHaveBeenCalled();
});
it("uses a custom package DB even when there exists a cabal sandbox", function() {
process.chdir("test/spec");
var cfg = {packageDb : "/path/to/package/db" };
var stdinSpy = spies.mockExec(null, "Hello world", "The error");
var doneSpy = spies.callHamlet(cfg, "foo", "bar");
expect(child_proc.execFile).toHaveBeenCalledWith(
"runghc",
["-XTemplateHaskell", "-package-db", "--ghc-arg=--ghc-arg=/path/to/package/db"],
{},
jasmine.any(Function)
);
expect(doneSpy).toHaveBeenCalledWith("Hello world");
expect(stdinSpy.write).toHaveBeenCalledWith(util.format(defTemplate, "bar"));
expect(stdinSpy.end).toHaveBeenCalled();
});
});