Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

81 lines
1.9 KiB
Go

package licenses
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
func TestScan_Variants(t *testing.T) {
cases := []struct {
name string
src string
want string
}{
{
name: "go-style",
src: "// SPDX-License-Identifier: MIT\npackage main\n",
want: "MIT",
},
{
name: "python-style",
src: "# SPDX-License-Identifier: Apache-2.0\nimport os\n",
want: "Apache-2.0",
},
{
name: "block-comment-leading-star",
src: "/*\n * SPDX-License-Identifier: BSD-3-Clause\n */\n",
want: "BSD-3-Clause",
},
{
name: "with-expression",
src: "// SPDX-License-Identifier: MIT OR Apache-2.0\n",
want: "MIT OR Apache-2.0",
},
{
name: "no-header",
src: "package main\n\nfunc main() {}\n",
want: "",
},
{
name: "below-window",
src: "// line 1\n// 2\n// 3\n// 4\n// 5\n// 6\n// 7\n// 8\n// 9\n// 10\n// SPDX-License-Identifier: MIT\n",
want: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := Scan([]byte(tc.src))
if got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}
func TestBuildGraphArtifacts(t *testing.T) {
nodes, edges := BuildGraphArtifacts("pkg/foo.go", "MIT", "go")
if len(nodes) != 1 || nodes[0].Kind != graph.KindLicense {
t.Fatalf("nodes = %+v", nodes)
}
if nodes[0].ID != "license::MIT" {
t.Errorf("id = %q", nodes[0].ID)
}
if nodes[0].Name != "MIT" {
t.Errorf("name = %q", nodes[0].Name)
}
if len(edges) != 1 || edges[0].Kind != graph.EdgeLicensedAs {
t.Fatalf("edges = %+v", edges)
}
if edges[0].From != "pkg/foo.go" || edges[0].To != "license::MIT" {
t.Errorf("edge endpoints wrong: %s -> %s", edges[0].From, edges[0].To)
}
}
func TestBuildGraphArtifacts_NoSPDXReturnsEmpty(t *testing.T) {
nodes, edges := BuildGraphArtifacts("pkg/foo.go", "", "go")
if len(nodes) != 0 || len(edges) != 0 {
t.Errorf("expected empty, got %d nodes / %d edges", len(nodes), len(edges))
}
}