-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
198 lines (167 loc) · 4.98 KB
/
Copy pathmain_test.go
File metadata and controls
198 lines (167 loc) · 4.98 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
package main
import (
"bytes"
"code2md/c2mConfig"
"os"
"path/filepath"
"strings"
"testing"
)
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
r, w, _ := os.Pipe()
os.Stdout = w
fn()
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
buf.ReadFrom(r)
return buf.String()
}
func TestRun(t *testing.T) {
t.Run("writes to stdout when no output specified", func(t *testing.T) {
tempDir := t.TempDir()
err := os.WriteFile(filepath.Join(tempDir, "main.go"), []byte("package main\n"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
config := &c2mConfig.Config{
InputFolder: tempDir,
OutputMarkdown: "",
AllowedLanguages: map[string]bool{".go": true},
AllowedFileNames: map[string]bool{},
UserIgnorePatterns: []string{},
MaxFileSize: 100 * 1024 * 1024,
}
var runErr error
output := captureStdout(t, func() {
runErr = run(config)
})
if runErr != nil {
t.Errorf("run() error: %v", runErr)
}
if !strings.Contains(output, "main.go") {
t.Error("Stdout should contain main.go")
}
})
t.Run("writes to file when output specified", func(t *testing.T) {
tempDir := t.TempDir()
err := os.WriteFile(filepath.Join(tempDir, "main.go"), []byte("package main\n"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
outputFile := filepath.Join(tempDir, "output.md")
config := &c2mConfig.Config{
InputFolder: tempDir,
OutputMarkdown: outputFile,
AllowedLanguages: map[string]bool{".go": true},
AllowedFileNames: map[string]bool{},
UserIgnorePatterns: []string{},
MaxFileSize: 100 * 1024 * 1024,
}
err = run(config)
if err != nil {
t.Errorf("run() error: %v", err)
}
content, _ := os.ReadFile(outputFile)
if !strings.Contains(string(content), "main.go") {
t.Error("Output file should contain main.go")
}
})
t.Run("never reads its own output file even when spelled ./out.md instead of out.md", func(t *testing.T) {
tempDir := t.TempDir()
err := os.WriteFile(filepath.Join(tempDir, "main.go"), []byte("package main\n"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
origDir, _ := os.Getwd()
if err := os.Chdir(tempDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
defer os.Chdir(origDir)
config := &c2mConfig.Config{
InputFolder: ".",
OutputMarkdown: "./out.md",
AllowedLanguages: map[string]bool{".go": true, ".md": true},
AllowedFileNames: map[string]bool{},
MaxFileSize: 100 * 1024 * 1024,
}
for i := 0; i < 2; i++ {
if err := run(config); err != nil {
t.Fatalf("run() error on pass %d: %v", i+1, err)
}
}
content, _ := os.ReadFile(filepath.Join(tempDir, "out.md"))
if !strings.Contains(string(content), "main.go") {
t.Error("Output file should contain main.go")
}
if strings.Contains(string(content), "out.md") {
t.Error("Output file should never contain itself")
}
})
t.Run("creates output directory if needed", func(t *testing.T) {
tempDir := t.TempDir()
err := os.WriteFile(filepath.Join(tempDir, "main.go"), []byte("package main\n"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
outputFile := filepath.Join(tempDir, "subdir", "nested", "output.md")
config := &c2mConfig.Config{
InputFolder: tempDir,
OutputMarkdown: outputFile,
AllowedLanguages: map[string]bool{".go": true},
AllowedFileNames: map[string]bool{},
UserIgnorePatterns: []string{},
MaxFileSize: 100 * 1024 * 1024,
}
err = run(config)
if err != nil {
t.Errorf("run() error: %v", err)
}
if _, err := os.Stat(outputFile); os.IsNotExist(err) {
t.Error("Output file should be created in nested directory")
}
})
}
func TestDisplayVersion(t *testing.T) {
output := captureStdout(t, func() {
displayVersion()
})
if !strings.Contains(output, "code2md") {
t.Error("Output should contain 'code2md'")
}
}
func TestDisplayUsageInstructions(t *testing.T) {
t.Run("with nil config", func(t *testing.T) {
output := captureStdout(t, func() {
displayUsageInstructions(nil, false)
})
if !strings.Contains(output, "Usage:") {
t.Error("Output should contain usage instructions")
}
if strings.Contains(output, "By default") {
t.Error("Should not show language info with nil config")
}
})
t.Run("with valid config shows languages", func(t *testing.T) {
config := &c2mConfig.Config{
AllowedLanguages: map[string]bool{".go": true, ".js": false},
}
output := captureStdout(t, func() {
displayUsageInstructions(config, false)
})
if !strings.Contains(output, "By default") {
t.Error("Output should show language info with valid config")
}
})
t.Run("shows error when requested", func(t *testing.T) {
output := captureStdout(t, func() {
displayUsageInstructions(nil, true)
})
if !strings.Contains(output, "Error:") {
t.Error("Output should contain error message")
}
})
}