forked from danielpaulus/go-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
154 lines (143 loc) · 4.58 KB
/
Copy pathmain_test.go
File metadata and controls
154 lines (143 loc) · 4.58 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main_test
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
var (
update = flag.Bool("update", false, "update golden files")
e2e = flag.Bool("e2e", false, "test with realdevice")
)
func TestDeviceList(t *testing.T) {
if !*e2e {
return
}
output, err := exec.Command("go", "run", "./ios.go", "list").Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(output))
}
func TestHelp_GlobalNoArgs(t *testing.T) {
stdout, stderr, code := runCLI(t)
if code != 0 {
t.Fatalf("exit code = %d, stderr = %q", code, stderr)
}
if stderr != "" {
t.Fatalf("stderr = %q, want empty", stderr)
}
assertGolden(t, "global.golden", stdout)
}
func TestHelp_GlobalFlagsEquivalent(t *testing.T) {
stdoutLong, stderrLong, codeLong := runCLI(t, "--help")
if codeLong != 0 || stderrLong != "" {
t.Fatalf("--help failed: code=%d stderr=%q", codeLong, stderrLong)
}
stdoutShort, stderrShort, codeShort := runCLI(t, "-h")
if codeShort != 0 || stderrShort != "" {
t.Fatalf("-h failed: code=%d stderr=%q", codeShort, stderrShort)
}
if eolFormat(stdoutLong) != eolFormat(stdoutShort) {
t.Fatalf("help outputs differ\n--help:\n%s\n-h:\n%s", stdoutLong, stdoutShort)
}
}
func TestHelp_SubcommandExplicitImplicitAndGlobalFlagOrdering(t *testing.T) {
explicit, explicitErr, explicitCode := runCLI(t, "help", "apps")
if explicitCode != 0 || explicitErr != "" {
t.Fatalf("help apps failed: code=%d stderr=%q", explicitCode, explicitErr)
}
implicit, implicitErr, implicitCode := runCLI(t, "apps", "--help")
if implicitCode != 0 || implicitErr != "" {
t.Fatalf("apps --help failed: code=%d stderr=%q", implicitCode, implicitErr)
}
implicitShort, implicitShortErr, implicitShortCode := runCLI(t, "apps", "-h")
if implicitShortCode != 0 || implicitShortErr != "" {
t.Fatalf("apps -h failed: code=%d stderr=%q", implicitShortCode, implicitShortErr)
}
withGlobal, withGlobalErr, withGlobalCode := runCLI(t, "--udid=abc", "help", "apps")
if withGlobalCode != 0 || withGlobalErr != "" {
t.Fatalf("--udid=abc help apps failed: code=%d stderr=%q", withGlobalCode, withGlobalErr)
}
normalizedExplicit := eolFormat(explicit)
if normalizedExplicit != eolFormat(implicit) {
t.Fatalf("explicit and implicit help differ")
}
if normalizedExplicit != eolFormat(implicitShort) {
t.Fatalf("explicit and short implicit help differ")
}
if normalizedExplicit != eolFormat(withGlobal) {
t.Fatalf("explicit and global-flag help differ")
}
assertGolden(t, "apps.golden", explicit)
}
func TestHelp_NestedSubcommandAndUnknown(t *testing.T) {
explicit, explicitErr, explicitCode := runCLI(t, "help", "tunnel", "start")
if explicitCode != 0 || explicitErr != "" {
t.Fatalf("help tunnel start failed: code=%d stderr=%q", explicitCode, explicitErr)
}
implicit, implicitErr, implicitCode := runCLI(t, "tunnel", "start", "--help")
if implicitCode != 0 || implicitErr != "" {
t.Fatalf("tunnel start --help failed: code=%d stderr=%q", implicitCode, implicitErr)
}
if eolFormat(explicit) != eolFormat(implicit) {
t.Fatalf("nested explicit and implicit help differ")
}
stdout, stderr, code := runCLI(t, "help", "nope")
if code == 0 {
t.Fatalf("expected non-zero exit for unknown topic")
}
if stdout != "" {
t.Fatalf("stdout should be empty for unknown topic, got %q", stdout)
}
if !strings.Contains(stderr, "unknown help topic") {
t.Fatalf("missing unknown topic message: %q", stderr)
}
}
func runCLI(t *testing.T, args ...string) (string, string, int) {
t.Helper()
cmd := exec.Command("go", append([]string{"run", "."}, args...)...)
cmd.Dir = "."
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
code := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
code = exitErr.ExitCode()
} else {
t.Fatalf("run cli: %v", err)
}
}
return stdout.String(), stderr.String(), code
}
func assertGolden(t *testing.T, goldenName string, got string) {
t.Helper()
path := filepath.Join("testdata", "help", goldenName)
got = eolFormat(got)
if *update {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir golden dir: %v", err)
}
if err := os.WriteFile(path, []byte(got), 0o644); err != nil {
t.Fatalf("write golden: %v", err)
}
}
wantBytes, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read golden %s: %v", goldenName, err)
}
want := eolFormat(string(wantBytes))
if got != want {
t.Fatalf("golden mismatch for %s\n--- got ---\n%s\n--- want ---\n%s", goldenName, got, want)
}
}
func eolFormat(s string) string {
return strings.ReplaceAll(s, "\r\n", "\n")
}