1package cmd
2
3import (
4 "testing"
5)
6
7func TestIsInteractive(t *testing.T) {
8 tests := []struct {
9 name string
10 stdinIsTerminal bool
11 envValue string // empty string means env is cleared/unset
12 want bool
13 }{
14 {
15 name: "terminal with empty env returns true",
16 stdinIsTerminal: true,
17 envValue: "",
18 want: true,
19 },
20 {
21 name: "terminal with env=1 returns false",
22 stdinIsTerminal: true,
23 envValue: "1",
24 want: false,
25 },
26 {
27 name: "terminal with env=true returns false",
28 stdinIsTerminal: true,
29 envValue: "true",
30 want: false,
31 },
32 {
33 name: "non-terminal with empty env returns false",
34 stdinIsTerminal: false,
35 envValue: "",
36 want: false,
37 },
38 {
39 name: "non-terminal with env=1 returns false",
40 stdinIsTerminal: false,
41 envValue: "1",
42 want: false,
43 },
44 {
45 name: "terminal with env=0 returns false",
46 stdinIsTerminal: true,
47 envValue: "0",
48 want: false,
49 },
50 }
51
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 // Save and restore the original function.
55 original := isStdinTerminal
56 t.Cleanup(func() {
57 isStdinTerminal = original
58 })
59
60 // Inject fake terminal check.
61 isStdinTerminal = func() bool {
62 return tt.stdinIsTerminal
63 }
64
65 // Set env var. t.Setenv("", "") clears/unsets it.
66 t.Setenv("KELD_NONINTERACTIVE", tt.envValue)
67
68 got := isInteractive()
69 if got != tt.want {
70 t.Errorf("isInteractive() = %v, want %v", got, tt.want)
71 }
72 })
73 }
74}