Files
at-container-registry/pkg/appview/licenses/integration_test.go
T

65 lines
1.6 KiB
Go

package licenses_test
import (
"html/template"
"strings"
"testing"
"atcr.io/pkg/appview/licenses"
)
// Test template integration with parseLicenses
func TestTemplateIntegration(t *testing.T) {
funcMap := template.FuncMap{
"parseLicenses": func(licensesStr string) []licenses.LicenseInfo {
return licenses.ParseLicenses(licensesStr)
},
}
tmplStr := `{{ range parseLicenses . }}{{ if .IsValid }}[VALID:{{ .SPDXID }}:{{ .URL }}]{{ else }}[INVALID:{{ .Name }}]{{ end }}{{ end }}`
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplStr))
tests := []struct {
name string
input string
wantText string
}{
{
name: "MIT license",
input: "MIT",
wantText: "[VALID:MIT:https://spdx.org/licenses/MIT.html]",
},
{
name: "Multiple licenses",
input: "MIT, Apache-2.0",
wantText: "[VALID:MIT:https://spdx.org/licenses/MIT.html][VALID:Apache-2.0:https://spdx.org/licenses/Apache-2.0.html]",
},
{
name: "Unknown license",
input: "CustomProprietary",
wantText: "[INVALID:CustomProprietary]",
},
{
name: "Mixed valid and invalid",
input: "MIT, CustomLicense, Apache-2.0",
wantText: "[VALID:MIT:https://spdx.org/licenses/MIT.html][INVALID:CustomLicense][VALID:Apache-2.0:https://spdx.org/licenses/Apache-2.0.html]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf strings.Builder
err := tmpl.Execute(&buf, tt.input)
if err != nil {
t.Fatalf("Template execution failed: %v", err)
}
got := buf.String()
if got != tt.wantText {
t.Errorf("Template output mismatch:\nGot: %s\nWant: %s", got, tt.wantText)
}
})
}
}