Skip to content

Commit

Permalink
chore: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Nov 10, 2023
1 parent 3b2bf40 commit 93ae997
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestIndexHandler_rangeOf(t *testing.T) {
}

func TestCustomTemplates(t *testing.T) {
t.Run("fails to create handler if template is missing", func(t *testing.T) {
t.Run("missing", func(t *testing.T) {
for _, name := range []string{"index.html", "package.html", "404.html"} {
templatesText := map[string]string{
"index.html": "index",
Expand All @@ -295,7 +295,7 @@ func TestCustomTemplates(t *testing.T) {
}
})

t.Run("only replaces correct templates", func(t *testing.T) {
t.Run("replace", func(t *testing.T) {
templates := getTestTemplates(t, map[string]string{
"404.html": "not found: {{ .Path }}",
})
Expand Down Expand Up @@ -332,7 +332,7 @@ func BenchmarkHandlerDispatch(b *testing.B) {
Repo: "github.com/yarpc/metrics",
},
},
}, template.Must(_templates.Clone()))
}, getTestTemplates(b, nil))
require.NoError(b, err)
resw := new(nopResponseWriter)

Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ func getCombinedTemplates(dir string) (*template.Template, error) {
if err != nil {
return nil, err
}

Check warning on line 54 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L53-L54

Added lines #L53 - L54 were not covered by tests
templates, err = templates.ParseGlob(filepath.Join(dir, "*.html"))
return templates, err
return templates.ParseGlob(filepath.Join(dir, "*.html"))
}
10 changes: 5 additions & 5 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ func AssertResponse(t *testing.T, rr *httptest.ResponseRecorder, code int, want
// getTestTemplates returns a [template.Template] object with the default templates,
// overwritten by the [overrideTemplates]. If [overrideTemplates] is nil, the returned
// templates are a clone of the global [_templates].
func getTestTemplates(t *testing.T, overrideTemplates map[string]string) *template.Template {
func getTestTemplates(tb testing.TB, overrideTemplates map[string]string) *template.Template {
if len(overrideTemplates) == 0 {
// We must clone! Cloning can only be done before templates are executed. Therefore,
// we cannot run some tests without cloning, and then attempt cloning it. It'll panic.
templates, err := _templates.Clone()
require.NoError(t, err)
require.NoError(tb, err)
return templates
}

templatesDir := t.TempDir() // This is automatically removed at the end of the test.
templatesDir := tb.TempDir() // This is automatically removed at the end of the test.
for name, content := range overrideTemplates {
err := os.WriteFile(filepath.Join(templatesDir, name), []byte(content), 0666)
require.NoError(t, err)
require.NoError(tb, err)
}

templates, err := getCombinedTemplates(templatesDir)
require.NoError(t, err)
require.NoError(tb, err)
return templates
}

Expand Down

0 comments on commit 93ae997

Please sign in to comment.