-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_test.go
331 lines (312 loc) · 7.98 KB
/
dir_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
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
package wordcounter_test
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
wcg "github.com/100gle/wordcounter"
"github.com/jedib0t/go-pretty/v6/table"
)
func TestDirCounter_Count(t *testing.T) {
type args struct {
dirname string
}
ignoreList := []string{".git", ".idea"}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Count files in directory",
args: args{dirname: "./testdata"},
},
{
name: "Empty directory",
args: args{dirname: ""},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dc := wcg.NewDirCounter(tt.args.dirname, ignoreList...)
if err := dc.Count(); (err != nil) != tt.wantErr {
t.Errorf("DirCounter.Count() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr == false && len(dc.GetRows()) == 0 {
t.Errorf("DirCounter.Count() did not count any files")
}
})
}
}
func TestFileCounter_IsIgnored(t *testing.T) {
dirname := "testdata"
dc := wcg.NewDirCounter(dirname, "*.txt", "otherfile.txt", ".*")
// Test checking if a file should be ignored based on an exact match ignore pattern
result := dc.IsIgnored("otherfile.txt")
if !result {
t.Error("FileCounter.isIgnored() failed, expected true for exact match ignore pattern")
}
// Test checking if a file should be ignored based on a wildcard ignore pattern
result = dc.IsIgnored("example.txt")
if !result {
t.Error("FileCounter.isIgnored() failed, expected true for wildcard ignore pattern")
}
// Test checking if a file should not be ignored
result = dc.IsIgnored("testfile.csv")
if result {
t.Error("FileCounter.isIgnored() failed, expected false for non-ignored file")
}
// Test checking if a file should not be ignored
result = dc.IsIgnored(".git")
if !result {
t.Error("FileCounter.isIgnored() failed, expected false for ignored file")
}
// Test glob-like ignores with test table
tests := []struct {
name string
patterns []string
path string
want bool
}{
{
name: "match one pattern",
patterns: []string{"*.go"},
path: "main.go",
want: true,
},
{
name: "match multiple patterns",
patterns: []string{"*.md", "*.txt"},
path: "README.md",
want: true,
},
{
name: "not match pattern",
patterns: []string{"*.md"},
path: "main.go",
want: false,
},
{
name: "match single suffix pattern",
patterns: []string{"*.js", "**/*.js"},
path: "foo.js",
want: true,
},
{
name: "match multiple suffix pattern",
patterns: []string{"*.js.map", "**/*.js.map"},
path: "foo.js.map",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dc = wcg.NewDirCounter(dirname, tt.patterns...)
got := dc.IsIgnored(tt.path)
if got != tt.want {
t.Errorf("FileCounter.isIgnored(%v) = %v, want %v", tt.path, got, tt.want)
}
})
}
}
func TestDirCounter_Ignore(t *testing.T) {
type fields struct {
ignoreList []string
}
type args struct {
pattern string
}
tests := []struct {
name string
fields fields
args args
want []string
}{
{
name: "Ignore single pattern",
fields: fields{
ignoreList: []string{},
},
args: args{pattern: ".git"},
want: []string{".git"},
},
{
name: "Ignore multiple patterns",
fields: fields{
ignoreList: []string{".git", ".idea"},
},
args: args{pattern: "node_modules"},
want: []string{".git", ".idea", "node_modules"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dc := wcg.NewDirCounter(".", tt.fields.ignoreList...)
dc.Ignore(tt.args.pattern)
if !reflect.DeepEqual(dc.IgnoreList, tt.want) {
t.Errorf("DirCounter.Ignore() got = %v, want %v", dc.IgnoreList, tt.want)
}
})
}
}
func TestDirCounter_GetHeaderAndRows(t *testing.T) {
testDir := filepath.Join(wd, "testdata")
tests := []struct {
name string
dc *wcg.DirCounter
want []wcg.Row
}{
{
name: "GetHeaderAndRows",
dc: wcg.NewDirCounter(testDir),
want: []wcg.Row{
{"File", "Lines", "ChineseChars", "NonChineseChars", "TotalChars"},
{filepath.Join(testDir, "foo.md"), 1, 12, 1, 13},
{filepath.Join(testDir, "test.md"), 1, 4, 1, 5},
{filepath.Join(testDir, "test.txt"), 1, 4, 15, 19},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dc.Count()
if got := tt.dc.GetHeaderAndRows(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("DirCounter.GetHeaderAndRows() = \n%v, want \n%v", got, tt.want)
}
})
}
}
func TestDirCounter_ExportCSV(t *testing.T) {
testDir := filepath.Join(wd, "testdata")
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,1,12,1,13\n%s,1,4,1,5\n%s,1,4,15,19",
filepath.Join(testDir, "foo.md"),
filepath.Join(testDir, "test.md"),
filepath.Join(testDir, "test.txt"),
)
tests := []struct {
name string
dc *wcg.DirCounter
want string
}{
{
name: "ExportCSV",
dc: wcg.NewDirCounter(testDir),
want: expectedCSV,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dc.Count()
got, err := tt.dc.ExportCSV()
if err != nil {
t.Errorf("DirCounter.ExportCSV() error = %v", err)
}
if got != tt.want {
t.Errorf("DirCounter.ExportCSV() = %v, want %v", got, tt.want)
}
})
}
}
func TestDirCounter_ExportCSVWithFileName(t *testing.T) {
testDir := filepath.Join(wd, "testdata")
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,1,12,1,13\n%s,1,4,1,5\n%s,1,4,15,19",
filepath.Join(testDir, "foo.md"),
filepath.Join(testDir, "test.md"),
filepath.Join(testDir, "test.txt"),
)
tests := []struct {
name string
dc *wcg.DirCounter
want string
}{
{
name: "ExportCSVWithFileName",
dc: wcg.NewDirCounter(testDir),
want: expectedCSV,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dc.Count()
got, err := tt.dc.ExportCSV("test.csv")
if err != nil {
t.Errorf("DirCounter.ExportCSV() error = %v", err)
}
if _, err := os.Stat("test.csv"); err != nil {
t.Errorf("DirCounter.ExportCSV() error = %v", err)
}
if got != tt.want {
t.Errorf("DirCounter.ExportCSV() = %v, want %v", got, tt.want)
}
err = os.Remove("test.csv")
if err != nil {
t.Errorf("DirCounter.ExportCSV() error = %v", err)
}
})
}
}
func TestDirCounter_ExportTable(t *testing.T) {
testDir := filepath.Join(wd, "testdata")
expectedTbl := table.NewWriter()
expectedTbl.AppendHeader(wcg.Row{"File", "Lines", "ChineseChars", "NonChineseChars", "TotalChars"})
rows := []table.Row{
{filepath.Join(testDir, "foo.md"), 1, 12, 1, 13},
{filepath.Join(testDir, "test.md"), 1, 4, 1, 5},
{filepath.Join(testDir, "test.txt"), 1, 4, 15, 19},
}
expectedTbl.AppendRows(rows)
tests := []struct {
name string
dc *wcg.DirCounter
want string
}{
{
name: "ExportTable",
dc: wcg.NewDirCounter(testDir),
want: expectedTbl.Render(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dc.Count()
if got := tt.dc.ExportTable(); got != tt.want {
t.Errorf("DirCounter.ExportTable() = %v, want %v", got, tt.want)
}
})
}
}
func TestDirCounter_ExportExcel(t *testing.T) {
fc := wcg.NewFileCounter("testdata")
fc.Count()
// Export the word count data to an Excel file for a FileCounter instance and check for errors
if err := fc.ExportExcel("testdata/test.xlsx"); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// remove test.xlsx after testing
if err := os.Remove("testdata/test.xlsx"); err != nil {
t.Fatalf("Unexpected error while removing test.xlsx: %v", err)
}
}
func TestDirCounter_EnableTotal(t *testing.T) {
tests := []struct {
name string
dc *wcg.DirCounter
want bool
}{
{
name: "EnableTotal",
dc: wcg.NewDirCounter("testdata"),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dc.EnableTotal()
if tt.dc.WithTotal != tt.want {
t.Errorf("DirCounter.EnableTotal() = %v, want %v", tt.dc.WithTotal, tt.want)
}
})
}
}