Skip to content
quarry Artifact [d3f6ec1bb8]
Login

Artifact [d3f6ec1bb8]

Artifact d3f6ec1bb8c234396079d19fc2265cdc6e9c34d4ac75439ad96eab852446f393:


package api

import (
	"os/exec"
	"path/filepath"
	"sync"
	"testing"

	"craftdesign.group/quarry/internal/events"
	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/ingest"
	"craftdesign.group/quarry/internal/zitcli"
)

// pairRuns answers the runs recorded against exactly one premerge pair
// each; a pair with no entry was never tested. judge runs each time a
// pair's runs are read, the moment an accept judges its pair.
type pairRuns struct {
	mu    sync.Mutex
	runs  map[string][]CIRun
	judge func()
}

func (p *pairRuns) CIRuns(string, int) ([]CIRun, error)          { return nil, nil }
func (p *pairRuns) CIRunsPage(string, int, int) ([]CIRun, error) { return nil, nil }
func (p *pairRuns) CIRunsFor(string, string) ([]CIRun, error)    { return nil, nil }
func (p *pairRuns) CIRunsForPair(_ string, pair string) ([]CIRun, error) {
	if p.judge != nil {
		p.judge()
	}
	p.mu.Lock()
	defer p.mu.Unlock()
	return p.runs[pair], nil
}

func (p *pairRuns) green(branchTip, targetTip string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	pair := "pair:" + branchTip + ":" + targetTip
	p.runs[pair] = append(p.runs[pair], CIRun{Checkin: pair, Workflow: "ci", Pipeline: "ci", Status: "success"})
}

// premergeStore is a real store with two open landings, one and two,
// each a branch off the root onto trunk touching its own file, folded
// into a real index. The checkout is left on two.
func premergeStore(t *testing.T) (dir string, ids [2]string, ix *index.Index, pipeSync func() (int, error)) {
	t.Helper()
	if _, err := exec.LookPath("zit"); err != nil {
		t.Skip("zit not on PATH")
	}
	dir = t.TempDir()
	zrun(t, dir, "init", ".")
	zwrite(t, dir, "f.txt", "root\n")
	zrun(t, dir, "add", "f.txt")
	zrun(t, dir, "commit", "-m", "root", "--user", "u")
	for i, branch := range []string{"one", "two"} {
		zrun(t, dir, "update", "trunk")
		zwrite(t, dir, branch+".txt", branch+"\n")
		zrun(t, dir, "add", branch+".txt")
		zrun(t, dir, "commit", "-m", branch, "--branch", branch, "--user", "u")
		id, err := zitcli.New(dir).LandingOpen(testToken, "dan", branch, "trunk", "land "+branch, "")
		if err != nil {
			t.Fatal(err)
		}
		ids[i] = id
	}
	ev, err := events.Start(filepath.Join(t.TempDir(), "nats"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(ev.Close)
	ix, err = index.Open(filepath.Join(t.TempDir(), "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { ix.Close() })
	pipe := ingest.NewPipeline(zitcli.New(dir), ev, ix, "test", "n1")
	if _, err := pipe.Sync(); err != nil {
		t.Fatal(err)
	}
	return dir, ids, ix, pipe.Sync
}