diff --git a/issues.go b/issues.go index 356054d..7c7b301 100644 --- a/issues.go +++ b/issues.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "path/filepath" "strings" @@ -17,12 +18,15 @@ var ( issues []string notices []string errorCount int + // issueLogWriter is the destination for human-facing issue log lines. + // Overridden during tests to suppress intentional error-path output. + issueLogWriter io.Writer = os.Stdout ) func logIssue(level, msg string) { issuesMu.Lock() defer issuesMu.Unlock() - fmt.Printf(" [%s] %s\n", level, msg) + fmt.Fprintf(issueLogWriter, " [%s] %s\n", level, msg) issues = append(issues, fmt.Sprintf("[%s] %s", level, msg)) if level == "ERROR" { errorCount++ diff --git a/issues_test.go b/issues_test.go index f8905d6..7c2d09f 100644 --- a/issues_test.go +++ b/issues_test.go @@ -12,27 +12,23 @@ func TestIssuesLogging(t *testing.T) { defer resetMocks() resetMocks() - // Capture stdout - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + // Capture issue log output via the injectable writer. + var buf bytes.Buffer + oldWriter := issueLogWriter + issueLogWriter = &buf + defer func() { issueLogWriter = oldWriter }() warn("something is deprecated") errLog("something failed") notice("please restart shell") - w.Close() - os.Stdout = oldStdout - - var buf bytes.Buffer - io.Copy(&buf, r) output := buf.String() if !strings.Contains(output, "[WARN] something is deprecated") { - t.Errorf("stdout missing warning: %q", output) + t.Errorf("output missing warning: %q", output) } if !strings.Contains(output, "[ERROR] something failed") { - t.Errorf("stdout missing error: %q", output) + t.Errorf("output missing error: %q", output) } issuesMu.Lock() diff --git a/testmain_test.go b/testmain_test.go new file mode 100644 index 0000000..4145616 --- /dev/null +++ b/testmain_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "io" + "os" + "testing" +) + +// TestMain silences issue-log output for the entire test binary. +// Tests still exercise error paths and assert via errorCount; we just +// don't want the human-facing "[ERROR] ..." prints polluting CI logs +// (GitHub Actions auto-annotates "[ERROR]" lines as workflow errors). +func TestMain(m *testing.M) { + issueLogWriter = io.Discard + os.Exit(m.Run()) +}