-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcli_test.go
More file actions
98 lines (80 loc) · 2.48 KB
/
cli_test.go
File metadata and controls
98 lines (80 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cli
import (
"testing"
cmdpkg "github.com/lets-cli/lets/internal/cmd"
"github.com/spf13/cobra"
)
func TestAllowsMissingConfig(t *testing.T) {
t.Run("help", func(t *testing.T) {
command := &cobra.Command{Use: "help"}
if !allowsMissingConfig(command) {
t.Fatal("expected help to allow missing config")
}
})
t.Run("completion", func(t *testing.T) {
root := cmdpkg.CreateRootCommand("v0.0.0-test", "")
cmdpkg.InitCompletionCmd(root, nil)
command, _, err := root.Find([]string{"completion"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !allowsMissingConfig(command) {
t.Fatal("expected completion to allow missing config")
}
})
t.Run("self subcommand", func(t *testing.T) {
root := cmdpkg.CreateRootCommand("v0.0.0-test", "")
cmdpkg.InitSelfCmd(root, "v0.0.0-test")
command, _, err := root.Find([]string{"self", "doc"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !allowsMissingConfig(command) {
t.Fatal("expected lets self doc to allow missing config")
}
})
t.Run("top level doc does not match self", func(t *testing.T) {
root := cmdpkg.CreateRootCommand("v0.0.0-test", "")
root.AddCommand(&cobra.Command{Use: "doc"})
command, _, err := root.Find([]string{"doc"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if allowsMissingConfig(command) {
t.Fatal("expected top-level doc to require config")
}
})
}
func TestShouldCheckForUpdate(t *testing.T) {
t.Run("should allow normal interactive commands", func(t *testing.T) {
t.Setenv("CI", "")
t.Setenv("LETS_CHECK_UPDATE", "")
if !shouldCheckForUpdate("lets", true) {
t.Fatal("expected update check to be enabled")
}
})
t.Run("should skip non interactive sessions", func(t *testing.T) {
if shouldCheckForUpdate("lets", false) {
t.Fatal("expected non-interactive session to skip update check")
}
})
t.Run("should skip when CI is set", func(t *testing.T) {
t.Setenv("CI", "1")
if shouldCheckForUpdate("lets", true) {
t.Fatal("expected CI to skip update check")
}
})
t.Run("should skip when notifier disabled", func(t *testing.T) {
t.Setenv("LETS_CHECK_UPDATE", "1")
if shouldCheckForUpdate("lets", true) {
t.Fatal("expected opt-out env to skip update check")
}
})
t.Run("should skip internal commands", func(t *testing.T) {
for _, name := range []string{"completion", "help", "lsp", "self"} {
if shouldCheckForUpdate(name, true) {
t.Fatalf("expected %q to skip update check", name)
}
}
})
}