-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
263 lines (225 loc) · 6.29 KB
/
main_test.go
File metadata and controls
263 lines (225 loc) · 6.29 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
package main
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"codemap/scanner"
)
// TestMain runs before all tests
func TestMain(m *testing.M) {
// Build the binary for integration tests
cmd := exec.Command("go", "build", "-o", "codemap_test_binary", ".")
if err := cmd.Run(); err != nil {
os.Exit(1)
}
code := m.Run()
os.Remove("codemap_test_binary")
os.Exit(code)
}
func runCodemap(args ...string) (string, error) {
cmd := exec.Command("./codemap_test_binary", args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return stderr.String(), err
}
return out.String(), nil
}
func TestHelpFlag(t *testing.T) {
output, err := runCodemap("--help")
if err != nil {
t.Fatalf("--help failed: %v", err)
}
expectedStrings := []string{
"codemap",
"Usage:",
"--skyline",
"--deps",
"--diff",
"--ref",
}
for _, expected := range expectedStrings {
if !strings.Contains(output, expected) {
t.Errorf("Help output should contain %q", expected)
}
}
}
func TestBasicTreeOutput(t *testing.T) {
output, err := runCodemap(".")
if err != nil {
t.Fatalf("Basic tree failed: %v", err)
}
// Should contain project name and file stats
if !strings.Contains(output, "Files:") && !strings.Contains(output, "Changed:") {
t.Error("Output should contain file stats")
}
// Should contain some Go files from this project
if !strings.Contains(output, ".go") {
t.Error("Output should show .go files for this Go project")
}
}
func TestJSONOutput(t *testing.T) {
output, err := runCodemap("--json", ".")
if err != nil {
t.Fatalf("JSON output failed: %v", err)
}
// Should be valid JSON
var project scanner.Project
if err := json.Unmarshal([]byte(output), &project); err != nil {
t.Errorf("Output should be valid JSON: %v", err)
}
// Verify structure
if project.Root == "" {
t.Error("JSON should have root field")
}
if project.Mode != "tree" {
t.Errorf("Expected mode 'tree', got %q", project.Mode)
}
if len(project.Files) == 0 {
t.Error("JSON should have files")
}
// Verify file info structure
for _, f := range project.Files {
if f.Path == "" {
t.Error("File path should not be empty")
}
}
}
func TestSubdirectoryPath(t *testing.T) {
// Test scanning a subdirectory
output, err := runCodemap("scanner")
if err != nil {
t.Fatalf("Subdirectory scan failed: %v", err)
}
// Should contain files from scanner directory
if !strings.Contains(output, ".go") {
t.Error("Should show .go files in scanner directory")
}
}
func TestNonexistentPath(t *testing.T) {
_, err := runCodemap("/nonexistent/path/that/does/not/exist")
if err == nil {
t.Error("Should fail for nonexistent path")
}
}
func TestDiffModeInGitRepo(t *testing.T) {
// This should either work or say "No files changed"
output, err := runCodemap("--diff", ".")
// Even if there's an error, we expect some output
if err != nil && output == "" {
// Check if it's a "no changes" scenario which is fine
stderrOutput, _ := runCodemap("--diff", ".")
if !strings.Contains(stderrOutput, "No files changed") {
t.Logf("Diff mode output: %s", output)
}
}
}
func TestSkylineFlag(t *testing.T) {
// Create a temp dir with some files to test skyline mode
tmpDir := t.TempDir()
for _, name := range []string{"a.go", "b.go", "c.py"} {
f, _ := os.Create(filepath.Join(tmpDir, name))
f.WriteString("content\n")
f.Close()
}
output, err := runCodemap("--skyline", tmpDir)
if err != nil {
t.Fatalf("Skyline mode failed: %v", err)
}
// Skyline mode should produce some output (could be minimal for small projects)
if output == "" {
t.Error("Skyline mode should produce output")
}
}
func TestDepsModeFallback(t *testing.T) {
// Without grammars installed, --deps should give a helpful error message
output, err := runCodemap("--deps", ".")
// This might succeed if grammars are installed, or fail with a message
if err != nil {
// Should have a helpful error message about grammars
if !strings.Contains(output, "grammar") && !strings.Contains(output, "Grammar") {
// It's okay if it just fails differently in CI
t.Logf("Deps mode output: %s", output)
}
}
}
func TestJSONDepsOutput(t *testing.T) {
// Test JSON output for deps mode (if grammars are available)
output, err := runCodemap("--deps", "--json", ".")
if err != nil {
// Expected if no grammars
return
}
// Should be valid JSON
var depsProject scanner.DepsProject
if err := json.Unmarshal([]byte(output), &depsProject); err != nil {
t.Errorf("Deps JSON should be valid: %v", err)
}
if depsProject.Mode != "deps" {
t.Errorf("Expected mode 'deps', got %q", depsProject.Mode)
}
}
func TestEmptyDirectory(t *testing.T) {
tmpDir := t.TempDir()
output, err := runCodemap(tmpDir)
if err != nil {
// Empty directory might be handled differently
t.Logf("Empty dir output: %s", output)
}
// Main thing is it shouldn't panic
}
func TestDebugFlag(t *testing.T) {
output, err := runCodemap("--debug", ".")
// Debug should produce output on stderr about paths and gitignore
// We can't easily capture stderr here, but at least check it doesn't fail
if err != nil {
t.Logf("Debug mode produced error: %v", err)
}
// Should still produce tree output on stdout
if output == "" {
t.Error("Debug mode should still produce output")
}
}
func TestMultipleFlags(t *testing.T) {
// Test combining flags
output, err := runCodemap("--json", ".")
if err != nil {
t.Fatalf("Multiple flags failed: %v", err)
}
if !strings.HasPrefix(strings.TrimSpace(output), "{") {
t.Error("JSON flag should produce JSON output starting with {")
}
}
func TestRelativePath(t *testing.T) {
// Test with relative path
output, err := runCodemap("./scanner")
if err != nil {
t.Fatalf("Relative path failed: %v", err)
}
if output == "" {
t.Error("Should produce output for relative path")
}
}
func TestCurrentDirectory(t *testing.T) {
// Test default (current directory)
output1, err1 := runCodemap()
output2, err2 := runCodemap(".")
if err1 != nil {
t.Fatalf("No arg failed: %v", err1)
}
if err2 != nil {
t.Fatalf("Dot arg failed: %v", err2)
}
// Both should produce similar output
// (Not checking exact equality as timing might differ)
if (output1 == "") != (output2 == "") {
t.Error("No arg and '.' should produce similar results")
}
}