| 1 | package cmdenv |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | cmds "github.com/ipfs/go-ipfs-cmds" |
| 8 | ) |
| 9 | |
| 10 | func TestShouldShowProgress(t *testing.T) { |
| 11 | const flag = "progress" |
| 12 | makeReq := func(opts map[string]any) *cmds.Request { |
| 13 | if opts == nil { |
| 14 | opts = map[string]any{} |
| 15 | } |
| 16 | return &cmds.Request{Options: opts} |
| 17 | } |
| 18 | |
| 19 | t.Run("explicit true wins regardless of TTY", func(t *testing.T) { |
| 20 | if !ShouldShowProgress(makeReq(map[string]any{flag: true}), flag) { |
| 21 | t.Error("expected true for --progress=true") |
| 22 | } |
| 23 | }) |
| 24 | |
| 25 | t.Run("explicit false wins regardless of TTY", func(t *testing.T) { |
| 26 | if ShouldShowProgress(makeReq(map[string]any{flag: false}), flag) { |
| 27 | t.Error("expected false for --progress=false") |
| 28 | } |
| 29 | }) |
| 30 | |
| 31 | t.Run("unset defaults to IsTerminal(stderr)", func(t *testing.T) { |
| 32 | got := ShouldShowProgress(makeReq(nil), flag) |
| 33 | want := IsTerminal(os.Stderr) |
| 34 | if got != want { |
| 35 | t.Errorf("ShouldShowProgress(unset) = %v, want IsTerminal(os.Stderr) = %v", got, want) |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | t.Run("non-bool value treated as unset", func(t *testing.T) { |
| 40 | got := ShouldShowProgress(makeReq(map[string]any{flag: "yes"}), flag) |
| 41 | want := IsTerminal(os.Stderr) |
| 42 | if got != want { |
| 43 | t.Errorf("ShouldShowProgress(non-bool) = %v, want IsTerminal(os.Stderr) = %v", got, want) |
| 44 | } |
| 45 | }) |
| 46 | } |