-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_test.go
More file actions
205 lines (174 loc) · 4.95 KB
/
scanner_test.go
File metadata and controls
205 lines (174 loc) · 4.95 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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestScanner(t *testing.T) {
// Create a temporary directory for testing
tmpDir := t.TempDir()
// Create test files
testFiles := []struct {
name string
content string
category string
}{
{"image.jpg", "fake image data", "Images"},
{"document.pdf", "fake pdf data", "Documents"},
{"video.mp4", "fake video data", "Videos"},
{"music.mp3", "fake music data", "Music"},
{"archive.zip", "fake zip data", "Archives"},
{"app.exe", "fake exe data", "Applications"},
{"disk.iso", "fake iso data", "Disk Images"},
{"unknown.xyz", "fake data", "Other"},
}
// Create the test files
for _, tf := range testFiles {
filePath := filepath.Join(tmpDir, tf.name)
err := os.WriteFile(filePath, []byte(tf.content), 0644)
if err != nil {
t.Fatalf("Failed to create test file %s: %v", tf.name, err)
}
}
// Create a scanner and scan the directory
scanner := NewScanner()
err := scanner.ScanDirectory(tmpDir)
if err != nil {
t.Fatalf("ScanDirectory() error = %v", err)
}
// Test that all files were found
if len(scanner.Files) != len(testFiles) {
t.Errorf("Expected %d files, got %d", len(testFiles), len(scanner.Files))
}
// Test categorization
for _, tf := range testFiles {
found := false
for _, file := range scanner.Files {
if file.Name == tf.name {
if file.Category != tf.category {
t.Errorf("File %s: expected category '%s', got '%s'", tf.name, tf.category, file.Category)
}
found = true
break
}
}
if !found {
t.Errorf("File %s not found in scan results", tf.name)
}
}
// Test categories map
for category, files := range scanner.Categories {
if len(files) == 0 {
t.Errorf("Category '%s' has no files", category)
}
}
}
func TestDetermineCategory(t *testing.T) {
scanner := NewScanner()
tests := []struct {
name string
ext string
expected string
}{
{"image.jpg", ".jpg", "Images"},
{"document.pdf", ".pdf", "Documents"},
{"video.mp4", ".mp4", "Videos"},
{"music.mp3", ".mp3", "Music"},
{"archive.zip", ".zip", "Archives"},
{"app.exe", ".exe", "Applications"},
{"disk.iso", ".iso", "Disk Images"},
{"unknown.xyz", ".xyz", "Other"},
{"installer.exe", ".exe", "Applications"},
{"setup.exe", ".exe", "Applications"},
{"manual.pdf", ".pdf", "Documents"},
{"guide.txt", ".txt", "Documents"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := scanner.determineCategory(tt.ext, tt.name)
if result != tt.expected {
t.Errorf("determineCategory(%s, %s) = %s, want %s", tt.ext, tt.name, result, tt.expected)
}
})
}
}
func TestCalculateFileHash(t *testing.T) {
scanner := NewScanner()
// Create a test file
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test.txt")
testContent := "Hello, World!"
err := os.WriteFile(testFile, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Calculate hash
hash, err := scanner.calculateFileHash(testFile)
if err != nil {
t.Fatalf("calculateFileHash() error = %v", err)
}
if hash == "" {
t.Error("calculateFileHash() returned empty hash")
}
// Test that same content produces same hash
hash2, err := scanner.calculateFileHash(testFile)
if err != nil {
t.Fatalf("calculateFileHash() error on second call: %v", err)
}
if hash != hash2 {
t.Errorf("Hashes don't match: %s != %s", hash, hash2)
}
}
func TestFindDuplicates(t *testing.T) {
scanner := NewScanner()
// Create test files with same content (should have same hash)
tmpDir := t.TempDir()
testContent := "duplicate content"
files := []string{"file1.txt", "file2.txt", "file3.txt"}
for _, filename := range files {
filePath := filepath.Join(tmpDir, filename)
err := os.WriteFile(filePath, []byte(testContent), 0644)
if err != nil {
t.Fatalf("Failed to create test file %s: %v", filename, err)
}
}
// Scan directory
err := scanner.ScanDirectory(tmpDir)
if err != nil {
t.Fatalf("ScanDirectory() error = %v", err)
}
// Check that duplicates were found
if len(scanner.Duplicates) == 0 {
t.Error("Expected duplicates to be found, but none were detected")
}
// Check that files are marked as duplicates
duplicateCount := 0
for _, file := range scanner.Files {
if file.IsDuplicate {
duplicateCount++
}
}
if duplicateCount == 0 {
t.Error("Expected files to be marked as duplicates")
}
}
func TestCheckFilePermissions(t *testing.T) {
scanner := NewScanner()
// Create a test file
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test.txt")
err := os.WriteFile(testFile, []byte("test content"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test that we can read the file
err = scanner.checkFilePermissions(testFile)
if err != nil {
t.Errorf("checkFilePermissions() error = %v", err)
}
// Test with non-existent file
err = scanner.checkFilePermissions("/non/existent/file")
if err == nil {
t.Error("Expected error for non-existent file")
}
}