Skip to content

Commit d641cc4

Browse files
test(cli): add --plain override tests and TTY limitation comment
Add tests verifying --plain overrides GOG_FORCE_OUTPUT and --non-interactive auto-JSON. Add comment explaining why TTY-positive path cannot be tested (captureStdout uses os.Pipe, always non-TTY). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5130385 commit d641cc4

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

internal/cmd/root_autojson_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
"testing"
66
)
77

8+
// NOTE: captureStdout replaces os.Stdout with an os.Pipe, which is always
9+
// non-TTY. This means we can test the "piped stdout → auto-JSON" path but
10+
// cannot test the "real TTY → no auto-JSON" path in unit tests. The TTY
11+
// guard (term.IsTerminal) is effectively always false here.
12+
813
func TestAutoJSON_Version_DefaultsToJSONWhenEnabled(t *testing.T) {
914
t.Setenv("GOG_AUTO_JSON", "1")
1015

@@ -59,6 +64,36 @@ func TestAutoJSON_Version_DefaultsToJSONWhenForceOutputEnabled(t *testing.T) {
5964
}
6065
}
6166

67+
func TestAutoJSON_Version_PlainOverridesForceOutput(t *testing.T) {
68+
t.Setenv("GOG_FORCE_OUTPUT", "1")
69+
70+
out := captureStdout(t, func() {
71+
_ = captureStderr(t, func() {
72+
if err := Execute([]string{"--plain", "version"}); err != nil {
73+
t.Fatalf("Execute: %v", err)
74+
}
75+
})
76+
})
77+
78+
if strings.HasPrefix(strings.TrimSpace(out), "{") || strings.Contains(out, "\"version\"") {
79+
t.Fatalf("expected text output (not json), got: %q", out)
80+
}
81+
}
82+
83+
func TestAutoJSON_Version_PlainOverridesNonInteractive(t *testing.T) {
84+
out := captureStdout(t, func() {
85+
_ = captureStderr(t, func() {
86+
if err := Execute([]string{"--non-interactive", "--plain", "version"}); err != nil {
87+
t.Fatalf("Execute: %v", err)
88+
}
89+
})
90+
})
91+
92+
if strings.HasPrefix(strings.TrimSpace(out), "{") || strings.Contains(out, "\"version\"") {
93+
t.Fatalf("expected text output (not json), got: %q", out)
94+
}
95+
}
96+
6297
func TestAutoJSON_Version_DefaultsToJSONWhenNoInputAndPiped(t *testing.T) {
6398
out := captureStdout(t, func() {
6499
_ = captureStderr(t, func() {

0 commit comments

Comments
 (0)