-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiltration_test.go
More file actions
118 lines (109 loc) · 2.78 KB
/
filtration_test.go
File metadata and controls
118 lines (109 loc) · 2.78 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
)
func TestProcessFiles(t *testing.T) {
// 创建一个临时目录作为测试环境
tempDir, err := ioutil.TempDir("", "test_dir")
if err != nil {
t.Fatalf("无法创建临时目录: %v", err)
}
defer os.RemoveAll(tempDir)
// 在临时目录中创建测试文件结构
files := []string{
"file1.txt",
"file2.go",
"file3.tmp",
"subdir/file4.go",
"subdir/file5.txt",
".hidden/file6.go",
}
for _, file := range files {
path := filepath.Join(tempDir, file)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
t.Fatalf("无法创建目录: %v", err)
}
err = ioutil.WriteFile(path, []byte("test content"), 0644)
if err != nil {
t.Fatalf("无法创建文件: %v", err)
}
}
tests := []struct {
name string
args AppArgs
expectedFiles []string
expectedErrNil bool
}{
{
name: "包含所有文件",
args: AppArgs{
Files: []string{tempDir},
Reg: []string{},
IReg: []string{},
IncludeHidden: true,
},
expectedFiles: []string{"file1.txt", "file2.go", "file3.tmp", "subdir/file4.go", "subdir/file5.txt", ".hidden/file6.go"},
expectedErrNil: true,
},
{
name: "只包含 .go 文件",
args: AppArgs{
Files: []string{tempDir},
Reg: []string{".go"},
IReg: []string{},
IncludeHidden: false,
},
expectedFiles: []string{"file2.go", "subdir/file4.go"},
expectedErrNil: true,
},
{
name: "排除 .tmp 文件",
args: AppArgs{
Files: []string{tempDir},
Reg: []string{},
IReg: []string{".tmp"},
IncludeHidden: false,
},
expectedFiles: []string{"file1.txt", "file2.go", "subdir/file4.go", "subdir/file5.txt"},
expectedErrNil: true,
},
{
name: "包含隐藏文件",
args: AppArgs{
Files: []string{tempDir},
Reg: []string{".go"},
IReg: []string{},
IncludeHidden: true,
},
expectedFiles: []string{"file2.go", "subdir/file4.go", ".hidden/file6.go"},
expectedErrNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := filtrationFiles(tt.args)
// 检查错误
if (err == nil) != tt.expectedErrNil {
t.Errorf("processFiles() error = %v, expectedErrNil %v", err, tt.expectedErrNil)
return
}
// 对结果进行排序,以确保比较的一致性
sort.Strings(result)
expected := make([]string, len(tt.expectedFiles))
for i, file := range tt.expectedFiles {
expected[i] = filepath.Join(tempDir, file)
}
sort.Strings(expected)
// 比较结果
if !reflect.DeepEqual(result, expected) {
t.Errorf("processFiles() = %v, want %v", result, expected)
}
})
}
}