diff --git a/tpl/resources/integration_test.go b/tpl/resources/integration_test.go index 7662f7b6b..0e0a29a98 100644 --- a/tpl/resources/integration_test.go +++ b/tpl/resources/integration_test.go @@ -98,3 +98,32 @@ func TestCopyPageShouldFail(t *testing.T) { 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 + + `) + +} diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index d8f06761d..fe6fd0434 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -125,13 +125,14 @@ func (ns *Namespace) Copy(s any, r resource.Resource) (resource.Resource, error) func (ns *Namespace) Get(filename any) resource.Resource { filenamestr, err := cast.ToStringE(filename) + if err != nil { + panic(err) + } if filenamestr == "" { return nil } - if err != nil { - panic(err) - } + r, err := ns.createClient.Get(filenamestr) if err != nil { panic(err)