main reporter_plain_test.go
Eric Bower  ·  2026-08-16
 1package main
 2
 3import (
 4	"context"
 5	"fmt"
 6	"io"
 7	"log/slog"
 8	"os/exec"
 9	"testing"
10	"time"
11)
12
13func TestWaitAndReportPlain_Cancellation(t *testing.T) {
14	if _, err := exec.LookPath("zmx"); err != nil {
15		t.Skip("zmx not found, skipping integration test")
16	}
17
18	ctx, cancel := context.WithCancel(context.Background())
19	cfg := &Cfg{
20		Ctx:             ctx,
21		Cancel:          cancel,
22		MonitorInterval: 50 * time.Millisecond,
23		Logger:          slog.New(slog.NewTextHandler(io.Discard, nil)),
24		ArtifactDir:     t.TempDir(),
25	}
26
27	jobID := fmt.Sprintf("plain-cancel-%d", time.Now().UnixNano())
28	prefix := "local.testrepo." + jobID + "."
29	runnerSession := prefix + "runner"
30
31	t.Cleanup(func() {
32		_ = exec.Command("zmx", "kill", "-f", runnerSession).Run()
33	})
34
35	cmd := exec.Command("zmx", "run", runnerSession, "-d", "sleep", "30")
36	if err := cmd.Run(); err != nil {
37		t.Fatalf("failed to start zmx session: %v", err)
38	}
39
40	go func() {
41		time.Sleep(100 * time.Millisecond)
42		cancel()
43	}()
44
45	err := waitAndReportPlain(cfg, nil, "testrepo", jobID, "local")
46	if err == nil {
47		t.Error("expected error when waitAndReportPlain is cancelled, got nil")
48	}
49}