|
6 | 6 | "errors" |
7 | 7 | "io" |
8 | 8 | "net/http" |
| 9 | + "os" |
9 | 10 | "strings" |
10 | 11 | "testing" |
11 | 12 |
|
@@ -209,6 +210,76 @@ func TestIsMachineOutput_JSONFlag(t *testing.T) { |
209 | 210 | assert.True(t, isMachineOutput(cmd)) |
210 | 211 | } |
211 | 212 |
|
| 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 | + |
212 | 283 | func TestIsNonInteractiveCommand_NonInteractiveEnv(t *testing.T) { |
213 | 284 | t.Setenv("BASECAMP_NONINTERACTIVE", "1") |
214 | 285 |
|
|
0 commit comments