ajhahn.de
← eeco
Go 70 lines
package workflow

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

	"github.com/ajhahnde/eeco/internal/config"
)

// newCfg returns a config rooted at a fresh temp repo with an
// initialised-looking workspace (the directories the workflows touch).
func newCfg(t *testing.T) *config.Config {
	return newCfgWithProfile(t, config.ProfileGeneric)
}

// newCfgWithProfile is the profile-parameterised form of newCfg, used by
// tests that need to exercise per-profile behaviour (e.g. scaffold
// template selection).
func newCfgWithProfile(t *testing.T, profile config.Profile) *config.Config {
	t.Helper()
	root := t.TempDir()
	ws := filepath.Join(root, ".eeco")
	for _, d := range []string{"workflows", "state", "memory"} {
		if err := os.MkdirAll(filepath.Join(ws, d), 0o755); err != nil {
			t.Fatal(err)
		}
	}
	return &config.Config{
		RepoRoot:      root,
		WorkspaceName: ".eeco",
		Workspace:     ws,
		Profile:       profile,
		StaleDays:     config.DefaultStaleDays,
	}
}

func writeRepoFile(t *testing.T, root, rel, content string) {
	t.Helper()
	full := filepath.Join(root, rel)
	if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(full, []byte(content), 0o644); err != nil {
		t.Fatal(err)
	}
}

// gitInit makes root a real git repo and stages everything currently in
// it. Tests that need tracked files call this after writing them.
func gitInit(t *testing.T, root string) {
	t.Helper()
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not available")
	}
	for _, args := range [][]string{
		{"init", "-q"},
		{"config", "user.email", "[email protected]"},
		{"config", "user.name", "t"},
		{"add", "-A"},
	} {
		cmd := exec.Command("git", args...)
		cmd.Dir = root
		if out, err := cmd.CombinedOutput(); err != nil {
			t.Fatalf("git %v: %v\n%s", args, err, out)
		}
	}
}