120 lines
3.4 KiB
Go
120 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseCoverProfile_AppliesExclusions(t *testing.T) {
|
|
t.Parallel()
|
|
tmp := t.TempDir()
|
|
profile := filepath.Join(tmp, "coverage.out")
|
|
content := "mode: set\n" +
|
|
"git.hrafn.xyz/aether/cue/api/graph/generated.go:1.1,2.1 2 1\n" +
|
|
"git.hrafn.xyz/aether/cue/api/graph/resolver.go:1.1,2.1 2 1\n" +
|
|
"git.hrafn.xyz/aether/cue/service/llm/validator.go:1.1,2.1 2 0\n"
|
|
if err := os.WriteFile(profile, []byte(content), 0600); err != nil {
|
|
t.Fatalf("write profile: %v", err)
|
|
}
|
|
|
|
policy := Policy{
|
|
MinimumStatementCoverage: 80,
|
|
CriticalPackages: []PackagePolicy{
|
|
{Package: "git.hrafn.xyz/aether/cue/api/graph", Include: true, Exclusions: []string{"generated.go"}},
|
|
},
|
|
}
|
|
|
|
got, err := ParseCoverProfile(profile, policy)
|
|
if err != nil {
|
|
t.Fatalf("ParseCoverProfile() error = %v", err)
|
|
}
|
|
|
|
api := got["git.hrafn.xyz/aether/cue/api/graph"]
|
|
if api.Total != 2 || api.Covered != 2 {
|
|
t.Fatalf("api coverage mismatch: got %+v", api)
|
|
}
|
|
llm := got["git.hrafn.xyz/aether/cue/service/llm"]
|
|
if llm.Total != 2 || llm.Covered != 0 {
|
|
t.Fatalf("llm coverage mismatch: got %+v", llm)
|
|
}
|
|
}
|
|
|
|
func TestEvaluateCoverage_UsesPolicyThresholds(t *testing.T) {
|
|
t.Parallel()
|
|
pkgs := []string{
|
|
"git.hrafn.xyz/aether/cue/service/llm",
|
|
"git.hrafn.xyz/aether/cue/service/orchestrator",
|
|
}
|
|
byPkg := map[string]Coverage{
|
|
"git.hrafn.xyz/aether/cue/service/llm": {Covered: 8, Total: 10},
|
|
"git.hrafn.xyz/aether/cue/service/orchestrator": {Covered: 3, Total: 10},
|
|
}
|
|
policy := Policy{
|
|
MinimumStatementCoverage: 80,
|
|
CriticalPackages: []PackagePolicy{
|
|
{Package: "git.hrafn.xyz/aether/cue/service/orchestrator", MinimumStatementCoverage: 30, Include: true},
|
|
},
|
|
}
|
|
|
|
results := EvaluateCoverage(pkgs, byPkg, policy)
|
|
if len(results) != 2 {
|
|
t.Fatalf("expected 2 results, got %d", len(results))
|
|
}
|
|
if !results[0].Pass {
|
|
t.Fatalf("expected llm to pass at default threshold: %+v", results[0])
|
|
}
|
|
if !results[1].Pass {
|
|
t.Fatalf("expected orchestrator to pass at overridden threshold: %+v", results[1])
|
|
}
|
|
}
|
|
|
|
func TestEvaluateCoverage_NoStatementsPasses(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pkg := "git.hrafn.xyz/aether/cue/repository/vector"
|
|
results := EvaluateCoverage(
|
|
[]string{pkg},
|
|
map[string]Coverage{pkg: {Covered: 0, Total: 0}},
|
|
Policy{MinimumStatementCoverage: 80},
|
|
)
|
|
|
|
if len(results) != 1 {
|
|
t.Fatalf("expected 1 result, got %d", len(results))
|
|
}
|
|
if !results[0].Pass {
|
|
t.Fatalf("expected pass for no-statement package, got %+v", results[0])
|
|
}
|
|
}
|
|
|
|
func TestLoadPolicy_RejectsNonJSONPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmp := t.TempDir()
|
|
policyPath := filepath.Join(tmp, "policy.yaml")
|
|
if err := os.WriteFile(policyPath, []byte("minimum_statement_coverage: 80\n"), 0600); err != nil {
|
|
t.Fatalf("write policy file: %v", err)
|
|
}
|
|
|
|
_, err := LoadPolicy(policyPath)
|
|
if err == nil {
|
|
t.Fatal("expected LoadPolicy to fail for non-json extension")
|
|
}
|
|
if !strings.Contains(err.Error(), "invalid policy file extension") {
|
|
t.Fatalf("expected extension error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseCoverProfile_RejectsDirectoryPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := ParseCoverProfile(t.TempDir(), Policy{MinimumStatementCoverage: 80})
|
|
if err == nil {
|
|
t.Fatal("expected ParseCoverProfile to fail for directory path")
|
|
}
|
|
if !strings.Contains(err.Error(), "coverage profile path must be a file") {
|
|
t.Fatalf("expected directory path error, got: %v", err)
|
|
}
|
|
}
|