-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: move templatecheck tests to their own package
The new package the templatecheck tests are in is not a transitive dependency of cmd/pkgsite, so that cmd/pkgsite no longer has a transitive test dependency on github.com/jba/templatecheck. To make this work we had to expose the templates from the internal/frontend and internal/godoc packages for the tests to use. For golang/go#61399 Change-Id: I1290ec24b53af77a82671c8fc068867e12857ce3 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/550936 LUCI-TryBot-Result: Go LUCI <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gopher Robot <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
- Loading branch information
Showing
10 changed files
with
226 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package templates | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"path" | ||
"strings" | ||
|
||
"github.com/google/safehtml/template" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
var templateFuncs = template.FuncMap{ | ||
"add": func(i, j int) int { return i + j }, | ||
"subtract": func(i, j int) int { return i - j }, | ||
"pluralize": func(i int, s string) string { | ||
if i == 1 { | ||
return s | ||
} | ||
return s + "s" | ||
}, | ||
"commaseparate": func(s []string) string { | ||
return strings.Join(s, ", ") | ||
}, | ||
"stripscheme": stripScheme, | ||
"capitalize": cases.Title(language.Und).String, | ||
"queryescape": url.QueryEscape, | ||
} | ||
|
||
func stripScheme(url string) string { | ||
if i := strings.Index(url, "://"); i > 0 { | ||
return url[i+len("://"):] | ||
} | ||
return url | ||
} | ||
|
||
// ParsePageTemplates parses html templates contained in the given filesystem in | ||
// order to generate a map of Name->*template.Template. | ||
// | ||
// Separate templates are used so that certain contextual functions (e.g. | ||
// templateName) can be bound independently for each page. | ||
// | ||
// Templates in directories prefixed with an underscore are considered helper | ||
// templates and parsed together with the files in each base directory. | ||
func ParsePageTemplates(fsys template.TrustedFS) (map[string]*template.Template, error) { | ||
templates := make(map[string]*template.Template) | ||
htmlSets := [][]string{ | ||
{"about"}, | ||
{"badge"}, | ||
{"error"}, | ||
{"fetch"}, | ||
{"homepage"}, | ||
{"license-policy"}, | ||
{"search"}, | ||
{"search-help"}, | ||
{"styleguide"}, | ||
{"subrepo"}, | ||
{"unit/importedby", "unit"}, | ||
{"unit/imports", "unit"}, | ||
{"unit/licenses", "unit"}, | ||
{"unit/main", "unit"}, | ||
{"unit/versions", "unit"}, | ||
{"vuln"}, | ||
{"vuln/main", "vuln"}, | ||
{"vuln/list", "vuln"}, | ||
{"vuln/entry", "vuln"}, | ||
} | ||
|
||
for _, set := range htmlSets { | ||
t, err := template.New("frontend.tmpl").Funcs(templateFuncs).ParseFS(fsys, "frontend/*.tmpl") | ||
if err != nil { | ||
return nil, fmt.Errorf("ParseFS: %v", err) | ||
} | ||
helperGlob := "shared/*/*.tmpl" | ||
if _, err := t.ParseFS(fsys, helperGlob); err != nil { | ||
return nil, fmt.Errorf("ParseFS(%q): %v", helperGlob, err) | ||
} | ||
for _, f := range set { | ||
if _, err := t.ParseFS(fsys, path.Join("frontend", f, "*.tmpl")); err != nil { | ||
return nil, fmt.Errorf("ParseFS(%v): %v", f, err) | ||
} | ||
} | ||
templates[set[0]] = t | ||
} | ||
|
||
return templates, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package templates | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestStripScheme(t *testing.T) { | ||
for _, test := range []struct { | ||
url, want string | ||
}{ | ||
{"http://github.com", "github.com"}, | ||
{"https://github.com/path/to/something", "github.com/path/to/something"}, | ||
{"example.com", "example.com"}, | ||
{"chrome-extension://abcd", "abcd"}, | ||
{"nonwellformed.com/path?://query=1", "query=1"}, | ||
} { | ||
if got := stripScheme(test.url); got != test.want { | ||
t.Errorf("%q: got %q, want %q", test.url, got, test.want) | ||
} | ||
} | ||
} |
Oops, something went wrong.