From 89dbee85f88d8a3ae2a0adb589f14c03c2268fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 10 Feb 2020 18:59:53 +0000 Subject: [PATCH] fix(backend): add more test coverage --- cmd/karma/assets.go | 3 ++- cmd/karma/assets_test.go | 46 ++++++++++++++++++++++++++++++++++ ui/public/go-test-invalid.html | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 ui/public/go-test-invalid.html diff --git a/cmd/karma/assets.go b/cmd/karma/assets.go index abec33384..cc0a10a92 100644 --- a/cmd/karma/assets.go +++ b/cmd/karma/assets.go @@ -9,8 +9,9 @@ import ( "strings" "time" - assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/gin-gonic/gin" + + assetfs "github.com/elazarl/go-bindata-assetfs" log "github.com/sirupsen/logrus" ) diff --git a/cmd/karma/assets_test.go b/cmd/karma/assets_test.go index a899dcc35..16ae1c011 100644 --- a/cmd/karma/assets_test.go +++ b/cmd/karma/assets_test.go @@ -1,10 +1,13 @@ package main import ( + "html/template" "net/http/httptest" "testing" "github.com/prymitive/karma/internal/config" + + log "github.com/sirupsen/logrus" ) type customizationAssetsTest struct { @@ -92,3 +95,46 @@ func TestStaticExpires404(t *testing.T) { t.Errorf("Got Expires: '%s' header on a 404 /static/ response", resp.Result().Header.Get("Expires")) } } + +func TestLoadTemplateChained(t *testing.T) { + var tmpl *template.Template + tmpl = loadTemplate(tmpl, "ui/build/index.html") + if tmpl == nil { + t.Errorf("loadTemplate returned nil") + } + + tmpl = loadTemplate(tmpl, "ui/build/favicon.ico") + if tmpl == nil { + t.Errorf("loadTemplate returned nil") + } + + if tmpl.Name() != "ui/build/index.html" { + t.Errorf("tmpl.Name() returned %q", tmpl.Name()) + } +} + +func TestLoadTemplateMissing(t *testing.T) { + log.SetLevel(log.PanicLevel) + defer func() { log.StandardLogger().ExitFunc = nil }() + var wasFatal bool + log.StandardLogger().ExitFunc = func(int) { wasFatal = true } + + loadTemplate(nil, "/this/file/does/not/exist") + + if !wasFatal { + t.Error("loadTemplate() with invalid path didn't cause log.Fatal()") + } +} + +func TestLoadTemplateUnparsable(t *testing.T) { + log.SetLevel(log.PanicLevel) + defer func() { log.StandardLogger().ExitFunc = nil }() + var wasFatal bool + log.StandardLogger().ExitFunc = func(int) { wasFatal = true } + + loadTemplate(nil, "ui/build/go-test-invalid.html") + + if !wasFatal { + t.Error("loadTemplate() with unparsable file didn't cause log.Fatal()") + } +} diff --git a/ui/public/go-test-invalid.html b/ui/public/go-test-invalid.html new file mode 100644 index 000000000..a83a4b166 --- /dev/null +++ b/ui/public/go-test-invalid.html @@ -0,0 +1 @@ +hello{{range