-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
324 lines (300 loc) · 10.6 KB
/
cli_test.go
File metadata and controls
324 lines (300 loc) · 10.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestCLIContract(t *testing.T) {
// Build the wintui binary for testing
exePath := filepath.Join(t.TempDir(), "wintui-test.exe")
buildCmd := exec.Command("go", "build", "-o", exePath, ".")
if out, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to build test binary: %v\nOutput: %s", err, out)
}
// Create a dummy winget executable to mock winget behavior
dummyWingetDir := filepath.Join(t.TempDir(), "bin")
os.MkdirAll(dummyWingetDir, 0755)
dummyWingetSrc := filepath.Join(t.TempDir(), "mock_winget.go")
dummyWinget := filepath.Join(dummyWingetDir, "winget.exe")
// Helper to run wintui with the dummy winget in PATH
runWintui := func(wingetOutput string, args ...string) (string, int) {
mockSrc := `package main
import (
"fmt"
)
func main() {
fmt.Print(` + "`" + wingetOutput + "`" + `)
}
`
err := os.WriteFile(dummyWingetSrc, []byte(mockSrc), 0644)
if err != nil {
t.Fatal(err)
}
buildMock := exec.Command("go", "build", "-o", dummyWinget, dummyWingetSrc)
if out, err := buildMock.CombinedOutput(); err != nil {
t.Fatalf("Failed to build mock winget: %v\nOutput: %s", err, out)
}
cmd := exec.Command(exePath, args...)
cmd.Env = append(os.Environ(), "PATH="+dummyWingetDir+";"+os.Getenv("PATH"))
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err = cmd.Run()
exitCode := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
} else {
t.Fatalf("Unexpected error running wintui: %v", err)
}
}
return out.String(), exitCode
}
t.Run("list", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 winget\n"
out, code := runWintui(output, "list")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, "Name") || !strings.Contains(out, "Version") {
t.Errorf("Expected header row, got: %q", out)
}
if !strings.Contains(out, "Test App1") {
t.Errorf("Expected output to contain 'Test App1', got: %q", out)
}
if !strings.Contains(out, "1 package(s) installed.") {
t.Errorf("Expected install summary, got: %q", out)
}
})
t.Run("check_updates_exist", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 2.0 winget\n"
out, code := runWintui(output, "check")
if code != 1 {
t.Errorf("Expected exit code 1 when updates exist, got %d", code)
}
if !strings.Contains(out, "Available") {
t.Errorf("Expected human-readable table header, got: %q", out)
}
if !strings.Contains(out, "App1") {
t.Errorf("Expected output to contain 'App1', got: %q", out)
}
if !strings.Contains(out, "1 package(s) have updates available.") {
t.Errorf("Expected update summary, got: %q", out)
}
})
t.Run("check_no_updates", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n"
out, code := runWintui(output, "check")
if code != 0 {
t.Errorf("Expected exit code 0 when no updates exist, got %d", code)
}
if !strings.Contains(out, "All packages are up to date.") {
t.Errorf("Expected output 'All packages are up to date.', got: %q", out)
}
})
t.Run("root_check_and_list_flags_are_removed", func(t *testing.T) {
out, code := runWintui("", "--check", "--list")
if code == 0 {
t.Errorf("Expected non-zero exit for removed root flags, got %d", code)
}
if !strings.Contains(out, "unknown flag: --check") {
t.Errorf("Expected unknown flag error for --check, got: %q", out)
}
})
t.Run("json_output", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 2.0 winget\n"
out, code := runWintui(output, "check", "--json")
if code != 1 {
t.Errorf("Expected exit code 1, got %d", code)
}
if !strings.Contains(out, `"id": "App1.ID"`) {
t.Errorf("Expected JSON output, got: %q", out)
}
})
t.Run("check_subcommand_exits_one_when_updates_exist", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 2.0 winget\n"
out, code := runWintui(output, "check")
if code != 1 {
t.Errorf("Expected exit code 1, got %d", code)
}
if !strings.Contains(out, "App1") {
t.Errorf("Expected output to contain 'App1', got: %q", out)
}
})
t.Run("check_subcommand_exits_zero_when_up_to_date", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n"
out, code := runWintui(output, "check")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, "All packages are up to date.") {
t.Errorf("Expected up-to-date message, got: %q", out)
}
})
t.Run("list_subcommand", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 winget\n"
out, code := runWintui(output, "list")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, "Test App1") || !strings.Contains(out, "1 package(s) installed.") {
t.Errorf("Expected list output, got: %q", out)
}
})
t.Run("check_subcommand_json", func(t *testing.T) {
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n" +
"Test App1 App1.ID 1.0 2.0 winget\n"
out, code := runWintui(output, "check", "--json")
if code != 1 {
t.Errorf("Expected exit code 1, got %d", code)
}
if !strings.Contains(out, `"id": "App1.ID"`) {
t.Errorf("Expected JSON output, got: %q", out)
}
})
t.Run("show_subcommand_human_output", func(t *testing.T) {
// show is read-only: doesn't actually call winget. Mock output is
// irrelevant.
out, code := runWintui("", "show", "Mozilla.Firefox")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, "ID: Mozilla.Firefox") {
t.Errorf("Expected ID line, got: %q", out)
}
if !strings.Contains(out, "Effective install command:") {
t.Errorf("Expected install command section, got: %q", out)
}
if !strings.Contains(out, "Effective upgrade command:") {
t.Errorf("Expected upgrade command section, got: %q", out)
}
})
t.Run("show_subcommand_json", func(t *testing.T) {
out, code := runWintui("", "show", "Mozilla.Firefox", "--json")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, `"id": "Mozilla.Firefox"`) {
t.Errorf("Expected JSON id, got: %q", out)
}
if !strings.Contains(out, `"install_args"`) {
t.Errorf("Expected install_args field, got: %q", out)
}
if !strings.Contains(out, `"upgrade_args"`) {
t.Errorf("Expected upgrade_args field, got: %q", out)
}
})
t.Run("show_subcommand_requires_id", func(t *testing.T) {
out, code := runWintui("", "show")
if code == 0 {
t.Errorf("Expected non-zero exit when id is missing, got %d", code)
}
if !strings.Contains(out, "accepts 1 arg") && !strings.Contains(out, "Error") {
t.Errorf("Expected arg-count error, got: %q", out)
}
})
t.Run("show_subcommand_msstore_source", func(t *testing.T) {
out, code := runWintui("", "show", "9NCBCSZSJRSB", "--source", "msstore", "--json")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, `"source": "msstore"`) {
t.Errorf("Expected msstore source in JSON, got: %q", out)
}
if !strings.Contains(out, `"--source",`) || !strings.Contains(out, `"msstore"`) {
t.Errorf("Expected --source msstore in args, got: %q", out)
}
})
t.Run("show_subcommand_rejects_unknown_source", func(t *testing.T) {
// Custom / private sources are deferred (see roadmap). Until they are
// supported end-to-end, --source must reject unknown values rather
// than silently mis-rendering output.
out, code := runWintui("", "show", "Foo.Bar", "--source", "private", "--json")
if code == 0 {
t.Errorf("Expected non-zero exit for --source private, got %d", code)
}
if !strings.Contains(out, "invalid --source") {
t.Errorf("Expected invalid-source error, got: %q", out)
}
})
t.Run("root_help_surfaces_subcommands_and_hides_internals", func(t *testing.T) {
// Examples block must mention each user-facing subcommand so the
// shape is discoverable from `wintui -h` without drilling into
// each subcommand's own help.
out, code := runWintui("", "-h")
if code != 0 {
t.Errorf("Expected exit code 0 for -h, got %d", code)
}
for _, want := range []string{
"Examples:",
"wintui check",
"wintui list",
"wintui show",
"wintui upgrade --all",
"wintui upgrade --auto",
"wintui upgrade --id",
} {
if !strings.Contains(out, want) {
t.Errorf("Expected %q in -h output, got: %q", want, out)
}
}
// Internal retry-handoff plumbing must not pollute user-facing help.
for _, hidden := range []string{
"--retry-op",
"--retry-batch",
"--package-version",
} {
if strings.Contains(out, hidden) {
t.Errorf("Did not expect %q in user -h output (internal flag): %q", hidden, out)
}
}
})
t.Run("upgrade_requires_all_flag", func(t *testing.T) {
// Without --all there is no action; cobra should surface the hint
// without ever calling winget.
out, code := runWintui("", "upgrade")
if code == 0 {
t.Errorf("Expected non-zero exit when --all is missing, got %d", code)
}
if !strings.Contains(out, "--all") {
t.Errorf("Expected --all hint in error, got: %q", out)
}
})
t.Run("upgrade_all_with_no_updates", func(t *testing.T) {
// Empty winget upgrade list → nothing to do, exit 0.
output := "Name Id Version Available Source\n" +
"----------------------------------------------\n"
out, code := runWintui(output, "upgrade", "--all")
if code != 0 {
t.Errorf("Expected exit code 0, got %d", code)
}
if !strings.Contains(out, "up to date") {
t.Errorf("Expected up-to-date message, got: %q", out)
}
})
t.Run("root_check_flag_is_removed", func(t *testing.T) {
out, code := runWintui("", "--check")
if code == 0 {
t.Errorf("Expected non-zero exit for removed --check, got %d", code)
}
if !strings.Contains(out, "unknown flag: --check") {
t.Errorf("Expected unknown flag error, got: %q", out)
}
})
}