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 }}` +
`{{ .SPDXID }}` +
`{{ else }}` +
`{{ .Name }}` +
`{{ 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{
`MIT`,
},
},
{
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`,
`>Apache-2.0`,
`>GPL-3.0-only`,
},
},
{
name: "Custom license",
licenses: "CustomProprietary",
wantContain: []string{
`CustomProprietary`,
},
wantNotContain: []string{
`MIT`,
// Custom license should be a span
`MyCustomLicense`,
},
},
{
name: "Apache fuzzy match",
licenses: "Apache 2.0",
wantContain: []string{
`https://spdx.org/licenses/Apache-2.0.html`,
`>Apache-2.0`,
},
},
{
name: "Empty licenses",
licenses: "",
wantNotContain: []string{
`