-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
383 lines (309 loc) · 10.2 KB
/
main_test.go
File metadata and controls
383 lines (309 loc) · 10.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestCLIBasicUsage tests basic CLI functionality with a real binary.
func TestCLIBasicUsage(t *testing.T) {
// Build gostrings binary
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
// Build test binary
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
import "fmt"
func main() {
fmt.Println("CLI_TEST_STRING_12345")
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run gostrings on test binary
cmd = exec.Command(gostringsBin, testBin)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v\nOutput: %s", err, out.String())
}
// Check output contains expected string
output := out.String()
if !strings.Contains(output, "CLI_TEST_STRING_12345") {
t.Errorf("gostrings output missing expected string\nOutput: %s", output)
}
}
// TestCLIMinLengthFlag tests the -n flag for minimum string length.
func TestCLIMinLengthFlag(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
import "fmt"
func main() {
fmt.Println("MINLEN_TEST_MARKER_12345")
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run with -n 20 (minimum length 20) - should find our marker
cmd = exec.Command(gostringsBin, "-n", "20", testBin)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
output := out.String()
if !strings.Contains(output, "MINLEN_TEST_MARKER_12345") {
t.Errorf("gostrings -n 20 did not find expected string")
}
// Run with -n 50 (minimum length 50) - should NOT find our marker
cmd = exec.Command(gostringsBin, "-n", "50", testBin)
out.Reset()
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
output = out.String()
if strings.Contains(output, "MINLEN_TEST_MARKER_12345") {
t.Errorf("gostrings -n 50 should not find 25-char string")
}
}
// TestCLIUniqueFlag tests the -u flag for unique strings only.
func TestCLIUniqueFlag(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
import "fmt"
func main() {
fmt.Println("UNIQUE_TEST_MARKER_XYZ")
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run without -u flag (may have duplicates)
cmd = exec.Command(gostringsBin, testBin)
var outNonUnique bytes.Buffer
cmd.Stdout = &outNonUnique
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
// Run with -u flag
cmd = exec.Command(gostringsBin, "-u", testBin)
var outUnique bytes.Buffer
cmd.Stdout = &outUnique
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
// Unique output should be shorter or equal
nonUniqueLines := len(strings.Split(strings.TrimSpace(outNonUnique.String()), "\n"))
uniqueLines := len(strings.Split(strings.TrimSpace(outUnique.String()), "\n"))
if uniqueLines > nonUniqueLines {
t.Errorf("gostrings -u returned more strings (%d) than without -u (%d)", uniqueLines, nonUniqueLines)
}
// Verify our marker appears in both
if !strings.Contains(outUnique.String(), "UNIQUE_TEST_MARKER_XYZ") {
t.Error("gostrings -u did not find expected marker string")
}
}
// TestCLINoArguments tests CLI behavior with no arguments.
func TestCLINoArguments(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
// Run with no arguments
cmd = exec.Command(gostringsBin)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
t.Error("gostrings with no arguments should fail, got success")
}
// Should print usage information
stderrStr := stderr.String()
if !strings.Contains(stderrStr, "Usage:") {
t.Errorf("gostrings with no args should print usage, got: %s", stderrStr)
}
}
// TestCLINonexistentFile tests CLI behavior with nonexistent file.
func TestCLINonexistentFile(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
// Run with nonexistent file
cmd = exec.Command(gostringsBin, "/nonexistent/file/path")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
t.Error("gostrings with nonexistent file should fail, got success")
}
// Should print error message
stderrStr := stderr.String()
if !strings.Contains(stderrStr, "Error:") {
t.Errorf("gostrings with nonexistent file should print error, got: %s", stderrStr)
}
}
// TestCLIVerboseFlag tests the -v flag for verbose output.
func TestCLIVerboseFlag(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
import "fmt"
func main() {
fmt.Println("verbose_test")
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run with -v flag
cmd = exec.Command(gostringsBin, "-v", testBin)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
// Verbose output should go to stderr and include format detection
stderrStr := stderr.String()
if !strings.Contains(stderrStr, "Detected") {
t.Errorf("gostrings -v should print format detection to stderr, got: %s", stderrStr)
}
}
// TestCLICombinedFlags tests multiple flags together.
func TestCLICombinedFlags(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
import "fmt"
func main() {
fmt.Println("combined_flags_test_string")
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run with -n, -u, and -v flags
cmd = exec.Command(gostringsBin, "-n", "10", "-u", "-v", testBin)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
// Check verbose output on stderr
if !strings.Contains(stderr.String(), "Detected") {
t.Errorf("expected verbose output on stderr")
}
// Check results respect minimum length
lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
for _, line := range lines {
if line != "" && len(line) < 10 {
t.Errorf("gostrings -n 10 returned string %q with length %d < 10", line, len(line))
}
}
}
// TestCLIFunctionNames tests that function names appear in the strings list.
func TestCLIFunctionNames(t *testing.T) {
gostringsBin := filepath.Join(t.TempDir(), "gostrings")
cmd := exec.Command("go", "build", "-o", gostringsBin, ".")
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build gostrings: %v", err)
}
tmpDir := t.TempDir()
testSrc := filepath.Join(tmpDir, "test.go")
testBin := filepath.Join(tmpDir, "test")
testCode := `package main
func myUniqueTestFunction() {
println("test")
}
func anotherSpecialFunction() {
println("test2")
}
func main() {
myUniqueTestFunction()
anotherSpecialFunction()
}
`
if err := os.WriteFile(testSrc, []byte(testCode), 0o644); err != nil {
t.Fatalf("failed to write test source: %v", err)
}
cmd = exec.Command("go", "build", "-o", testBin, testSrc)
if err := cmd.Run(); err != nil {
t.Fatalf("failed to build test binary: %v", err)
}
// Run gostrings on test binary
cmd = exec.Command(gostringsBin, testBin)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
t.Fatalf("gostrings command failed: %v", err)
}
output := out.String()
// Verify function names appear in output
if !strings.Contains(output, "myUniqueTestFunction") {
t.Errorf("gostrings output missing function name 'myUniqueTestFunction'\nOutput: %s", output)
}
if !strings.Contains(output, "anotherSpecialFunction") {
t.Errorf("gostrings output missing function name 'anotherSpecialFunction'\nOutput: %s", output)
}
}