resources/tpl: Add a test for resources.Get

Updates #10101
This commit is contained in:
Bjørn Erik Pedersen 2022-11-17 17:17:36 +01:00
parent db945a6ed2
commit a99fed4852
2 changed files with 33 additions and 3 deletions

View file

@ -98,3 +98,32 @@ func TestCopyPageShouldFail(t *testing.T) {
b.Assert(err, qt.IsNotNil) b.Assert(err, qt.IsNotNil)
} }
func TestGet(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
baseURL = "http://example.com/blog"
-- assets/images/pixel.png --
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
-- layouts/index.html --
{{ with resources.Get "images/pixel.png" }}Image OK{{ else }}Image not found{{ end }}
{{ with resources.Get "" }}Failed{{ else }}Empty string not found{{ end }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
}).Build()
b.AssertFileContent("public/index.html", `
Image OK
Empty string not found
`)
}

View file

@ -125,13 +125,14 @@ func (ns *Namespace) Copy(s any, r resource.Resource) (resource.Resource, error)
func (ns *Namespace) Get(filename any) resource.Resource { func (ns *Namespace) Get(filename any) resource.Resource {
filenamestr, err := cast.ToStringE(filename) filenamestr, err := cast.ToStringE(filename)
if err != nil {
panic(err)
}
if filenamestr == "" { if filenamestr == "" {
return nil return nil
} }
if err != nil {
panic(err)
}
r, err := ns.createClient.Get(filenamestr) r, err := ns.createClient.Get(filenamestr)
if err != nil { if err != nil {
panic(err) panic(err)