Files
archie/internal/domain/workflow/repo_tooling_test.go
T

139 lines
4.6 KiB
Go

package workflow
import (
"context"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// repo.tooling takes its commands from the repository's policy document, so the
// stage reads tc.Repo rather than any settings. The tests below resolve it
// through the registered vocabulary: a missing step type fails the assertion
// instead of the build.
const repoToolingStepName = "repo.tooling"
func newRepoToolingTestStage(t *testing.T) Stage {
t.Helper()
for _, stepType := range RepoStepTypes() {
if stepType.Name != repoToolingStepName {
continue
}
stage, err := stepType.Factory(yaml.Node{})
if err != nil {
t.Fatalf("%s: factory: %v", repoToolingStepName, err)
}
return stage
}
t.Fatalf("%s is not a registered repository step type", repoToolingStepName)
return Stage{}
}
func repoToolingContext(t *testing.T, dir string, tooling [][]string) *TaskContext {
t.Helper()
return &TaskContext{
Task: &task.Task{ID: 1, Attempt: 1, Source: task.SourceTest},
Repo: config.Repo{Owner: "acme", Name: "widgets", Tooling: tooling},
Dir: dir,
Log: slog.Default(),
}
}
// A repository that declares no tooling is not a repository that failed to
// install any: the stage is a no-op and the workflow continues.
func TestRepoToolingWithoutDeclarationsSucceeds(t *testing.T) {
dir := t.TempDir()
stage := newRepoToolingTestStage(t)
for _, tooling := range [][][]string{nil, {}, {{}}} {
if err := stage.Run(context.Background(), repoToolingContext(t, dir, tooling)); err != nil {
t.Fatalf("tooling %v: err = %v, want none", tooling, err)
}
}
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 0 {
t.Fatalf("worktree holds %v, want nothing written", entries)
}
}
// The commands run in the task worktree, in the declared order: installing a
// toolchain that landed somewhere other than the repository would leave the
// gate unable to find it.
func TestRepoToolingRunsDeclaredCommandsInOrderInTheWorktree(t *testing.T) {
dir := t.TempDir()
stage := newRepoToolingTestStage(t)
tc := repoToolingContext(t, dir, [][]string{
{"sh", "-c", "printf first > order.txt"},
{"sh", "-c", "printf second >> order.txt"},
{"sh", "-c", "pwd > where.txt"},
})
if err := stage.Run(context.Background(), tc); err != nil {
t.Fatalf("err = %v, want none", err)
}
order, err := os.ReadFile(filepath.Join(dir, "order.txt"))
if err != nil {
t.Fatalf("declared command did not run in the worktree: %v", err)
}
if got, want := string(order), "firstsecond"; got != want {
t.Fatalf("commands ran out of order: got %q, want %q", got, want)
}
where, err := os.ReadFile(filepath.Join(dir, "where.txt"))
if err != nil {
t.Fatalf("declared command did not run in the worktree: %v", err)
}
if got := strings.TrimSpace(string(where)); got != dir {
t.Fatalf("command ran in %q, want the worktree %q", got, dir)
}
}
// A failed install is a failure to install, not a red repository: the error
// carries the command and its output and says so, so an operator (and the
// baseline-fix builder) does not read it as failing tests.
func TestRepoToolingFailureNamesTheInstallAndItsOutput(t *testing.T) {
dir := t.TempDir()
stage := newRepoToolingTestStage(t)
tc := repoToolingContext(t, dir, [][]string{
{"sh", "-c", "printf first > ran.txt"},
{"sh", "-c", "echo 'no such toolchain: protoc-gen-nope' >&2; exit 7"},
{"sh", "-c", "printf third > after.txt"},
})
err := stage.Run(context.Background(), tc)
if err == nil {
t.Fatal("err = nil, want the failed install reported")
}
message := err.Error()
for _, want := range []string{"protoc-gen-nope", "no such toolchain", "install"} {
if !strings.Contains(message, want) {
t.Fatalf("err = %q, want it to name %q", message, want)
}
}
if strings.Contains(strings.ToLower(message), "test failure") {
t.Fatalf("err = %q, want it not to read as a failing test", message)
}
if _, err := os.Stat(filepath.Join(dir, "after.txt")); !os.IsNotExist(err) {
t.Fatalf("a command after the failure ran; the step must stop at the first failure (stat err = %v)", err)
}
}
// The step needs a worktree to install into: running it before repo.prepare is
// a definition error, not a silent install in the daemon's own directory.
func TestRepoToolingWithoutWorktreeFails(t *testing.T) {
stage := newRepoToolingTestStage(t)
err := stage.Run(context.Background(), repoToolingContext(t, "", [][]string{{"sh", "-c", "true"}}))
if err == nil {
t.Fatal("err = nil, want a failure naming the missing worktree")
}
}