|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func test(t *testing.T) { |
| 12 | + // Create a temporary directory for testing |
| 13 | + testDir, err := os.MkdirTemp("", "fidy_test") |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 16 | + } |
| 17 | + defer os.RemoveAll(testDir) // Clean up after the test |
| 18 | + |
| 19 | + // test files with different extensions |
| 20 | + testFiles := []string{ |
| 21 | + "file1.txt", |
| 22 | + "file2.txt", |
| 23 | + "file3.jpg", |
| 24 | + "file4.pdf", |
| 25 | + } |
| 26 | + for _, fileName := range testFiles { |
| 27 | + _, err := os.Create(filepath.Join(testDir, fileName)) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to create test file %s: %v", fileName, err) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Call the function to organize files |
| 34 | + organizeFiles(testDir, []string{}, []string{}, false, false) |
| 35 | + |
| 36 | + // Check if the files were moved to the correct directories |
| 37 | + expectedDirs := []string{"txt", "jpg", "pdf"} |
| 38 | + for _, dir := range expectedDirs { |
| 39 | + dirPath := filepath.Join(testDir, dir) |
| 40 | + if _, err := os.Stat(dirPath); os.IsNotExist(err) { |
| 41 | + t.Errorf("Expected directory %s does not exist", dirPath) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Check if the files are in the correct directories |
| 46 | + expectedFiles := map[string]string{ |
| 47 | + "file1.txt": "txt", |
| 48 | + "file2.txt": "txt", |
| 49 | + "file3.jpg": "jpg", |
| 50 | + "file4.pdf": "pdf", |
| 51 | + } |
| 52 | + for fileName, dir := range expectedFiles { |
| 53 | + filePath := filepath.Join(testDir, dir, fileName) |
| 54 | + if _, err := os.Stat(filePath); os.IsNotExist(err) { |
| 55 | + t.Errorf("Expected file %s does not exist in directory %s", fileName, dir) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// function to replicate fidy's logic |
| 61 | +func organizeFiles(dir string, excludeExtensions []string, includeExtensions []string, verbose bool, dryRun bool) { |
| 62 | + files, err := ioutil.ReadDir(dir) |
| 63 | + if err != nil { |
| 64 | + panic(err) |
| 65 | + } |
| 66 | + |
| 67 | + createdDirs := make(map[string]bool) |
| 68 | + |
| 69 | + for _, file := range files { |
| 70 | + if !file.IsDir() { |
| 71 | + ext := filepath.Ext(file.Name()) |
| 72 | + if ext != "" { |
| 73 | + ext = ext[1:] // Remove the leading dot |
| 74 | + |
| 75 | + // Check if the extension is in the exclude list |
| 76 | + exclude := false |
| 77 | + for _, excludeExt := range excludeExtensions { |
| 78 | + if ext == excludeExt { |
| 79 | + exclude = true |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + if exclude { |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + inc := false |
| 88 | + for _, includeExt := range includeExtensions { |
| 89 | + if ext == includeExt { |
| 90 | + inc = true |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + if !inc { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + targetDir := filepath.Join(dir, ext) |
| 99 | + if _, exists := createdDirs[targetDir]; !exists { |
| 100 | + if verbose || dryRun { |
| 101 | + fmt.Printf("Creating directory: %s\n", targetDir) |
| 102 | + } |
| 103 | + if !dryRun { |
| 104 | + os.Mkdir(targetDir, os.ModePerm) |
| 105 | + } |
| 106 | + createdDirs[targetDir] = true |
| 107 | + } |
| 108 | + |
| 109 | + oldPath := filepath.Join(dir, file.Name()) |
| 110 | + newPath := filepath.Join(targetDir, file.Name()) |
| 111 | + if verbose || dryRun { |
| 112 | + fmt.Printf("Moving file: %s -> %s\n", oldPath, newPath) |
| 113 | + } |
| 114 | + if !dryRun { |
| 115 | + os.Rename(oldPath, newPath) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments