-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_integration_test.go
More file actions
189 lines (154 loc) · 4.05 KB
/
main_integration_test.go
File metadata and controls
189 lines (154 loc) · 4.05 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
180
181
182
183
184
185
186
187
188
189
package main
import (
"bytes"
"encoding/json"
"flag"
"os"
"path/filepath"
"testing"
)
func TestMainWithFixtures(t *testing.T) {
// Save original values
oldArgs := os.Args
oldStdout := os.Stdout
oldStderr := os.Stderr
defer func() {
os.Args = oldArgs
os.Stdout = oldStdout
os.Stderr = oldStderr
}()
// Create a buffer to capture stdout
var buf bytes.Buffer
// Create temp files for redirecting stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stdout = w
// Set up command line args to use fixtures
fixturesDir, err := filepath.Abs(".fixtures")
if err != nil {
t.Fatalf("failed to get fixtures dir: %v", err)
}
interfaceFile := filepath.Join(fixturesDir, "internal", "app", "app.go")
searchDir := filepath.Join(fixturesDir, "pkg")
os.Args = []string{
"gofindimpl",
"-interface", interfaceFile + ":App",
"-dir", searchDir,
}
// Reset flag package state for clean test
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Change to fixtures directory for go.mod
oldDir, _ := os.Getwd()
defer os.Chdir(oldDir)
os.Chdir(fixturesDir)
// Capture output in goroutine
done := make(chan bool)
readDone := make(chan bool)
// Start reading first
go func() {
defer close(readDone)
defer r.Close()
buf.ReadFrom(r)
}()
// Then start main function
go func() {
defer close(done)
defer w.Close()
// This should not panic and should find implementations
defer func() {
if r := recover(); r != nil {
t.Errorf("main() panicked: %v", r)
}
}()
main()
}()
// Wait for both to complete
<-done
<-readDone
// Parse the JSON output
var results []Implementation
if err := json.Unmarshal(buf.Bytes(), &results); err != nil {
t.Fatalf("failed to parse JSON output: %v", err)
}
// Verify we found the expected implementations
if len(results) != 3 {
t.Errorf("expected 3 implementations, got %d", len(results))
}
// Check that we found the expected structs
expectedStructs := map[string]string{
"WebServer": "something1",
"ServiceDaemon": "something2",
"MicroService": "something3",
}
foundStructs := make(map[string]string)
for _, result := range results {
foundStructs[result.Struct] = result.Package
}
for expectedStruct, expectedPackage := range expectedStructs {
if foundPackage, exists := foundStructs[expectedStruct]; !exists {
t.Errorf("expected struct %s not found", expectedStruct)
} else if foundPackage != expectedPackage {
t.Errorf("expected %s to be in package %s, got %s",
expectedStruct, expectedPackage, foundPackage)
}
}
}
func TestMainHelp(t *testing.T) {
// Save original values
oldArgs := os.Args
oldStderr := os.Stderr
defer func() {
os.Args = oldArgs
os.Stderr = oldStderr
}()
// Capture stderr for help output
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stderr = w
// Set up command line args for help
os.Args = []string{"gofindimpl", "-help"}
// Reset flag package state
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Capture output
var buf bytes.Buffer
done := make(chan bool)
readDone := make(chan bool)
// Start reading first
go func() {
defer close(readDone)
defer r.Close()
buf.ReadFrom(r)
}()
// Then start main function
go func() {
defer close(done)
defer w.Close()
// This should call os.Exit(0) for help, but we can't test that easily
// Instead we test that it doesn't panic unexpectedly
defer func() {
if r := recover(); r != nil {
// Help might cause os.Exit which could panic in test
// That's expected behavior
}
}()
main()
}()
// Wait for both to complete
<-done
<-readDone
// Check that help output contains expected text
output := buf.String()
if len(output) > 0 {
// If we got output, verify it looks like help text
expectedTexts := []string{"Usage:", "Options:", "Example:"}
for _, expected := range expectedTexts {
if !bytes.Contains(buf.Bytes(), []byte(expected)) {
t.Errorf("help output should contain '%s'", expected)
}
}
}
}