package ci
import (
"slices"
"testing"
)
// A rerun of one run starts that run's pipeline alone, at the run's
// check-in, and hands back the new run's id.
func TestRerunRunStartsOnlyThePipelineOfTheRun(t *testing.T) {
srv, started := startedEngine(t)
e := New(srv.URL)
st := &fakeStore{mark: map[string]string{}}
repo := deployRepo()
repo.branches = map[string]string{"t1": "trunk"}
linked := &rerunStore{fakeStore: st, of: map[string]string{}}
id, err := RerunRun(e, linked, "alpha", repo, "http://x/r/alpha", "t1", "ci/test.json", "dan", "orig")
if err != nil {
t.Fatalf("FAIL: rerun: %v", err)
}
if linked.of[id] != "orig" {
t.Errorf("FAIL: the rerun was recorded linked to %q, want orig in the same write", linked.of[id])
}
if id == "" {
t.Errorf("FAIL: rerun returned no run id")
}
if got := started(); !slices.Equal(got, []string{"t1 ci/test.json"}) {
t.Errorf("FAIL: rerun started %v, want only ci/test.json at t1", got)
}
if st.starts[id].Starter != "dan" {
t.Errorf("FAIL: starter %q, want dan", st.starts[id].Starter)
}
if _, err := RerunRun(e, st, "alpha", repo, "http://x/r/alpha", "t1", "ci/gone.json", "dan", ""); err == nil {
t.Errorf("FAIL: rerun of a pipeline the check-in no longer holds started something")
}
pid, err := RerunRun(e, st, "alpha", repo, "http://x/r/alpha", Pair("b2", "t1"), "ci/test.json", "dan", "orig")
if err != nil || pid == "" {
t.Errorf("FAIL: rerun of a pair run: id %q err %v", pid, err)
}
if !t.Failed() {
t.Log("ok")
}
}
// rerunStore records which run each rerun was written as repeating.
type rerunStore struct {
*fakeStore
of map[string]string
}
func (s *rerunStore) AddRerunRun(runID, repo, checkin, workflow, pipeline, def, status, rerunOf string) error {
s.of[runID] = rerunOf
return s.fakeStore.AddRun(runID, repo, checkin, workflow, pipeline, def, status)
}