forked from mna/pigeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
54 lines (48 loc) · 994 Bytes
/
main_test.go
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
package main
import (
"os"
"strings"
"testing"
)
func TestMain(t *testing.T) {
stdout, stderr := os.Stdout, os.Stderr
os.Stdout, _ = os.Open(os.DevNull)
os.Stderr, _ = os.Open(os.DevNull)
defer func() {
exit = os.Exit
os.Stdout = stdout
os.Stderr = stderr
}()
exit = func(code int) {
panic(code)
}
cases := []struct {
args string
code int
}{
{args: "", code: 3}, // stdin: no match found
{args: "-h", code: 0}, // help
{args: "FILE1 FILE2", code: 1}, // want only 1 non-flag arg
{args: "-x", code: 3}, // stdin: no match found
}
for _, tc := range cases {
os.Args = append([]string{"pigeon"}, strings.Fields(tc.args)...)
got := runMainRecover()
if got != tc.code {
t.Errorf("%q: want code %d, got %d", tc.args, tc.code, got)
}
}
}
func runMainRecover() (code int) {
defer func() {
if e := recover(); e != nil {
if i, ok := e.(int); ok {
code = i
return
}
panic(e)
}
}()
main()
return 0
}