root_noninteractive_test.go

 1package cmd
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8func TestRootRunNonInteractiveRequiresSubcommand(t *testing.T) {
 9	// Not parallel: mutates package-level isStdinTerminal.
10
11	// Override stdin terminal check to simulate non-interactive environment.
12	original := isStdinTerminal
13	t.Cleanup(func() {
14		isStdinTerminal = original
15	})
16	isStdinTerminal = func() bool { return false }
17
18	// Reset root command flags to defaults.
19	setRootFlagValuesForTest(t, "", false, "")
20	t.Setenv("KELD_CONFIG_FILE", "")
21	t.Setenv("KELD_DRYRUN", "")
22
23	err := rootCmd.RunE(rootCmd, nil)
24	if err == nil {
25		t.Fatal("expected error for bare keld in non-interactive mode, got nil")
26	}
27
28	errMsg := err.Error()
29	// Should NOT be a bubbletea TTY error.
30	if strings.Contains(errMsg, "bubbletea") || strings.Contains(errMsg, "TTY") {
31		t.Fatalf("non-interactive mode incorrectly tried to open TTY: %v", err)
32	}
33
34	// Should mention missing subcommand.
35	if !strings.Contains(errMsg, "subcommand") {
36		t.Fatalf("expected error to mention subcommand, got: %v", err)
37	}
38}