-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
53 lines (47 loc) · 1.38 KB
/
main_test.go
File metadata and controls
53 lines (47 loc) · 1.38 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
package main
// main_test.go — covers the `run()` entry-point that main() delegates to.
// This lets us assert behaviour without calling os.Exit. The cmd package's
// own test suite exhaustively covers cobra invocations, so here we only
// need exit-code translation + stderr printing.
import (
"bytes"
"strings"
"testing"
)
func TestRun_HelpReturnsZero(t *testing.T) {
var buf bytes.Buffer
code := run([]string{"--help"}, &buf)
if code != 0 {
t.Errorf("--help exit code = %d, want 0", code)
}
}
func TestRun_VersionReturnsZero(t *testing.T) {
var buf bytes.Buffer
code := run([]string{"--version"}, &buf)
if code != 0 {
t.Errorf("--version exit code = %d, want 0", code)
}
}
func TestRun_UnknownFlagReturnsNonZero(t *testing.T) {
var buf bytes.Buffer
code := run([]string{"--definitely-not-a-flag"}, &buf)
if code == 0 {
t.Error("unknown flag should produce non-zero exit code")
}
if buf.Len() == 0 {
t.Error("unknown flag should print to stderr")
}
// Print should mention the unknown flag.
out := buf.String()
if !strings.Contains(out, "definitely-not-a-flag") && !strings.Contains(out, "unknown") {
t.Logf("stderr: %q", out) // informational
}
}
func TestRun_EmptyArgsReturnsZero(t *testing.T) {
var buf bytes.Buffer
code := run([]string{}, &buf)
// No args -> cobra prints help; exit code is 0.
if code != 0 {
t.Errorf("empty args exit code = %d, want 0", code)
}
}