mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
UpdateNotification() wrote to stdout via fmt.Printf, corrupting JSON output when --json was passed to commands like search. Machine consumers got invalid JSON with trailing human-readable text. Three-layer fix: 1. Move UpdateNotification output to os.Stderr (safety net for any caller that bypasses the main.go guard). 2. Add isStructuredOutput() that detects all machine-readable modes: --json, -j, and --format json/sarif/markdown. 3. Skip the update check and trailing newline entirely in main() when structured output is requested, honouring the contract that stdout = pure structured data and stderr = empty. Tests: - Unit: isStructuredOutput table test (9 cases) - Unit: UpdateNotification writes only to stderr - Integration: versioned binary (1.0.0) with seeded cache (9.9.9) exercises the real update path for search --json, list -j, and audit --format json — asserts valid JSON stdout + empty stderr. Closes #129
48 lines
950 B
Go
48 lines
950 B
Go
package ui
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestUpdateNotification_WritesToStderr(t *testing.T) {
|
|
// Capture stderr
|
|
oldStderr := os.Stderr
|
|
rErr, wErr, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Stderr = wErr
|
|
|
|
// Capture stdout
|
|
oldStdout := os.Stdout
|
|
rOut, wOut, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Stdout = wOut
|
|
|
|
UpdateNotification("1.0.0", "2.0.0", "skillshare upgrade")
|
|
|
|
wErr.Close()
|
|
wOut.Close()
|
|
os.Stderr = oldStderr
|
|
os.Stdout = oldStdout
|
|
|
|
var stderrBuf bytes.Buffer
|
|
io.Copy(&stderrBuf, rErr)
|
|
|
|
var stdoutBuf bytes.Buffer
|
|
io.Copy(&stdoutBuf, rOut)
|
|
|
|
if stdoutBuf.Len() > 0 {
|
|
t.Errorf("UpdateNotification must not write to stdout, got: %s", stdoutBuf.String())
|
|
}
|
|
if !strings.Contains(stderrBuf.String(), "1.0.0") || !strings.Contains(stderrBuf.String(), "2.0.0") {
|
|
t.Errorf("UpdateNotification should write version info to stderr, got: %s", stderrBuf.String())
|
|
}
|
|
}
|