package spindle import ( "context" "slices" "strings" "testing" "tangled.org/core/api/org_tangled" "tangled.org/core/spindle/xrpc" "tangled.org/core/workflow" ) func TestPushReport(t *testing.T) { broken := workflow.Error{Path: "ci.yml", Error: workflow.MissingEngine} skipped := workflow.Warning{Path: "ci.yml", Type: workflow.WorkflowSkipped, Reason: "did not match trigger push"} misconfigured := workflow.Warning{Path: "ci.yml", Type: workflow.InvalidConfiguration, Reason: "when[0] listens for push without branch or tag and won't match any ref"} tests := []struct { name string diagnostics workflow.Diagnostics scheduled bool verbose bool want []string }{ {"quiet and clean", workflow.Diagnostics{}, true, false, nil}, {"quiet with nothing to run", workflow.Diagnostics{}, false, false, nil}, {"verbose and clean", workflow.Diagnostics{}, true, true, []string{"pipeline compiled with no diagnostics"}}, {"verbose with nothing to run", workflow.Diagnostics{}, false, true, []string{"no pipelines to compile"}}, {"errors are never hidden", workflow.Diagnostics{Errors: []workflow.Error{broken}}, false, false, []string{"error: ci.yml: missing engine"}}, {"skipped workflows wait for verbose", workflow.Diagnostics{Warnings: []workflow.Warning{skipped}}, false, false, nil}, {"misconfigurations are never hidden", workflow.Diagnostics{Warnings: []workflow.Warning{skipped, misconfigured}}, false, false, []string{misconfigured.String()}}, { "verbose keeps the order and every warning", workflow.Diagnostics{Errors: []workflow.Error{broken}, Warnings: []workflow.Warning{skipped, misconfigured}}, false, true, []string{broken.String(), skipped.String(), misconfigured.String(), "no pipelines to compile"}, }, {"a warning is not a clean compile", workflow.Diagnostics{Warnings: []workflow.Warning{misconfigured}}, true, true, []string{misconfigured.String()}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := pushReport(tt.diagnostics, tt.scheduled, tt.verbose); !slices.Equal(got, tt.want) { t.Errorf("report = %q, want %q", got, tt.want) } }) } } func (e *pushEnv) pushWith(t *testing.T, files map[string]string, options ...string) (xrpc.NotifyResult, error) { t.Helper() push := faultPush("refs/heads/main", zeroSha, e.repo.commitWorkflows(t, files)) push.EventPush.PushOptions = options return e.n.s.NotifyEvent(context.Background(), e.w.knotRef(), testRepoDid, push) } const ( cleanWorkflow = "engine: dummy\nwhen:\n - event: push\n branch: main\nsteps:\n - name: hi\n command: echo hi\n" otherBranch = "engine: dummy\nwhen:\n - event: push\n branch: release\nsteps:\n - name: hi\n command: echo hi\n" noRefs = "engine: dummy\nwhen:\n - event: push\nsteps:\n - name: hi\n command: echo hi\n" noEngine = "when:\n - event: push\n branch: main\n" notEvenYAML = "engine: : not valid : yaml :\n - [\n" skippedMessage = "warning: ci.yml: workflow skipped: did not match trigger push" ) func TestNotifyEventReportsWhatTheWorkflowsGotWrong(t *testing.T) { tests := []struct { name string files map[string]string options []string wantPipeline bool wantReport []string }{ {"a clean push says nothing", map[string]string{"ci.yml": cleanWorkflow}, nil, true, nil}, {"verbose-ci confirms a clean push", map[string]string{"ci.yml": cleanWorkflow}, []string{"verbose-ci"}, true, []string{"pipeline compiled with no diagnostics"}}, {"ci-verbose is the same option", map[string]string{"ci.yml": cleanWorkflow}, []string{"ci-verbose"}, true, []string{"pipeline compiled with no diagnostics"}}, {"no workflows say nothing", map[string]string{}, nil, false, nil}, {"verbose-ci says there is nothing to compile", map[string]string{}, []string{"verbose-ci"}, false, []string{"no pipelines to compile"}}, {"a skipped workflow stays quiet", map[string]string{"ci.yml": otherBranch}, nil, false, nil}, {"verbose-ci explains the skip", map[string]string{"ci.yml": otherBranch}, []string{"verbose-ci"}, false, []string{skippedMessage, "no pipelines to compile"}}, { "a constraint without refs is reported without verbose-ci", map[string]string{"ci.yml": noRefs}, nil, false, []string{"warning: ci.yml: invalid configuration: when[0] listens for push without branch or tag and won't match any ref"}, }, {"a missing engine is reported", map[string]string{"ci.yml": noEngine}, nil, false, []string{"error: ci.yml: missing engine"}}, {"a broken file is reported next to a working one", map[string]string{"a.yml": cleanWorkflow, "b.yml": notEvenYAML}, nil, true, []string{"error: b.yml: "}}, {"skip-ci beats verbose-ci", map[string]string{"ci.yml": noEngine}, []string{"verbose-ci", "skip-ci"}, false, nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { e := newPushEnv(t) result, err := e.pushWith(t, tt.files, tt.options...) if err != nil { t.Fatalf("NotifyEvent: %v", err) } if scheduled := result.Pipeline != ""; scheduled != tt.wantPipeline { t.Errorf("pipeline = %q, want scheduled %v", result.Pipeline, tt.wantPipeline) } if len(result.Report) != len(tt.wantReport) { t.Fatalf("report = %q, want %q", result.Report, tt.wantReport) } for i, want := range tt.wantReport { if !strings.HasPrefix(result.Report[i], want) { t.Errorf("report[%d] = %q, want it to start with %q", i, result.Report[i], want) } } }) } } func TestNotifyEventReplayRepeatsThePipelineButNotTheReport(t *testing.T) { e := newPushEnv(t) sha := e.repo.commitWorkflows(t, map[string]string{"ci.yml": cleanWorkflow}) push := faultPush("refs/heads/main", zeroSha, sha) push.EventPush.PushOptions = []string{"verbose-ci"} notify := func(event *org_tangled.EventNotifyEvent_Input_Event) xrpc.NotifyResult { t.Helper() result, err := e.n.s.NotifyEvent(context.Background(), e.w.knotRef(), testRepoDid, event) if err != nil { t.Fatalf("NotifyEvent: %v", err) } return result } first := notify(push) if first.Pipeline == "" || len(first.Report) != 1 { t.Fatalf("first answer = %+v, want a pipeline and a report", first) } again := notify(push) if again.Pipeline != first.Pipeline || again.Report != nil { t.Errorf("replay = %+v, want the same pipeline and no report", again) } }