Files
forge/modules/templates/scopedtmpl/scopedtmpl_test.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

97 lines
2.3 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package scopedtmpl
import (
"bytes"
"html/template"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestScopedTemplateSetFuncMap(t *testing.T) {
all := template.New("")
all.Funcs(template.FuncMap{"CtxFunc": func(s string) string {
return "default"
}})
_, err := all.New("base").Parse(`{{CtxFunc "base"}}`)
assert.NoError(t, err)
_, err = all.New("test").Parse(strings.TrimSpace(`
{{template "base"}}
{{CtxFunc "test"}}
{{template "base"}}
{{CtxFunc "test"}}
`))
assert.NoError(t, err)
ts, err := newScopedTemplateSet(all, "test")
assert.NoError(t, err)
// try to use different CtxFunc to render concurrently
funcMap1 := template.FuncMap{
"CtxFunc": func(s string) string {
time.Sleep(100 * time.Millisecond)
return s + "1"
},
}
funcMap2 := template.FuncMap{
"CtxFunc": func(s string) string {
time.Sleep(100 * time.Millisecond)
return s + "2"
},
}
out1 := bytes.Buffer{}
out2 := bytes.Buffer{}
wg := sync.WaitGroup{}
wg.Go(func() {
err := ts.newExecutor(funcMap1).Execute(&out1, nil)
assert.NoError(t, err)
})
wg.Go(func() {
err := ts.newExecutor(funcMap2).Execute(&out2, nil)
assert.NoError(t, err)
})
wg.Wait()
assert.Equal(t, "base1\ntest1\nbase1\ntest1", out1.String())
assert.Equal(t, "base2\ntest2\nbase2\ntest2", out2.String())
}
func TestScopedTemplateSetEscape(t *testing.T) {
all := template.New("")
_, err := all.New("base").Parse(`<a href="?q={{.param}}">{{.text}}</a>`)
assert.NoError(t, err)
_, err = all.New("test").Parse(`{{template "base" .}}<form action="?q={{.param}}">{{.text}}</form>`)
assert.NoError(t, err)
ts, err := newScopedTemplateSet(all, "test")
assert.NoError(t, err)
out := bytes.Buffer{}
err = ts.newExecutor(nil).Execute(&out, map[string]string{"param": "/", "text": "<"})
assert.NoError(t, err)
assert.Equal(t, `<a href="?q=%2f">&lt;</a><form action="?q=%2f">&lt;</form>`, out.String())
}
func TestScopedTemplateSetUnsafe(t *testing.T) {
all := template.New("")
_, err := all.New("test").Parse(`<a href="{{if true}}?{{end}}a={{.param}}"></a>`)
assert.NoError(t, err)
_, err = newScopedTemplateSet(all, "test")
assert.ErrorContains(t, err, "appears in an ambiguous context within a URL")
}