I'm trying to test an implementation of a simple web server in Go using the Gin.
The service has a single endpoint rendering HTML.
server.go
// Serve triggers the server initialization
func Serve(addr string) {
if err := serverEngine().Run(addr); err != nil {
log.Fatalf("could not serve on %s: %s", addr, err)
}
}
func serverEngine() *gin.Engine {
eng := gin.Default()
// Register resource handlers
eng.LoadHTMLGlob("tmpl/*")
eng.GET("/", indexHandler)
return eng
}
func indexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "foo",
})
}
server_test.go
func TestServe(t *testing.T) {
timeOut := time.Duration(3) * time.Second
server := httptest.NewServer(serverEngine())
defer server.Close()
// fixes weird double ':' problem
port := server.URL[len(server.URL)-5:]
_, err := net.DialTimeout("tcp", "localhost:"+port, timeOut)
if err != nil {
t.Errorf("failed to dial server: %s", err)
}
}
When I run the code everything works fine. But when running the unit tests it panics with the following message:
--- FAIL: TestServe (0.00s)
panic: html/template: pattern matches no files: `tmpl/*` [recovered]
panic: html/template: pattern matches no files: `tmpl/*`
Project structure:
.
├── main.go
└── server
├── server.go
├── server_test.go
└── tmpl
└── index.tmpl
How can I ensure that the go test
know about the template location at runtime so I can perform this test?