Skip to content

Commit e68ce1d

Browse files
authored
Make IsMachineOutput honor an explicit --styled/--md (#670)
ApplyFlags lets --styled and --md rebuild the writer over a configured json/quiet format, but IsMachineOutput never read those flags — so `basecamp --styled <cmd>` on a format=json machine was human to the renderer and machine to every gate. That split surfaced as the declined review thread on #654: chat delete demanded --force for an invocation whose confirmation a person could see and answer, and chat.go grew humanFormatOverride to state the rule at one call site because the predicate couldn't. The clause sits between the machine-flag block and the config switch, mirroring ApplyFlags' ordering: --json --styled resolves JSON-first there, so machine flags still win here. Deliberately not delegated to EffectiveFormat(): that would also pull in FormatAuto-on-a-pipe, flipping upgrade's stdout, attachment progress, the skill-update nudge, and quickstart/wizard gating for every `basecamp foo | grep`. TestMachinePredicatesAgree pins the whole flag/config matrix against machineReadsThisOutput, asserting that one divergence explicitly instead of skipping it.
1 parent 3d672fc commit e68ce1d

5 files changed

Lines changed: 112 additions & 2 deletions

File tree

‎internal/appctx/context.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ func (a *App) IsMachineOutput() bool {
318318
if a.Flags.Agent || a.Flags.Quiet || a.Flags.IDsOnly || a.Flags.Count || a.Flags.JSON || a.Flags.JQFilter != "" {
319319
return true
320320
}
321+
// An explicit --styled/--md is what ApplyFlags honors over a configured
322+
// machine format; this predicate must agree, or the same invocation is
323+
// human to the renderer and machine to every gate.
324+
if a.Flags.Styled || a.Flags.MD {
325+
return false
326+
}
321327
// Config-driven machine output formats
322328
if a.Config != nil {
323329
switch a.Config.Format {

‎internal/appctx/context_test.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,35 @@ func TestIsMachineOutputConfigFormat(t *testing.T) {
586586
}
587587
}
588588

589+
// Test that an explicit --styled/--md overrides a configured machine format,
590+
// matching ApplyFlags: the flag rebuilds the writer as styled/markdown, so the
591+
// predicate must stand down too, or the same invocation is human to the
592+
// renderer and machine to every gate. Machine flags still win over style flags,
593+
// mirroring ApplyFlags' JSON-first ordering.
594+
func TestIsMachineOutputStyleFlagOverridesConfig(t *testing.T) {
595+
tests := []struct {
596+
name string
597+
format string
598+
setFlags func(*App)
599+
expected bool
600+
}{
601+
{"styled over config json", "json", func(a *App) { a.Flags.Styled = true }, false},
602+
{"md over config quiet", "quiet", func(a *App) { a.Flags.MD = true }, false},
603+
{"config json with no style flag stays machine", "json", func(a *App) {}, true},
604+
{"json flag beats styled flag", "json", func(a *App) { a.Flags.JSON = true; a.Flags.Styled = true }, true},
605+
}
606+
607+
for _, tt := range tests {
608+
t.Run(tt.name, func(t *testing.T) {
609+
cfg := &config.Config{Format: tt.format}
610+
app := NewApp(cfg)
611+
tt.setFlags(app)
612+
613+
assert.Equal(t, tt.expected, app.IsMachineOutput())
614+
})
615+
}
616+
}
617+
589618
// Test that app.Err doesn't print stats in machine output modes
590619
func TestAppErrMachineOutputNoStats(t *testing.T) {
591620
tests := []struct {

‎internal/commands/helpers_test.go‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"io"
88
"net/http"
9+
"os"
910
"strings"
1011
"testing"
1112

@@ -209,6 +210,76 @@ func TestIsMachineOutput_JSONFlag(t *testing.T) {
209210
assert.True(t, isMachineOutput(cmd))
210211
}
211212

213+
// TestMachinePredicatesAgree is the regression net for app.IsMachineOutput and
214+
// machineReadsThisOutput drifting apart. They answer the same question through
215+
// different lenses — flags+config vs the writer ApplyFlags built — and the chat
216+
// delete --force corner came from them disagreeing on `--styled` over a
217+
// configured json. Every flag/config combination must agree, with one
218+
// deliberate exception: FormatAuto on a pipe, where the renderer emits JSON
219+
// (machineReadsThisOutput true) while gates keep treating `basecamp foo | grep`
220+
// as human (IsMachineOutput false) so notices, progress, and wizard gating
221+
// don't flip on redirection alone. That row is asserted as a divergence, not
222+
// skipped, so a change to either side of it shows up here.
223+
func TestMachinePredicatesAgree(t *testing.T) {
224+
t.Setenv("BASECAMP_NO_KEYRING", "1")
225+
226+
// Neither predicate may consult a real terminal: pin all three stdio
227+
// streams to pipes and assert the precondition, so the FormatAuto rows
228+
// resolve deterministically wherever the test runs.
229+
r, w, err := os.Pipe()
230+
require.NoError(t, err)
231+
t.Cleanup(func() { r.Close(); w.Close() })
232+
origIn, origOut, origErr := os.Stdin, os.Stdout, os.Stderr
233+
os.Stdin, os.Stdout, os.Stderr = r, w, w
234+
t.Cleanup(func() { os.Stdin, os.Stdout, os.Stderr = origIn, origOut, origErr })
235+
fi, err := os.Stdout.Stat()
236+
require.NoError(t, err)
237+
require.Zero(t, fi.Mode()&os.ModeCharDevice, "precondition: stdout must not be a terminal")
238+
239+
configFormats := []string{"", "json", "quiet", "markdown"}
240+
flagSets := []struct {
241+
name string
242+
set func(*appctx.App)
243+
}{
244+
{"none", func(a *appctx.App) {}},
245+
{"json", func(a *appctx.App) { a.Flags.JSON = true }},
246+
{"agent", func(a *appctx.App) { a.Flags.Agent = true }},
247+
{"quiet", func(a *appctx.App) { a.Flags.Quiet = true }},
248+
{"ids-only", func(a *appctx.App) { a.Flags.IDsOnly = true }},
249+
{"count", func(a *appctx.App) { a.Flags.Count = true }},
250+
{"jq", func(a *appctx.App) { a.Flags.JQFilter = ".x" }},
251+
{"styled", func(a *appctx.App) { a.Flags.Styled = true }},
252+
{"md", func(a *appctx.App) { a.Flags.MD = true }},
253+
{"styled+json", func(a *appctx.App) { a.Flags.Styled = true; a.Flags.JSON = true }},
254+
}
255+
256+
for _, format := range configFormats {
257+
for _, flags := range flagSets {
258+
name := "config=" + format + "/flags=" + flags.name
259+
t.Run(name, func(t *testing.T) {
260+
app := appctx.NewApp(&config.Config{Format: format})
261+
flags.set(app)
262+
app.ApplyFlags()
263+
264+
cmd := &cobra.Command{Use: "test"}
265+
cmd.SetContext(appctx.WithApp(context.Background(), app))
266+
267+
gate := app.IsMachineOutput()
268+
renderer := machineReadsThisOutput(cmd)
269+
270+
if format == "" && flags.name == "none" {
271+
// The FormatAuto-on-a-pipe exception described above.
272+
assert.False(t, gate, "IsMachineOutput must not flip on redirection alone")
273+
assert.True(t, renderer, "FormatAuto on a pipe renders JSON")
274+
return
275+
}
276+
assert.Equal(t, gate, renderer,
277+
"IsMachineOutput=%v but machineReadsThisOutput=%v for %s", gate, renderer, name)
278+
})
279+
}
280+
}
281+
}
282+
212283
func TestIsNonInteractiveCommand_NonInteractiveEnv(t *testing.T) {
213284
t.Setenv("BASECAMP_NONINTERACTIVE", "1")
214285

‎internal/commands/wizard.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,9 @@ func fetchProjectName(cmd *cobra.Command, app *appctx.App, projectID string) str
487487
// no one of them sees everything: IsInteractive covers non-terminal
488488
// stdin/stdout, the machine-output flags and the BASECAMP_NONINTERACTIVE escape
489489
// hatch; IsMachineOutput adds the config-driven json/quiet formats it does not
490-
// look at; InteractivePrompt adds stderr, which is where huh actually draws.
490+
// look at (standing down when an explicit --styled/--md overrides them, since
491+
// ApplyFlags renders those human); InteractivePrompt adds stderr, which is
492+
// where huh actually draws.
491493
//
492494
// Two callers, deliberately different responses. `basecamp setup` was asked for
493495
// by name, so it refuses out loud. Bare `basecamp` never asked for a wizard at

‎internal/commands/wizard_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,9 @@ func TestSetupRefusesUnderNonInteractiveEnv(t *testing.T) {
756756
// Terminal stdio is not enough: a caller that asked for machine output has
757757
// declared it is not there to answer questions, and the wizard is nothing but
758758
// questions. Config-driven json/quiet counts too — app.IsInteractive() does not
759-
// look at it, which is why the gate also asks IsMachineOutput().
759+
// look at it, which is why the gate also asks IsMachineOutput(). An explicit
760+
// --styled/--md overrides a configured machine format there, so that pairing
761+
// prompts like any human invocation.
760762
func TestSetupRefusesMachineOutputOnATerminal(t *testing.T) {
761763
if runtime.GOOS == "windows" {
762764
t.Skip("no /dev/ptmx on Windows")

0 commit comments

Comments
 (0)