mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-04-21 17:10:28 +00:00
135 lines
3.4 KiB
Go
135 lines
3.4 KiB
Go
package licenses_test
|
|
|
|
import (
|
|
"html/template"
|
|
"strings"
|
|
"testing"
|
|
|
|
"atcr.io/pkg/appview/licenses"
|
|
)
|
|
|
|
// TestRepositoryPageTemplate demonstrates how the license badges will render
|
|
// in the actual repository.html template
|
|
func TestRepositoryPageTemplate(t *testing.T) {
|
|
funcMap := template.FuncMap{
|
|
"parseLicenses": func(licensesStr string) []licenses.LicenseInfo {
|
|
return licenses.ParseLicenses(licensesStr)
|
|
},
|
|
}
|
|
|
|
// This is the exact template structure from repository.html
|
|
tmplStr := `{{ if .Licenses }}` +
|
|
`{{ range parseLicenses .Licenses }}` +
|
|
`{{ if .IsValid }}` +
|
|
`<a href="{{ .URL }}" target="_blank" rel="noopener noreferrer" class="metadata-badge license-badge" title="{{ .Name }}">{{ .SPDXID }}</a>` +
|
|
`{{ else }}` +
|
|
`<span class="metadata-badge license-badge" title="Custom license: {{ .Name }}">{{ .Name }}</span>` +
|
|
`{{ end }}` +
|
|
`{{ end }}` +
|
|
`{{ end }}`
|
|
|
|
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplStr))
|
|
|
|
tests := []struct {
|
|
name string
|
|
licenses string
|
|
wantContain []string
|
|
wantNotContain []string
|
|
}{
|
|
{
|
|
name: "MIT license",
|
|
licenses: "MIT",
|
|
wantContain: []string{
|
|
`<a href="https://spdx.org/licenses/MIT.html"`,
|
|
`class="metadata-badge license-badge"`,
|
|
`title="MIT License"`,
|
|
`>MIT</a>`,
|
|
},
|
|
},
|
|
{
|
|
name: "Multiple valid licenses",
|
|
licenses: "MIT, Apache-2.0, GPL-3.0-only",
|
|
wantContain: []string{
|
|
`https://spdx.org/licenses/MIT.html`,
|
|
`https://spdx.org/licenses/Apache-2.0.html`,
|
|
`https://spdx.org/licenses/GPL-3.0-only.html`,
|
|
`>MIT</a>`,
|
|
`>Apache-2.0</a>`,
|
|
`>GPL-3.0-only</a>`,
|
|
},
|
|
},
|
|
{
|
|
name: "Custom license",
|
|
licenses: "CustomProprietary",
|
|
wantContain: []string{
|
|
`<span class="metadata-badge license-badge"`,
|
|
`title="Custom license: CustomProprietary"`,
|
|
`>CustomProprietary</span>`,
|
|
},
|
|
wantNotContain: []string{
|
|
`<a href=`,
|
|
`https://spdx.org`,
|
|
},
|
|
},
|
|
{
|
|
name: "Mixed valid and custom",
|
|
licenses: "MIT, MyCustomLicense",
|
|
wantContain: []string{
|
|
// Valid license (MIT) should be a link
|
|
`<a href="https://spdx.org/licenses/MIT.html"`,
|
|
`>MIT</a>`,
|
|
// Custom license should be a span
|
|
`<span class="metadata-badge license-badge"`,
|
|
`title="Custom license: MyCustomLicense"`,
|
|
`>MyCustomLicense</span>`,
|
|
},
|
|
},
|
|
{
|
|
name: "Apache fuzzy match",
|
|
licenses: "Apache 2.0",
|
|
wantContain: []string{
|
|
`https://spdx.org/licenses/Apache-2.0.html`,
|
|
`>Apache-2.0</a>`,
|
|
},
|
|
},
|
|
{
|
|
name: "Empty licenses",
|
|
licenses: "",
|
|
wantNotContain: []string{
|
|
`<a `,
|
|
`<span`,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
data := struct{ Licenses string }{Licenses: tt.licenses}
|
|
|
|
var buf strings.Builder
|
|
err := tmpl.Execute(&buf, data)
|
|
if err != nil {
|
|
t.Fatalf("Template execution failed: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
|
|
// Check for expected content
|
|
for _, want := range tt.wantContain {
|
|
if !strings.Contains(output, want) {
|
|
t.Errorf("Output missing expected content:\nWant: %s\nGot: %s", want, output)
|
|
}
|
|
}
|
|
|
|
// Check for unexpected content
|
|
for _, notWant := range tt.wantNotContain {
|
|
if strings.Contains(output, notWant) {
|
|
t.Errorf("Output contains unexpected content:\nDon't want: %s\nGot: %s", notWant, output)
|
|
}
|
|
}
|
|
|
|
t.Logf("Template output:\n%s", output)
|
|
})
|
|
}
|
|
}
|