71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package developmentexecutor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
type fakeRunner struct {
|
|
calls []string
|
|
exits []int
|
|
}
|
|
|
|
func (r *fakeRunner) Run(_ context.Context, _ string, name string, args []string) int {
|
|
call := name
|
|
for _, arg := range args {
|
|
call += " " + arg
|
|
}
|
|
r.calls = append(r.calls, call)
|
|
if len(r.exits) == 0 {
|
|
return 0
|
|
}
|
|
exit := r.exits[0]
|
|
r.exits = r.exits[1:]
|
|
return exit
|
|
}
|
|
|
|
func validPlan(t *testing.T) Plan {
|
|
t.Helper()
|
|
worktree, err := filepath.Abs(".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return Plan{Worktree: worktree, WorktreeID: "sandbox-1", BaseRepoRevision: "revision-1", ProtectedMainAccess: "DENIED", ProductionAccess: "DENIED", SecretAccess: "DENIED", Checks: []Check{{Kind: CheckGoTest, Packages: []string{"./modules/work/..."}}, {Kind: CheckGoVet, Packages: []string{"./modules/work/..."}}, {Kind: CheckGitDiffCheck}}}
|
|
}
|
|
|
|
func TestExecuteRunsOnlyTypedChecksAndReturnsEvidence(t *testing.T) {
|
|
runner := &fakeRunner{}
|
|
result, err := Execute(context.Background(), validPlan(t), runner)
|
|
if err != nil || !result.Passed || len(result.Checks) != 3 || result.EvidenceRef == "" {
|
|
t.Fatalf("result=%+v err=%v", result, err)
|
|
}
|
|
if len(runner.calls) != 3 || runner.calls[0] != "go test ./modules/work/..." || runner.calls[2] != "git diff --check" {
|
|
t.Fatalf("calls=%v", runner.calls)
|
|
}
|
|
}
|
|
|
|
func TestExecuteFailureCannotBecomeVerificationSuccess(t *testing.T) {
|
|
runner := &fakeRunner{exits: []int{1, 0}}
|
|
plan := validPlan(t)
|
|
plan.Checks = []Check{{Kind: CheckGoTest, Packages: []string{"./..."}}, {Kind: CheckGitDiffCheck}}
|
|
result, err := Execute(context.Background(), plan, runner)
|
|
if err != nil || result.Passed || result.Checks[0].Passed || result.EvidenceRef == "" {
|
|
t.Fatalf("failed result was overclaimed: %+v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestValidatePlanRejectsProtectedAndShellLikeInputs(t *testing.T) {
|
|
protected := validPlan(t)
|
|
protected.WorktreeID = "production"
|
|
if !errors.Is(ValidatePlan(protected), ErrProtectedWorktree) {
|
|
t.Fatal("protected worktree accepted")
|
|
}
|
|
injection := validPlan(t)
|
|
injection.Checks = []Check{{Kind: CheckGoTest, Packages: []string{"./...; echo leaked"}}}
|
|
if !errors.Is(ValidatePlan(injection), ErrInvalidPlan) {
|
|
t.Fatal("shell-like package accepted")
|
|
}
|
|
}
|