forked from 1024XEngineer/neo-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
119 lines (103 loc) · 2.99 KB
/
Copy pathmain_test.go
File metadata and controls
119 lines (103 loc) · 2.99 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
package main
import (
"context"
"errors"
"strings"
"testing"
)
type testExitError struct {
message string
code int
}
func (e testExitError) Error() string {
return e.message
}
func (e testExitError) ExitCode() int {
return e.code
}
func TestRunMainReturnsExitCodeFromCLI(t *testing.T) {
originalExecute := executeCLI
originalConsume := consumeCLIUpdateNotice
t.Cleanup(func() {
executeCLI = originalExecute
consumeCLIUpdateNotice = originalConsume
})
executeCLI = func(context.Context) error {
return testExitError{message: "boom", code: 7}
}
consumeCLIUpdateNotice = func() string { return "" }
var stdout strings.Builder
var stderr strings.Builder
exitCode := runMain(context.Background(), &stdout, &stderr)
if exitCode != 7 {
t.Fatalf("exitCode = %d, want 7", exitCode)
}
if stdout.String() != "" {
t.Fatalf("stdout = %q, want empty", stdout.String())
}
if !strings.Contains(stderr.String(), "neocode: boom") {
t.Fatalf("stderr = %q, want error output", stderr.String())
}
}
func TestMainInvokesExitProcessWithRunMainResult(t *testing.T) {
originalExecute := executeCLI
originalConsume := consumeCLIUpdateNotice
originalExit := exitProcess
t.Cleanup(func() {
executeCLI = originalExecute
consumeCLIUpdateNotice = originalConsume
exitProcess = originalExit
})
executeCLI = func(context.Context) error {
return testExitError{message: "boom", code: 6}
}
consumeCLIUpdateNotice = func() string { return "" }
var gotExitCode int
exitProcess = func(code int) {
gotExitCode = code
}
main()
if gotExitCode != 6 {
t.Fatalf("exit code = %d, want 6", gotExitCode)
}
}
func TestRunMainPrintsUpdateNoticeOnSuccess(t *testing.T) {
originalExecute := executeCLI
originalConsume := consumeCLIUpdateNotice
t.Cleanup(func() {
executeCLI = originalExecute
consumeCLIUpdateNotice = originalConsume
})
executeCLI = func(context.Context) error { return nil }
consumeCLIUpdateNotice = func() string { return "update available" }
var stdout strings.Builder
var stderr strings.Builder
exitCode := runMain(context.Background(), &stdout, &stderr)
if exitCode != 0 {
t.Fatalf("exitCode = %d, want 0", exitCode)
}
if !strings.Contains(stdout.String(), "update available") {
t.Fatalf("stdout = %q, want update notice", stdout.String())
}
if stderr.String() != "" {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
}
func TestRunMainFallsBackToExitCodeOne(t *testing.T) {
originalExecute := executeCLI
originalConsume := consumeCLIUpdateNotice
t.Cleanup(func() {
executeCLI = originalExecute
consumeCLIUpdateNotice = originalConsume
})
executeCLI = func(context.Context) error { return errors.New("plain failure") }
consumeCLIUpdateNotice = func() string { return "" }
var stderr strings.Builder
exitCode := runMain(context.Background(), &strings.Builder{}, &stderr)
if exitCode != 1 {
t.Fatalf("exitCode = %d, want 1", exitCode)
}
if !strings.Contains(stderr.String(), "plain failure") {
t.Fatalf("stderr = %q, want plain failure", stderr.String())
}
}