Skip to content

Commit 1216c15

Browse files
authored
test: avoid Fatalf in concurrent scenario helper (#52)
1 parent de12b60 commit 1216c15

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

internal/scenarios/scenario_test.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func runPolicyVerdictScenario(t *testing.T, repoRoot string, binaryPath string,
119119
if err := os.WriteFile(intentPath, []byte(intents[i]), 0o600); err != nil {
120120
t.Fatalf("write intent fixture %s: %v", intentPath, err)
121121
}
122-
output, code := runCommand(t, workDir, binaryPath,
122+
output, code := mustRunCommand(t, workDir, binaryPath,
123123
"gate", "eval",
124124
"--policy", policyPath,
125125
"--intent", intentPath,
@@ -148,7 +148,7 @@ func runDryRunScenario(t *testing.T, repoRoot string, binaryPath string, scenari
148148
if flags.Simulate {
149149
args = append(args, "--simulate")
150150
}
151-
output, code := runCommand(t, t.TempDir(), binaryPath, args...)
151+
output, code := mustRunCommand(t, t.TempDir(), binaryPath, args...)
152152
if code != expected.ExitCode {
153153
t.Fatalf("unexpected exit code: got=%d want=%d output=%s", code, expected.ExitCode, output)
154154
}
@@ -192,12 +192,16 @@ func runConcurrentScenario(t *testing.T, repoRoot string, binaryPath string, sce
192192
errCh <- fmt.Errorf("mkdir workdir: %w", err)
193193
return
194194
}
195-
output, code := runCommand(t, workDir, binaryPath,
195+
output, code, err := runCommand(workDir, binaryPath,
196196
"gate", "eval",
197197
"--policy", policyPath,
198198
"--intent", intentPath,
199199
"--json",
200200
)
201+
if err != nil {
202+
errCh <- fmt.Errorf("run gate eval run=%d: %w", i+1, err)
203+
return
204+
}
201205
if code != expected.ExitCode {
202206
errCh <- fmt.Errorf("unexpected exit code run=%d got=%d want=%d output=%s", i+1, code, expected.ExitCode, output)
203207
return
@@ -234,12 +238,12 @@ func runPackScenario(t *testing.T, repoRoot string, binaryPath string, scenarioP
234238
expected := readExpectedYAML(t, filepath.Join(scenarioPath, "expected.yaml"))
235239
workDir := t.TempDir()
236240

237-
_, demoCode := runCommand(t, workDir, binaryPath, "demo", "--json")
241+
_, demoCode := mustRunCommand(t, workDir, binaryPath, "demo", "--json")
238242
if demoCode != 0 {
239243
t.Fatalf("demo failed with exit code %d", demoCode)
240244
}
241245
packPath := filepath.Join(workDir, "scenario-pack.zip")
242-
buildOutput, buildCode := runCommand(t, workDir, binaryPath,
246+
buildOutput, buildCode := mustRunCommand(t, workDir, binaryPath,
243247
"pack", "build",
244248
"--type", "run",
245249
"--from", "run_demo",
@@ -249,7 +253,7 @@ func runPackScenario(t *testing.T, repoRoot string, binaryPath string, scenarioP
249253
if buildCode != 0 {
250254
t.Fatalf("pack build failed: code=%d output=%s", buildCode, buildOutput)
251255
}
252-
verifyOutput, verifyCode := runCommand(t, workDir, binaryPath,
256+
verifyOutput, verifyCode := mustRunCommand(t, workDir, binaryPath,
253257
"pack", "verify",
254258
packPath,
255259
"--json",
@@ -282,7 +286,7 @@ func runDelegationScenario(t *testing.T, repoRoot string, binaryPath string, sce
282286
for _, rel := range flags.DelegationChainFiles {
283287
chain = append(chain, filepath.Join(scenarioPath, rel))
284288
}
285-
output, code := runCommand(t, t.TempDir(), binaryPath,
289+
output, code := mustRunCommand(t, t.TempDir(), binaryPath,
286290
"gate", "eval",
287291
"--policy", filepath.Join(scenarioPath, "policy.yaml"),
288292
"--intent", filepath.Join(scenarioPath, "intent.json"),
@@ -313,7 +317,7 @@ func runDelegationScenario(t *testing.T, repoRoot string, binaryPath string, sce
313317

314318
func runApprovalScenario(t *testing.T, repoRoot string, binaryPath string, scenarioPath string) {
315319
expected := readExpectedYAML(t, filepath.Join(scenarioPath, "expected.yaml"))
316-
output, code := runCommand(t, t.TempDir(), binaryPath,
320+
output, code := mustRunCommand(t, t.TempDir(), binaryPath,
317321
"gate", "eval",
318322
"--policy", filepath.Join(scenarioPath, "policy.yaml"),
319323
"--intent", filepath.Join(scenarioPath, "intent.json"),
@@ -356,20 +360,27 @@ func buildGaitBinary(t *testing.T, repoRoot string) string {
356360
return binaryPath
357361
}
358362

359-
func runCommand(t *testing.T, workDir string, binaryPath string, args ...string) (string, int) {
363+
func mustRunCommand(t *testing.T, workDir string, binaryPath string, args ...string) (string, int) {
360364
t.Helper()
365+
output, code, err := runCommand(workDir, binaryPath, args...)
366+
if err != nil {
367+
t.Fatalf("run command %v: %v", args, err)
368+
}
369+
return output, code
370+
}
371+
372+
func runCommand(workDir string, binaryPath string, args ...string) (string, int, error) {
361373
cmd := exec.Command(binaryPath, args...)
362374
cmd.Dir = workDir
363375
output, err := cmd.CombinedOutput()
364376
if err == nil {
365-
return strings.TrimSpace(string(output)), 0
377+
return strings.TrimSpace(string(output)), 0, nil
366378
}
367379
var exitErr *exec.ExitError
368380
if errors.As(err, &exitErr) {
369-
return strings.TrimSpace(string(output)), exitErr.ExitCode()
381+
return strings.TrimSpace(string(output)), exitErr.ExitCode(), nil
370382
}
371-
t.Fatalf("run command %v: %v output=%s", args, err, string(output))
372-
return "", -1
383+
return strings.TrimSpace(string(output)), -1, fmt.Errorf("%w output=%s", err, string(output))
373384
}
374385

375386
func readScenarioFlags(t *testing.T, path string) scenarioFlags {

0 commit comments

Comments
 (0)