forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
58 lines (50 loc) · 1.31 KB
/
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
55
56
57
58
package main
import (
"fmt"
"os"
"testing"
"github.com/bpicode/fritzctl/internal/errors"
"github.com/stretchr/testify/assert"
)
// TestBlankRun runs the app without args.
func TestBlankRun(t *testing.T) {
defer func() {
exit = os.Exit
}()
exit = func(code int) {}
main()
}
// TestHelp runs the app with the help flag.
func TestHelp(t *testing.T) {
osArgsBefore := os.Args
exit = func(code int) {}
defer func() {
os.Args = osArgsBefore
exit = os.Exit
}()
os.Args = []string{"app_called_in_test_scope", "--help"}
main()
}
// TestVersion runs the app with the version flag.
func TestVersion(t *testing.T) {
osArgsBefore := os.Args
exit = func(code int) {}
defer func() {
os.Args = osArgsBefore
exit = os.Exit
}()
os.Args = []string{"app_called_in_test_scope", "--version"}
main()
}
// TestDetermineExitCode tests the exit code determination.
func TestDetermineExitCode(t *testing.T) {
assert.Equal(t, 0, determineExitCode(nil))
assert.Equal(t, 1, determineExitCode("an error"))
}
//// TestStack exercises the stack traversal.
func TestStack(t *testing.T) {
assert.Len(t, stack(nil), 0)
assert.Equal(t, []string{"error"}, stack("error"))
assert.Equal(t, []string{"error"}, stack(fmt.Errorf("error")))
assert.Equal(t, []string{"parent:", "root"}, stack(errors.Wrapf(fmt.Errorf("root"), "parent")))
}