feat(runbook): retry/depends directives, remove TUI, add skip reasons

Add #112 step directive framework (retry, delay, depends) via HTML
comment syntax: <!-- runbook: retry=3 delay=5s depends=2 -->

- Parser: applyDirectiveComment() parses retry/delay/depends/timeout
- Executor: retry wraps subshell in for-loop, depends checks
  __rb_status_N, exit code -2 sentinel for depends-skipped
- Fix __rb_rc stale value bug when depends-else + fail-fast interact
- Reporter: show skip reasons (depends/fail-fast) at all verbosity levels

Remove TUI (executor.go, reporter_tui.go) to prepare for independent
extraction. All execution now uses unified session executor path,
eliminating feature parity issues (retry/depends/env were TUI-only gaps).

Tests: +10 new tests covering directive parsing, retry, depends,
fail-fast, env seeding, per-step timeout at both executor and runner
levels.
This commit is contained in:
Willie
2026-03-11 04:53:08 +08:00
parent 65fc6a0458
commit 9c2dcda9e6
11 changed files with 412 additions and 475 deletions
-71
View File
@@ -1,71 +0,0 @@
package main
import (
"bytes"
"context"
"errors"
"os/exec"
"strings"
"syscall"
"time"
)
// Execute runs a bash command from a Step and returns the result.
// Refuses to execute outside a container unless RUNBOOK_ALLOW_EXECUTE is set.
func Execute(ctx context.Context, s Step) StepResult {
if !IsContainerEnv() {
return StepResult{
Step: s,
Status: StatusFailed,
Error: ErrNotInContainer.Error(),
}
}
start := time.Now()
command := s.Command
// Merge consecutive code blocks separated by "---".
command = strings.ReplaceAll(command, "\n---\n", "\n")
cmd := exec.CommandContext(ctx, "bash", "-eo", "pipefail", "-c", command)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
elapsed := time.Since(start)
result := StepResult{
Step: s,
DurationMs: msDuration(elapsed),
Stdout: stdout.String(),
Stderr: stderr.String(),
}
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
result.ExitCode = ws.ExitStatus()
} else {
result.ExitCode = exitErr.ExitCode()
}
result.Status = StatusFailed
} else if ctx.Err() != nil {
// Context timeout or cancellation.
result.Status = StatusFailed
result.ExitCode = -1
result.Error = ctx.Err().Error()
} else {
result.Status = StatusFailed
result.ExitCode = -1
result.Error = err.Error()
}
return result
}
result.Status = StatusPassed
result.ExitCode = 0
return result
}
+61 -21
View File
@@ -172,39 +172,49 @@ func buildSessionScript(steps []indexedStep, tmpDir string, failFast bool, envVa
fmt.Fprintf(&sb, "if [ \"$__rb_stop\" = \"0\" ]; then\n")
}
// depends directive: skip if the depended-on step failed.
if as.step.DependsOn > 0 {
fmt.Fprintf(&sb, "if [ \"${__rb_status_%d:-1}\" = \"0\" ]; then\n", as.step.DependsOn)
}
fmt.Fprintf(&sb, "echo '@@RB:BEGIN:%d@@'\n", n)
fmt.Fprintf(&sb, "__rb_t0=$(__rb_now_ms)\n")
// Subshell: isolates failures while env file + trap persist variables.
// No set -e: the step's exit code is the last command's exit code,
// so authors don't need workarounds like `cmd || EXIT=$?`.
fmt.Fprintf(&sb, "(\n")
fmt.Fprintf(&sb, " set -o pipefail -a\n")
fmt.Fprintf(&sb, " [ -f %q ] && source %q\n", envFile, envFile)
fmt.Fprintf(&sb, " __rb_save_env() { export -p > %q 2>/dev/null; }\n", envFile)
fmt.Fprintf(&sb, " trap __rb_save_env EXIT\n")
// Build the subshell body.
subshell := buildStepSubshell(as.step, command, envFile, errFile)
if as.step.Timeout > 0 {
// Per-step timeout: wrap command in `timeout` with heredoc to avoid quoting issues.
// Exit code 124 = timeout killed the process.
secs := int(as.step.Timeout.Seconds())
if secs < 1 {
secs = 1
// retry directive: wrap subshell in a for loop.
if as.step.Retry > 0 {
attempts := as.step.Retry + 1
fmt.Fprintf(&sb, "for __rb_attempt in $(seq 1 %d); do\n", attempts)
fmt.Fprintf(&sb, "%s", subshell)
fmt.Fprintf(&sb, "__rb_rc=$?\n")
fmt.Fprintf(&sb, "[ $__rb_rc -eq 0 ] && break\n")
if as.step.RetryDelay > 0 {
fmt.Fprintf(&sb, "[ $__rb_attempt -lt %d ] && sleep %d\n", attempts, int(as.step.RetryDelay.Seconds()))
}
fmt.Fprintf(&sb, " timeout %d bash <<'__RB_STEP_%d__'\n", secs, n)
fmt.Fprintf(&sb, "%s\n", command)
fmt.Fprintf(&sb, "__RB_STEP_%d__\n", n)
sb.WriteString("done\n")
} else {
fmt.Fprintf(&sb, " %s\n", command)
fmt.Fprintf(&sb, "%s", subshell)
fmt.Fprintf(&sb, "__rb_rc=$?\n")
}
fmt.Fprintf(&sb, ") 2>%q\n", errFile)
fmt.Fprintf(&sb, "__rb_rc=$?\n")
fmt.Fprintf(&sb, "__rb_t1=$(__rb_now_ms)\n")
fmt.Fprintf(&sb, "__rb_dur=$(( __rb_t1 - __rb_t0 ))\n")
fmt.Fprintf(&sb, "echo \"@@RB:END:%d:${__rb_rc}:${__rb_dur}@@\"\n", n)
// Track step status for depends directives.
fmt.Fprintf(&sb, "__rb_status_%d=$__rb_rc\n", n)
// Close depends block.
if as.step.DependsOn > 0 {
fmt.Fprintf(&sb, "else\n")
fmt.Fprintf(&sb, "echo '@@RB:END:%d:-2:0@@'\n", n)
fmt.Fprintf(&sb, "__rb_status_%d=-2\n", n)
fmt.Fprintf(&sb, "__rb_rc=0\n") // prevent stale __rb_rc from triggering fail-fast
fmt.Fprintf(&sb, "fi\n")
}
if failFast {
fmt.Fprintf(&sb, "[ $__rb_rc -ne 0 ] && __rb_stop=1\n")
fmt.Fprintf(&sb, "else\n")
@@ -219,6 +229,32 @@ func buildSessionScript(steps []indexedStep, tmpDir string, failFast bool, envVa
return sb.String()
}
// buildStepSubshell generates the subshell block for a single step.
// Returns the subshell string WITHOUT the trailing `__rb_rc=$?`.
func buildStepSubshell(step Step, command, envFile, errFile string) string {
var sb strings.Builder
fmt.Fprintf(&sb, "(\n")
fmt.Fprintf(&sb, " set -o pipefail -a\n")
fmt.Fprintf(&sb, " [ -f %q ] && source %q\n", envFile, envFile)
fmt.Fprintf(&sb, " __rb_save_env() { export -p > %q 2>/dev/null; }\n", envFile)
fmt.Fprintf(&sb, " trap __rb_save_env EXIT\n")
if step.Timeout > 0 {
secs := int(step.Timeout.Seconds())
if secs < 1 {
secs = 1
}
fmt.Fprintf(&sb, " timeout %d bash <<'__RB_STEP_%d__'\n", secs, step.Number)
fmt.Fprintf(&sb, "%s\n", command)
fmt.Fprintf(&sb, "__RB_STEP_%d__\n", step.Number)
} else {
fmt.Fprintf(&sb, " %s\n", command)
}
fmt.Fprintf(&sb, ") 2>%q\n", errFile)
return sb.String()
}
// parseSessionResults reads the combined stdout and splits it into per-step
// results using the @@RB:BEGIN/END markers.
func parseSessionResults(stdout *bytes.Buffer, autoSteps []indexedStep, results []StepResult, tmpDir string) {
@@ -255,9 +291,13 @@ func parseSessionResults(stdout *bytes.Buffer, autoSteps []indexedStep, results
r.Step = as.step
// Exit code -1 is a sentinel for fail-fast skipped steps.
// Exit code -2 is a sentinel for depends-skipped steps.
if exitCode == -1 {
r.Status = StatusSkipped
r.Error = "skipped: earlier step failed (--fail-fast)"
} else if exitCode == -2 {
r.Status = StatusSkipped
r.Error = fmt.Sprintf("skipped: depends on step %d (failed)", as.step.DependsOn)
} else {
r.ExitCode = exitCode
r.DurationMs = durationMs
+114
View File
@@ -158,3 +158,117 @@ func TestExecuteSession_MergedCodeBlocks(t *testing.T) {
t.Fatalf("expected both 'first' and 'second', got %q", results[0].Stdout)
}
}
func TestExecuteSession_FailFast(t *testing.T) {
steps := []Step{
{Number: 1, Title: "fail", Command: "echo step1 && exit 1", Executor: ExecutorAuto},
{Number: 2, Title: "skip", Command: "echo step2", Executor: ExecutorAuto},
{Number: 3, Title: "skip", Command: "echo step3", Executor: ExecutorAuto},
}
results := ExecuteSession(context.Background(), steps, 30*time.Second, true, nil)
if results[0].Status != StatusFailed {
t.Errorf("step 1: expected failed, got %s", results[0].Status)
}
if results[1].Status != StatusSkipped {
t.Errorf("step 2: expected skipped, got %s", results[1].Status)
}
if results[2].Status != StatusSkipped {
t.Errorf("step 3: expected skipped, got %s", results[2].Status)
}
}
func TestExecuteSession_EnvSeeding(t *testing.T) {
steps := []Step{
{Number: 1, Title: "check env", Command: "echo MY_VAR=$MY_VAR", Executor: ExecutorAuto},
}
env := map[string]string{"MY_VAR": "seeded"}
results := ExecuteSession(context.Background(), steps, 30*time.Second, false, env)
if results[0].Status != StatusPassed {
t.Fatalf("expected passed, got %s (err=%s)", results[0].Status, results[0].Error)
}
if !strings.Contains(results[0].Stdout, "MY_VAR=seeded") {
t.Errorf("expected MY_VAR=seeded, got %q", results[0].Stdout)
}
}
func TestExecuteSession_Retry(t *testing.T) {
// Uses a temp file as counter: first attempt creates file + fails,
// second attempt sees file + passes.
steps := []Step{
{
Number: 1,
Title: "retry step",
Command: "F=/tmp/rb_test_retry_$$\nif [ -f \"$F\" ]; then\n echo passed\n rm -f \"$F\"\nelse\n touch \"$F\"\n echo failed && exit 1\nfi",
Retry: 1,
Executor: ExecutorAuto,
},
}
results := ExecuteSession(context.Background(), steps, 30*time.Second, false, nil)
if results[0].Status != StatusPassed {
t.Errorf("expected passed after retry, got %s (stdout=%q stderr=%q)",
results[0].Status, results[0].Stdout, results[0].Stderr)
}
}
func TestExecuteSession_DependsOn(t *testing.T) {
steps := []Step{
{Number: 1, Title: "fail", Command: "exit 1", Executor: ExecutorAuto},
{Number: 2, Title: "depends", Command: "echo should_not_run", DependsOn: 1, Executor: ExecutorAuto},
{Number: 3, Title: "independent", Command: "echo ok", Executor: ExecutorAuto},
}
results := ExecuteSession(context.Background(), steps, 30*time.Second, false, nil)
if results[0].Status != StatusFailed {
t.Errorf("step 1: expected failed, got %s", results[0].Status)
}
if results[1].Status != StatusSkipped {
t.Errorf("step 2: expected skipped (depends), got %s", results[1].Status)
}
if !strings.Contains(results[1].Error, "depends") {
t.Errorf("step 2 error should mention depends, got: %q", results[1].Error)
}
if results[2].Status != StatusPassed {
t.Errorf("step 3: expected passed, got %s (err=%s)", results[2].Status, results[2].Error)
}
}
func TestExecuteSession_DependsOnPasses(t *testing.T) {
steps := []Step{
{Number: 1, Title: "pass", Command: "echo ok", Executor: ExecutorAuto},
{Number: 2, Title: "depends", Command: "echo depends_ok", DependsOn: 1, Executor: ExecutorAuto},
}
results := ExecuteSession(context.Background(), steps, 30*time.Second, false, nil)
if results[0].Status != StatusPassed {
t.Errorf("step 1: expected passed, got %s", results[0].Status)
}
if results[1].Status != StatusPassed {
t.Errorf("step 2: expected passed (depends satisfied), got %s (err=%s)", results[1].Status, results[1].Error)
}
if !strings.Contains(results[1].Stdout, "depends_ok") {
t.Errorf("step 2: expected 'depends_ok', got %q", results[1].Stdout)
}
}
func TestExecuteSession_PerStepTimeout(t *testing.T) {
steps := []Step{
{
Number: 1,
Title: "slow step",
Command: "sleep 10 && echo done",
Timeout: 1 * time.Second,
Executor: ExecutorAuto,
},
}
results := ExecuteSession(context.Background(), steps, 30*time.Second, false, nil)
if results[0].Status != StatusFailed {
t.Errorf("expected failed (timeout), got %s", results[0].Status)
}
if results[0].ExitCode == 0 {
t.Errorf("expected non-zero exit code from timeout")
}
}
-109
View File
@@ -1,109 +0,0 @@
package main
import (
"context"
"strings"
"testing"
"time"
)
func TestExecute_SimpleEcho(t *testing.T) {
r := Execute(context.Background(), Step{
Number: 1,
Title: "echo test",
Command: "echo hello",
})
if r.Status != "passed" {
t.Fatalf("expected passed, got %s", r.Status)
}
if r.ExitCode != 0 {
t.Fatalf("expected exit 0, got %d", r.ExitCode)
}
if got := strings.TrimSpace(r.Stdout); got != "hello" {
t.Fatalf("expected stdout 'hello', got %q", got)
}
}
func TestExecute_ExitOne(t *testing.T) {
r := Execute(context.Background(), Step{
Number: 2,
Title: "exit 1",
Command: "exit 1",
})
if r.Status != "failed" {
t.Fatalf("expected failed, got %s", r.Status)
}
if r.ExitCode != 1 {
t.Fatalf("expected exit 1, got %d", r.ExitCode)
}
}
func TestExecute_Multiline(t *testing.T) {
r := Execute(context.Background(), Step{
Number: 3,
Title: "multiline",
Command: "echo line1\necho line2",
})
if r.Status != "passed" {
t.Fatalf("expected passed, got %s", r.Status)
}
lines := strings.TrimSpace(r.Stdout)
if lines != "line1\nline2" {
t.Fatalf("expected 'line1\\nline2', got %q", lines)
}
}
func TestExecute_ContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
// Small sleep to ensure the timeout has a chance to fire.
time.Sleep(5 * time.Millisecond)
r := Execute(ctx, Step{
Number: 4,
Title: "timeout",
Command: "sleep 10",
})
if r.Status != "failed" {
t.Fatalf("expected failed, got %s", r.Status)
}
if r.Error == "" {
t.Fatal("expected error message for timeout")
}
}
func TestExecute_CapturesStderr(t *testing.T) {
r := Execute(context.Background(), Step{
Number: 5,
Title: "stderr",
Command: "echo err >&2",
})
if r.Status != "passed" {
t.Fatalf("expected passed, got %s", r.Status)
}
if got := strings.TrimSpace(r.Stderr); got != "err" {
t.Fatalf("expected stderr 'err', got %q", got)
}
}
func TestExecute_MergedCodeBlocks(t *testing.T) {
r := Execute(context.Background(), Step{
Number: 6,
Title: "merged blocks",
Command: "echo first\n---\necho second",
})
if r.Status != "passed" {
t.Fatalf("expected passed, got %s", r.Status)
}
lines := strings.TrimSpace(r.Stdout)
if lines != "first\nsecond" {
t.Fatalf("expected 'first\\nsecond', got %q", lines)
}
}
+18 -102
View File
@@ -1,7 +1,6 @@
package main
import (
"context"
"flag"
"fmt"
"os"
@@ -9,28 +8,24 @@ import (
"strconv"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
var (
reportFmt string
dryRun bool
timeout time.Duration
noTUI bool
cliBuild string
cliSetup string
cliTeardown string
failFast bool
outputFile string
verbose countFlag
reportFmt string
dryRun bool
timeout time.Duration
cliBuild string
cliSetup string
cliTeardown string
failFast bool
outputFile string
verbose countFlag
)
flag.StringVar(&reportFmt, "report", "", "output format: json")
flag.BoolVar(&dryRun, "dry-run", false, "parse and classify only, don't execute")
flag.DurationVar(&timeout, "timeout", 0, "per-step timeout (default: 2m, or from runbook.json)")
flag.BoolVar(&noTUI, "no-tui", false, "disable TUI, use plain text output")
flag.StringVar(&cliBuild, "build", "", "command to run once before all runbooks")
flag.StringVar(&cliSetup, "setup", "", "command to run before each runbook")
flag.StringVar(&cliTeardown, "teardown", "", "command to run after each runbook")
@@ -106,9 +101,8 @@ func main() {
}
// Run build hook once before all runbooks.
var buildResult *HookResult
if cfg.Build != "" && !dryRun {
buildResult = runBuildHook(cfg.Build)
buildResult := runBuildHook(cfg.Build)
if !buildResult.OK {
fmt.Fprintf(os.Stderr, "build failed (exit %d), aborting\n", buildResult.ExitCode)
if buildResult.Output != "" {
@@ -118,23 +112,17 @@ func main() {
}
}
// Hooks require session executor for env persistence, so disable TUI when active.
hasHooks := cfg.Setup != "" || cfg.Teardown != ""
useTUI := !noTUI && !dryRun && reportFmt == "" && !hasHooks
exitCode := 0
var reports []Report
for _, file := range files {
name := filepath.Base(file)
var report Report
var runErr error
stepFilter := RunOptions{Steps: stepNums, From: fromFlag, FailFast: failFast}
if useTUI && len(files) == 1 {
report, runErr = runWithTUI(file, name, effectiveTimeout, stepFilter)
} else {
report, runErr = runPlain(file, name, dryRun, effectiveTimeout, cfg, stepFilter)
}
report, runErr := runPlain(file, name, dryRun, effectiveTimeout, cfg, RunOptions{
Steps: stepNums,
From: fromFlag,
FailFast: failFast,
})
if runErr != nil {
fmt.Fprintf(os.Stderr, "error running %s: %v\n", file, runErr)
@@ -153,12 +141,12 @@ func main() {
}
}
// Print summary for non-JSON, non-TUI modes.
// Print summary.
if reportFmt != "json" {
verbosity := int(verbose)
if len(reports) > 1 {
WritePlainSummary(os.Stdout, reports, verbosity)
} else if len(reports) == 1 && !useTUI {
} else if len(reports) == 1 {
WriteSingleReport(os.Stdout, reports[0], verbosity)
}
}
@@ -181,7 +169,7 @@ func main() {
os.Exit(exitCode)
}
// runPlain runs a runbook without TUI.
// runPlain runs a runbook with plain text output.
func runPlain(path, name string, dryRun bool, timeout time.Duration, cfg RunbookConfig, filter RunOptions) (Report, error) {
f, err := os.Open(path)
if err != nil {
@@ -201,78 +189,6 @@ func runPlain(path, name string, dryRun bool, timeout time.Duration, cfg Runbook
})
}
// runWithTUI runs a runbook with bubbletea TUI.
func runWithTUI(path, name string, timeout time.Duration, filter RunOptions) (Report, error) {
f, err := os.Open(path)
if err != nil {
return Report{}, err
}
defer f.Close()
rb, err := ParseRunbook(f)
if err != nil {
return Report{}, err
}
steps := ClassifyAll(rb.Steps)
// Apply step filter: mark filtered-out auto steps as manual so they get skipped.
for i, s := range steps {
if s.Executor == ExecutorAuto && !filter.shouldRun(s.Number) {
steps[i].Executor = ExecutorManual
}
}
// Separate auto and manual steps.
var autoSteps []Step
var skippedResults []StepResult
for _, s := range steps {
if s.Executor == ExecutorAuto {
autoSteps = append(autoSteps, s)
} else {
skippedResults = append(skippedResults, StepResult{
Step: s,
Status: StatusSkipped,
Error: "manual step",
})
}
}
if timeout == 0 {
timeout = 2 * time.Minute
}
start := time.Now()
execFn := func(s Step) StepResult {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
result := Execute(ctx, s)
checkAssertions(&result, s)
return result
}
model := newTUIModel(name, autoSteps, execFn)
p := tea.NewProgram(model)
finalModel, err := p.Run()
if err != nil {
return Report{}, err
}
m := finalModel.(tuiModel)
allResults := append(m.results, skippedResults...)
report := Report{
Version: "1",
Runbook: name,
DurationMs: msDuration(time.Since(start)),
Steps: allResults,
Summary: computeSummary(allResults),
}
return report, nil
}
// resolveFiles finds runbook files from a path (file or directory).
func resolveFiles(target string) ([]string, error) {
info, err := os.Stat(target)
+43
View File
@@ -39,6 +39,9 @@ var stepHeadingRe = regexp.MustCompile(
// directiveTimeoutRe matches (timeout: Xm) or (timeout: Xs) in step titles.
var directiveTimeoutRe = regexp.MustCompile(`\(timeout:\s*([^)]+)\)`)
// directiveCommentRe matches HTML comment directives: <!-- runbook: key=value ... -->
var directiveCommentRe = regexp.MustCompile(`^<!--\s*runbook:\s*(.+?)\s*-->$`)
// codeFenceOpenRe matches opening code fences like ```bash, ```sh, etc.
var codeFenceOpenRe = regexp.MustCompile("^```(\\w*)\\s*$")
@@ -249,6 +252,11 @@ func ParseRunbook(r io.Reader) (*Runbook, error) {
// --- State: in step ---
if state == stateInStep || state == stateInExpected {
// Check for directive comment: <!-- runbook: key=value ... -->
if currentStep != nil && applyDirectiveComment(currentStep, strings.TrimSpace(line)) {
continue
}
// Check for code fence opening
if cm := codeFenceOpenRe.FindStringSubmatch(line); cm != nil {
if state == stateInExpected {
@@ -336,6 +344,41 @@ func parseStepDirectives(title string) (string, time.Duration) {
return title, timeout
}
// applyDirectiveComment parses <!-- runbook: key=value ... --> and applies to step.
// Supported keys: timeout, retry, delay, depends.
func applyDirectiveComment(step *Step, line string) bool {
m := directiveCommentRe.FindStringSubmatch(line)
if m == nil {
return false
}
for _, part := range strings.Fields(m[1]) {
kv := strings.SplitN(part, "=", 2)
if len(kv) != 2 {
continue
}
key, val := kv[0], kv[1]
switch key {
case "timeout":
if d, err := time.ParseDuration(val); err == nil {
step.Timeout = d
}
case "retry":
if n, err := strconv.Atoi(val); err == nil && n > 0 {
step.Retry = n
}
case "delay":
if d, err := time.ParseDuration(val); err == nil {
step.RetryDelay = d
}
case "depends":
if n, err := strconv.Atoi(val); err == nil && n > 0 {
step.DependsOn = n
}
}
}
return true
}
// stripInlineMarkdown removes backticks and bold markers from text.
func stripInlineMarkdown(s string) string {
s = strings.ReplaceAll(s, "`", "")
+42
View File
@@ -709,5 +709,47 @@ func TestParseRunbook_TimeoutDirective(t *testing.T) {
}
}
func TestParseRunbook_DirectiveComment(t *testing.T) {
input := "# Directive Comment Runbook\n\n" +
"### Step 1: Quick check\n\n" +
"```bash\necho ok\n```\n\n" +
"### Step 2: Flaky service call\n\n" +
"<!-- runbook: retry=3 delay=5s -->\n\n" +
"```bash\ncurl http://example.com\n```\n\n" +
"### Step 3: Depends on step 1\n\n" +
"<!-- runbook: depends=1 timeout=2m -->\n\n" +
"```bash\necho depends\n```\n"
rb, err := ParseRunbook(strings.NewReader(input))
if err != nil {
t.Fatalf("ParseRunbook error: %v", err)
}
if len(rb.Steps) != 3 {
t.Fatalf("got %d steps, want 3", len(rb.Steps))
}
// Step 1: no directives.
if rb.Steps[0].Retry != 0 {
t.Errorf("step 1: expected no retry, got %d", rb.Steps[0].Retry)
}
// Step 2: retry=3, delay=5s.
if rb.Steps[1].Retry != 3 {
t.Errorf("step 2: expected retry=3, got %d", rb.Steps[1].Retry)
}
if rb.Steps[1].RetryDelay != 5*time.Second {
t.Errorf("step 2: expected delay=5s, got %v", rb.Steps[1].RetryDelay)
}
// Step 3: depends=1, timeout=2m.
if rb.Steps[2].DependsOn != 1 {
t.Errorf("step 3: expected depends=1, got %d", rb.Steps[2].DependsOn)
}
if rb.Steps[2].Timeout != 2*time.Minute {
t.Errorf("step 3: expected timeout=2m, got %v", rb.Steps[2].Timeout)
}
}
// Runbook is the parsed output from ParseRunbook — defined here for test clarity.
// The actual struct lives in parser.go.
+13 -4
View File
@@ -34,10 +34,14 @@ func WriteSingleReport(w io.Writer, r Report, verbosity int) {
}
fmt.Fprintln(w)
if s.Status == StatusFailed && verbosity == 0 {
reason := stepFailReason(s)
if reason != "" {
fmt.Fprintf(w, " └─ %s\n", reason)
if verbosity == 0 {
if s.Status == StatusFailed {
reason := stepFailReason(s)
if reason != "" {
fmt.Fprintf(w, " └─ %s\n", reason)
}
} else if s.Status == StatusSkipped && s.Error != "" && s.Error != "manual step" {
fmt.Fprintf(w, " └─ %s\n", s.Error)
}
}
@@ -113,6 +117,11 @@ func WritePlainSummary(w io.Writer, reports []Report, verbosity int) {
// writeAssertionDetails prints assertion results for a step (-v).
func writeAssertionDetails(w io.Writer, s StepResult) {
// Show skip reason.
if s.Status == StatusSkipped && s.Error != "" && s.Error != "manual step" {
fmt.Fprintf(w, " ○ %s\n", s.Error)
return
}
if len(s.Assertions) == 0 && s.Status == StatusFailed {
reason := stepFailReason(s)
if reason != "" {
-168
View File
@@ -1,168 +0,0 @@
package main
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// TUI color palette — matches skillshare CLI conventions.
var (
tuiCyan = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))
tuiGreen = lipgloss.NewStyle().Foreground(lipgloss.Color("2"))
tuiRed = lipgloss.NewStyle().Foreground(lipgloss.Color("1"))
tuiYellow = lipgloss.NewStyle().Foreground(lipgloss.Color("3"))
tuiDim = lipgloss.NewStyle().Faint(true)
tuiBold = lipgloss.NewStyle().Bold(true)
)
// stepDoneMsg is sent when a step finishes execution.
type stepDoneMsg struct {
index int
result StepResult
}
// tuiModel is the bubbletea model for real-time runbook progress.
type tuiModel struct {
name string
steps []Step
results []StepResult
execFn func(Step) StepResult
start time.Time
}
// newTUIModel creates a TUI model with injected execution function.
func newTUIModel(name string, steps []Step, execFn func(Step) StepResult) tuiModel {
results := make([]StepResult, len(steps))
for i, s := range steps {
results[i] = StepResult{Step: s, Status: "pending"}
}
return tuiModel{
name: name,
steps: steps,
results: results,
execFn: execFn,
start: time.Now(),
}
}
func (m tuiModel) Init() tea.Cmd {
if len(m.steps) == 0 {
return tea.Quit
}
return m.runStep(0)
}
func (m tuiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
}
case stepDoneMsg:
m.results[msg.index] = msg.result
next := msg.index + 1
if next < len(m.steps) {
return m, m.runStep(next)
}
return m, tea.Quit
}
return m, nil
}
func (m tuiModel) View() string {
var b strings.Builder
// Header
b.WriteString(tuiBold.Render("Runbook: "+m.name) + "\n")
b.WriteString(tuiDim.Render(strings.Repeat("─", 50)) + "\n")
// Step list
for _, r := range m.results {
icon := statusIcon(r.Status)
title := truncateText(r.Step.Title, 38)
line := fmt.Sprintf(" %s %2d. %s", icon, r.Step.Number, title)
if r.Status == StatusPassed || r.Status == StatusFailed {
line += tuiDim.Render(fmt.Sprintf(" %s", formatDurationMs(r.DurationMs)))
}
b.WriteString(line + "\n")
// Show failure reason
if r.Status == StatusFailed {
reason := stepFailReason(r)
if reason != "" {
b.WriteString(tuiRed.Render(" "+reason) + "\n")
}
}
}
// Footer
b.WriteString(tuiDim.Render(strings.Repeat("─", 50)) + "\n")
b.WriteString(m.summaryLine() + "\n")
return b.String()
}
// runStep returns a tea.Cmd that executes step at index i.
func (m tuiModel) runStep(i int) tea.Cmd {
step := m.steps[i]
execFn := m.execFn
return func() tea.Msg {
result := execFn(step)
return stepDoneMsg{index: i, result: result}
}
}
// statusIcon returns the colored icon for a step status.
func statusIcon(status string) string {
switch status {
case StatusPassed:
return tuiGreen.Render("✓")
case StatusFailed:
return tuiRed.Render("✗")
case StatusRunning:
return tuiCyan.Render("●")
default: // pending
return tuiDim.Render("○")
}
}
// summaryLine returns the footer summary string.
func (m tuiModel) summaryLine() string {
var passed, failed, running int
for _, r := range m.results {
switch r.Status {
case StatusPassed:
passed++
case StatusFailed:
failed++
case StatusRunning:
running++
}
}
elapsed := time.Since(m.start)
parts := []string{
fmt.Sprintf("%d/%d passed", passed, len(m.steps)),
}
if failed > 0 {
parts = append(parts, tuiRed.Render(fmt.Sprintf("%d failed", failed)))
} else {
parts = append(parts, fmt.Sprintf("%d failed", failed))
}
if running > 0 {
parts = append(parts, tuiCyan.Render(fmt.Sprintf("%d running", running)))
}
parts = append(parts, tuiDim.Render(fmt.Sprintf("%.1fs", elapsed.Seconds())))
return strings.Join(parts, " ")
}
+118
View File
@@ -360,6 +360,124 @@ echo "MY_VAR=$MY_VAR"
}
}
func TestRunRunbook_DependsOn(t *testing.T) {
md := makeRunbook(`### Step 1: Fail
` + "```bash" + `
echo "step1" && exit 1
` + "```" + `
### Step 2: Depends on step 1
<!-- runbook: depends=1 -->
` + "```bash" + `
echo "step2"
` + "```" + `
### Step 3: Independent
` + "```bash" + `
echo "step3"
` + "```" + `
`)
report, err := RunRunbook(strings.NewReader(md), "test", RunOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if report.Summary.Total != 3 {
t.Fatalf("expected 3 steps, got %d", report.Summary.Total)
}
// Step 1: fails.
if report.Steps[0].Status != StatusFailed {
t.Errorf("step 1: expected failed, got %s", report.Steps[0].Status)
}
// Step 2: skipped because depends on step 1.
if report.Steps[1].Status != StatusSkipped {
t.Errorf("step 2: expected skipped (depends), got %s", report.Steps[1].Status)
}
if !strings.Contains(report.Steps[1].Error, "depends") {
t.Errorf("step 2 error should mention depends, got: %q", report.Steps[1].Error)
}
// Step 3: independent, should pass.
if report.Steps[2].Status != StatusPassed {
t.Errorf("step 3: expected passed (independent), got %s", report.Steps[2].Status)
}
}
func TestRunRunbook_Retry(t *testing.T) {
// Step uses a counter file to fail on first attempt and pass on second.
md := makeRunbook(`### Step 1: Retry step
<!-- runbook: retry=2 -->
` + "```bash" + `
F="/tmp/runbook_retry_test_$$"
if [ -f "$F" ]; then
echo "passed"
rm -f "$F"
else
touch "$F"
echo "failed" && exit 1
fi
` + "```" + `
**Expected:**
- passed
`)
report, err := RunRunbook(strings.NewReader(md), "test", RunOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if report.Steps[0].Status != StatusPassed {
t.Errorf("expected passed after retry, got %s (stdout=%q, stderr=%q)",
report.Steps[0].Status, report.Steps[0].Stdout, report.Steps[0].Stderr)
}
}
func TestRunRunbook_DependsOnWithFailFast(t *testing.T) {
// Ensure depends-skip doesn't cascade via fail-fast.
md := makeRunbook(`### Step 1: Fail
` + "```bash" + `
echo "step1" && exit 1
` + "```" + `
### Step 2: Depends on step 1
<!-- runbook: depends=1 -->
` + "```bash" + `
echo "step2"
` + "```" + `
### Step 3: Independent
` + "```bash" + `
echo "step3"
` + "```" + `
`)
report, err := RunRunbook(strings.NewReader(md), "test", RunOptions{FailFast: true})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Step 1 fails → fail-fast triggers.
if report.Steps[0].Status != StatusFailed {
t.Errorf("step 1: expected failed, got %s", report.Steps[0].Status)
}
// Step 2: skipped (fail-fast, since step 1 already failed).
if report.Steps[1].Status != StatusSkipped {
t.Errorf("step 2: expected skipped, got %s", report.Steps[1].Status)
}
// Step 3: also skipped via fail-fast.
if report.Steps[2].Status != StatusSkipped {
t.Errorf("step 3: expected skipped, got %s", report.Steps[2].Status)
}
}
func TestRunRunbook_JSONOutput(t *testing.T) {
md := makeRunbook(`### Step 1: Echo
+3
View File
@@ -12,6 +12,9 @@ type Step struct {
Expected []string `json:"expected,omitempty"`
Executor string `json:"executor,omitempty"` // "auto", "ai-delegate", "manual"
Timeout time.Duration `json:"timeout,omitempty"` // per-step timeout override (0 = use global)
Retry int `json:"retry,omitempty"` // retry count on failure (0 = no retry)
RetryDelay time.Duration `json:"retry_delay,omitempty"`
DependsOn int `json:"depends_on,omitempty"` // skip if this step number failed (0 = none)
}
// StepResult represents the execution result of a single step.