-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
148 lines (124 loc) · 4.03 KB
/
main_test.go
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestReadDirIgnore(t *testing.T) {
tmpDir := t.TempDir()
ignoreFilePath := filepath.Join(tmpDir, ".dirignore")
ignoredDirs := []string{"ignored1", "ignored2"}
err := os.WriteFile(ignoreFilePath, []byte(strings.Join(ignoredDirs, "\n")), 0644)
if err != nil {
t.Fatalf("Failed to write .dirignore file: %v", err)
}
ignoredMap, err := readDirIgnore(tmpDir)
if err != nil {
t.Fatalf("Error reading .dirignore: %v", err)
}
for _, dir := range ignoredDirs {
if _, ok := ignoredMap[dir]; !ok {
t.Errorf("Expected directory %s to be ignored, but it was not", dir)
}
}
}
func TestScanDirectory(t *testing.T) {
tmpDir := t.TempDir()
os.Mkdir(filepath.Join(tmpDir, "dir1"), 0755)
os.Mkdir(filepath.Join(tmpDir, "dir2"), 0755)
os.WriteFile(filepath.Join(tmpDir, "file1.txt"), []byte("content"), 0644)
ignoredDirs := map[string]struct{}{
"dir2": {},
}
style := ConnectorStyle{
Intermediate: "├── ",
Last: "└── ",
Prefix: " ",
Branch: "│ ",
}
structure, err := scanDirectory(tmpDir, "", ignoredDirs, style, []string{}, -1, 0)
if err != nil {
t.Fatalf("Error scanning directory: %v", err)
}
expectedStructure := "├── dir1\n└── file1.txt\n"
if structure != expectedStructure {
t.Errorf("Expected structure:\n%s\nGot:\n%s", expectedStructure, structure)
}
}
func TestScanDirectoryWithExclusions(t *testing.T) {
tmpDir := t.TempDir()
os.Mkdir(filepath.Join(tmpDir, "dir1"), 0755)
os.WriteFile(filepath.Join(tmpDir, "file1.txt"), []byte("content"), 0644)
os.WriteFile(filepath.Join(tmpDir, "file2.log"), []byte("content"), 0644)
ignoredDirs := map[string]struct{}{}
style := ConnectorStyle{
Intermediate: "├── ",
Last: "└── ",
Prefix: " ",
Branch: "│ ",
}
excludePatterns := []string{"*.log"}
structure, err := scanDirectory(tmpDir, "", ignoredDirs, style, excludePatterns, -1, 0)
if err != nil {
t.Fatalf("Error scanning directory: %v", err)
}
expectedStructure := "├── dir1\n└── file1.txt\n"
if structure != expectedStructure {
t.Errorf("Expected structure:\n%s\nGot:\n%s", expectedStructure, structure)
}
}
func TestScanDirectoryWithDepthLimit(t *testing.T) {
tmpDir := t.TempDir()
os.Mkdir(filepath.Join(tmpDir, "dir1"), 0755)
os.Mkdir(filepath.Join(tmpDir, "dir1", "subdir1"), 0755)
os.WriteFile(filepath.Join(tmpDir, "file1.txt"), []byte("content"), 0644)
os.WriteFile(filepath.Join(tmpDir, "dir1", "subdir1", "file2.txt"), []byte("content"), 0644)
ignoredDirs := map[string]struct{}{}
style := ConnectorStyle{
Intermediate: "├── ",
Last: "└── ",
Prefix: " ",
Branch: "│ ",
}
structure, err := scanDirectory(tmpDir, "", ignoredDirs, style, []string{}, 1, 0)
if err != nil {
t.Fatalf("Error scanning directory: %v", err)
}
expectedStructure := "├── dir1\n└── file1.txt\n"
if structure != expectedStructure {
t.Errorf("Expected structure:\n%s\nGot:\n%s", expectedStructure, structure)
}
}
func TestEnsureMdExtension(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"output", "output.md"},
{"output.md", "output.md"},
{"doc.txt", "doc.txt.md"},
}
for _, tt := range tests {
result := ensureMdExtension(tt.input)
if result != tt.expected {
t.Errorf("ensureMdExtension(%s): expected %s, got %s", tt.input, tt.expected, result)
}
}
}
func TestWriteToFile(t *testing.T) {
tmpDir := t.TempDir()
filename := filepath.Join(tmpDir, "testfile.md")
content := "This is a test"
err := writeToFile(filename, content)
if err != nil {
t.Fatalf("Error writing to file: %v", err)
}
readContent, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
if string(readContent) != content {
t.Errorf("Expected content: %s, got: %s", content, string(readContent))
}
}