-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_test.go
More file actions
105 lines (95 loc) · 3.56 KB
/
Copy pathcoverage_test.go
File metadata and controls
105 lines (95 loc) · 3.56 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
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"os"
"strings"
"testing"
)
func TestMainFunctionUsesOsExitHook(t *testing.T) {
oldExit := osExit
oldArgs := os.Args
defer func() { osExit = oldExit; os.Args = oldArgs }()
called := false
osExit = func(code int) {
called = true
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
}
os.Args = []string{"whoop-cli", "version"}
main()
if !called {
t.Fatal("expected osExit hook to be called")
}
}
func TestRunMainReturnsErrorCode(t *testing.T) {
if code := runMain([]string{"does-not-exist"}); code != 2 {
t.Fatalf("expected 2, got %d", code)
}
}
func TestVersionOutput(t *testing.T) {
stdout, stderr, code := ExecuteWithEnv([]string{"version"}, TestEnv{})
if code != 0 || stderr != "" || !strings.Contains(stdout, "whoop-cli ") || !strings.Contains(stdout, `\___/`) {
t.Fatalf("expected branded version output, code=%d stdout=%q stderr=%q", code, stdout, stderr)
}
stdout, stderr, code = ExecuteWithEnv([]string{"version", "--json"}, TestEnv{})
if code != 0 || stderr != "" || !json.Valid([]byte(stdout)) || !strings.Contains(stdout, `"version"`) {
t.Fatalf("expected JSON version output, code=%d stdout=%q stderr=%q", code, stdout, stderr)
}
}
func TestHelpAndUnknownCommands(t *testing.T) {
stdout, stderr, code := ExecuteWithEnv(nil, TestEnv{})
if code != 0 || stderr != "" || stdout == "" {
t.Fatalf("expected help success, got code=%d stdout=%q stderr=%q", code, stdout, stderr)
}
stdout, stderr, code = ExecuteWithEnv([]string{"--help"}, TestEnv{})
if code != 0 || stderr != "" || stdout == "" {
t.Fatalf("expected help flag success")
}
stdout, stderr, code = ExecuteWithEnv([]string{"nope"}, TestEnv{})
if code != 2 || stdout != "" || !json.Valid([]byte(stderr)) {
t.Fatalf("expected JSON unknown command error, code=%d stdout=%q stderr=%q", code, stdout, stderr)
}
}
func TestAuthBranches(t *testing.T) {
configDir := t.TempDir()
cases := [][]string{{"auth"}, {"auth", "status", "--json"}, {"auth", "login", "--json"}, {"auth", "refresh", "--json"}, {"auth", "logout", "--json"}, {"auth", "revoke", "--force", "--json"}, {"auth", "wat", "--json"}}
for _, args := range cases {
ExecuteWithEnv(args, TestEnv{ConfigDir: configDir})
}
}
func TestInvalidInvocations(t *testing.T) {
cases := [][]string{
{"workouts"}, {"workouts", "get"}, {"workouts", "wat"},
{"sleep"}, {"sleep", "get"}, {"sleep", "wat"},
{"cycles"}, {"cycles", "get"}, {"cycles", "sleep", "get"}, {"cycles", "wat"},
{"recovery"}, {"mapping"}, {"mapping", "get"}, {"user", "wat"},
{"feedback"}, {"feedback", "create", "--json"}, {"feedback", "wat"},
}
for _, args := range cases {
_, stderr, code := ExecuteWithEnv(args, TestEnv{ConfigDir: t.TempDir()})
if code == 0 || !json.Valid([]byte(stderr)) {
t.Fatalf("expected JSON error for %v, code=%d stderr=%q", args, code, stderr)
}
}
}
func TestFeedbackListNoFile(t *testing.T) {
stdout, stderr, code := ExecuteWithEnv([]string{"feedback", "list", "--json"}, TestEnv{ConfigDir: t.TempDir()})
if code != 0 || stderr != "" || !json.Valid([]byte(stdout)) {
t.Fatalf("expected empty feedback list JSON, code=%d stderr=%q stdout=%q", code, stderr, stdout)
}
}
func TestParseLimitErrors(t *testing.T) {
cases := [][]string{{"--limit"}, {"--limit", "abc"}, {"--limit", "0"}}
for _, args := range cases {
_, stderr, code := parseLimit(args, "example")
if code != 2 || !json.Valid([]byte(stderr)) {
t.Fatalf("expected JSON limit error for %v", args)
}
}
}
func TestListEndpointUnknown(t *testing.T) {
if _, ok := listEndpoint("unknown"); ok {
t.Fatal("expected unknown resource")
}
}