Skip to content

Commit f88b68a

Browse files
committed
fix(tui): exit 1 when the results file is not written (#155)
reportOutputErr only added a message to the view, so exitCode still returned 0 after a failed write and scripts checking $? treated a truncated or missing results file as success. Track the failure on the model and fail the run, matching the CLI, where a file error outranks everything else.
1 parent 3139c44 commit f88b68a

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎internal/tui/model.go‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type Model struct {
4343
vals formValues // the running scan's settings, for its report
4444
seed uint64
4545
started time.Time
46+
47+
// outputErr records that the results file could not be written, so the
48+
// process exits non-zero even when the scan itself succeeded (#155).
49+
outputErr bool
4650
}
4751

4852
// Options configure the TUI.
@@ -65,8 +69,9 @@ func New(opts Options) Model {
6569
}
6670
}
6771

68-
// Start runs the TUI and returns an exit code: 1 if the TUI itself failed or
69-
// the last scan failed (#55), else 0.
72+
// Start runs the TUI and returns an exit code: 1 if the TUI itself failed,
73+
// the last scan failed (#55), or the results file could not be written (#155),
74+
// else 0.
7075
func Start(opts Options) int {
7176
p := tea.NewProgram(New(opts), tea.WithAltScreen())
7277
final, err := p.Run()
@@ -78,7 +83,7 @@ func Start(opts Options) int {
7883

7984
// exitCode maps the final model to the process exit code.
8085
func exitCode(final tea.Model) int {
81-
if m, ok := final.(Model); ok && m.scanView.failed() {
86+
if m, ok := final.(Model); ok && (m.scanView.failed() || m.outputErr) {
8287
return 1
8388
}
8489
return 0
@@ -331,9 +336,11 @@ func (m *Model) summary(done doneMsg) output.Summary {
331336
return s
332337
}
333338

334-
// reportOutputErr surfaces an output-file failure in the scan view.
339+
// reportOutputErr surfaces an output-file failure in the scan view and makes
340+
// it fail the run, as it does on the command line (#155).
335341
func (m *Model) reportOutputErr(err error) {
336342
if err != nil {
343+
m.outputErr = true
337344
m.scanView.addMessage(errorStyle.Render("✗ output file incomplete: " + err.Error()))
338345
}
339346
}

‎internal/tui/model_test.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ func TestFinalizeOutputReportsCloseError(t *testing.T) {
121121
}
122122
}
123123

124+
// TestOutputFileFailureSetsExitCode covers #155: when the results file cannot
125+
// be written, the TUI exits 1 like the CLI does, even though the scan itself
126+
// succeeded.
127+
func TestOutputFileFailureSetsExitCode(t *testing.T) {
128+
path := filepath.Join(t.TempDir(), "out.txt")
129+
f, err := output.CreateFile(path)
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
m := Model{out: output.NewFile(f.Writer, false, output.FormatText), outFile: f}
134+
m.out.Result("a.example.com", nil)
135+
// Put a non-empty directory where the results file goes, so the final
136+
// rename into place fails.
137+
if err := os.MkdirAll(filepath.Join(path, "blocker"), 0o755); err != nil {
138+
t.Fatal(err)
139+
}
140+
m.reportOutputErr(m.finalizeOutput(true))
141+
if code := exitCode(m); code != 1 {
142+
t.Fatalf("exit code = %d, want 1 after an output-file failure", code)
143+
}
144+
if !strings.Contains(m.scanView.View(), "output file incomplete") {
145+
t.Errorf("scan view does not report the incomplete results file: %s", m.scanView.View())
146+
}
147+
// A run without an output-file failure still exits 0.
148+
if code := exitCode(Model{}); code != 0 {
149+
t.Errorf("exit code = %d, want 0 without an output-file failure", code)
150+
}
151+
}
152+
124153
// TestValidateRejectsStdinWordlist covers #55: "-" would read the stdin that
125154
// Bubble Tea owns and freeze the UI.
126155
func TestValidateRejectsStdinWordlist(t *testing.T) {

0 commit comments

Comments
 (0)