-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
179 lines (153 loc) · 4.25 KB
/
Copy pathmain_test.go
File metadata and controls
179 lines (153 loc) · 4.25 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
package main
import (
"bytes"
"errors"
"os"
"testing"
"github.com/zhaochunqi/git-open/cmd"
"github.com/zhaochunqi/git-open/internal/testhelper"
)
func Test_runApp(t *testing.T) {
// Save original args and restore after test
oldArgs := os.Args
defer func() {
os.Args = oldArgs
}()
// Save original OpenURLInBrowser function and restore after test
originalOpenURL := cmd.OpenURLInBrowser
defer func() {
cmd.OpenURLInBrowser = originalOpenURL
}()
// Save original cmd.Execute and restore after test
originalExecute := cmd.Execute
defer func() {
cmd.Execute = originalExecute
}()
// Save original exitFunc and restore after test
originalExitFunc := exitFunc
defer func() {
exitFunc = originalExitFunc
}()
// Mock OpenURLInBrowser function
var openedURL string
cmd.OpenURLInBrowser = func(url string) error {
openedURL = url
return nil
}
tests := []struct {
name string
args []string
wantURL string
executeErr error // New field to simulate cmd.Execute error
wantErr bool // Expect runApp to return an error
}{
{
name: "default behavior",
args: []string{"git-open"},
wantURL: "https://github.com/zhaochunqi/git-open/tree/feat/open-branch",
executeErr: nil,
wantErr: false,
},
{
name: "error from cmd.Execute",
args: []string{"git-open"},
wantURL: "", // Not relevant for this test
executeErr: errors.New("mock execute error"),
wantErr: true,
},
}
for _, tt := range tests {
// Mock os.Exit to capture the exit code
var actualExitCode int
exitFunc = func(code int) {
actualExitCode = code
// We don't want to actually exit during tests
panic("os.Exit called")
}
t.Run(tt.name, func(t *testing.T) {
// Setup test repository for this test case
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "feat/open-branch")
t.Cleanup(cleanup)
os.Args = tt.args
openedURL = ""
// Mock cmd.Execute to return a specific error or call original
cmd.Execute = func() error {
if tt.executeErr != nil {
return tt.executeErr
}
// Call the original cmd.Execute if no error is simulated
return originalExecute()
}
// Capture stdout and stderr
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stdout = w
os.Stderr = w
// Call runApp and recover from panic if os.Exit is called
func() {
defer func() {
if r := recover(); r != nil && r.(string) != "os.Exit called" {
t.Fatalf("Unexpected panic: %v", r)
}
}()
if err := runApp(); (err != nil) != tt.wantErr {
t.Errorf("runApp() error = %v, wantErr %v", err, tt.wantErr)
}
}()
w.Close()
os.Stdout = oldStdout
os.Stderr = oldStderr
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
if tt.wantErr && output == "" {
t.Errorf("Expected error output, but got none")
}
if tt.executeErr == nil && openedURL != tt.wantURL {
t.Errorf("OpenURLInBrowser called with %v, want %v", openedURL, tt.wantURL)
}
// For error case, check if os.Exit was called with 1
if tt.wantErr && actualExitCode != 1 {
t.Errorf("Expected os.Exit(1) to be called, but got %v", actualExitCode)
}
})
}
}
func Test_main_func(t *testing.T) {
// This test ensures that the main function calls runApp
// and handles os.Exit correctly.
// Save original exitFunc and restore after test
originalExitFunc := exitFunc
defer func() {
exitFunc = originalExitFunc
}()
// Save original cmd.Execute and restore after test
originalExecute := cmd.Execute
defer func() {
cmd.Execute = originalExecute
}()
// Mock os.Exit to capture the exit code
var actualExitCode int
exitFunc = func(code int) {
actualExitCode = code
panic("os.Exit called") // Panic to stop execution
}
// Mock cmd.Execute to return an error
cmd.Execute = func() error {
return errors.New("mock error for main func test")
}
// Call main and recover from panic
func() {
defer func() {
if r := recover(); r != nil && r.(string) != "os.Exit called" {
t.Fatalf("Unexpected panic: %v", r)
}
}()
main()
}()
// Verify that os.Exit was called with 1
if actualExitCode != 1 {
t.Errorf("Expected os.Exit(1) to be called, but got %v", actualExitCode)
}
}