-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
120 lines (107 loc) · 2.78 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func test(t *testing.T) {
// Create a temporary directory for testing
testDir, err := os.MkdirTemp("", "fidy_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(testDir) // Clean up after the test
// test files with different extensions
testFiles := []string{
"file1.txt",
"file2.txt",
"file3.jpg",
"file4.pdf",
}
for _, fileName := range testFiles {
_, err := os.Create(filepath.Join(testDir, fileName))
if err != nil {
t.Fatalf("Failed to create test file %s: %v", fileName, err)
}
}
// Call the function to organize files
organizeFiles(testDir, []string{}, []string{}, false, false)
// Check if the files were moved to the correct directories
expectedDirs := []string{"txt", "jpg", "pdf"}
for _, dir := range expectedDirs {
dirPath := filepath.Join(testDir, dir)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
t.Errorf("Expected directory %s does not exist", dirPath)
}
}
// Check if the files are in the correct directories
expectedFiles := map[string]string{
"file1.txt": "txt",
"file2.txt": "txt",
"file3.jpg": "jpg",
"file4.pdf": "pdf",
}
for fileName, dir := range expectedFiles {
filePath := filepath.Join(testDir, dir, fileName)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Errorf("Expected file %s does not exist in directory %s", fileName, dir)
}
}
}
// function to replicate fidy's logic
func organizeFiles(dir string, excludeExtensions []string, includeExtensions []string, verbose bool, dryRun bool) {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
createdDirs := make(map[string]bool)
for _, file := range files {
if !file.IsDir() {
ext := filepath.Ext(file.Name())
if ext != "" {
ext = ext[1:] // Remove the leading dot
// Check if the extension is in the exclude list
exclude := false
for _, excludeExt := range excludeExtensions {
if ext == excludeExt {
exclude = true
break
}
}
if exclude {
continue
}
inc := false
for _, includeExt := range includeExtensions {
if ext == includeExt {
inc = true
break
}
}
if !inc {
continue
}
targetDir := filepath.Join(dir, ext)
if _, exists := createdDirs[targetDir]; !exists {
if verbose || dryRun {
fmt.Printf("Creating directory: %s\n", targetDir)
}
if !dryRun {
os.Mkdir(targetDir, os.ModePerm)
}
createdDirs[targetDir] = true
}
oldPath := filepath.Join(dir, file.Name())
newPath := filepath.Join(targetDir, file.Name())
if verbose || dryRun {
fmt.Printf("Moving file: %s -> %s\n", oldPath, newPath)
}
if !dryRun {
os.Rename(oldPath, newPath)
}
}
}
}
}