|
| 1 | +package ops |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/programmersd21/zap/internal/errs" |
| 9 | + "github.com/programmersd21/zap/internal/walk" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCopyFileCount(t *testing.T) { |
| 13 | + srcDir := t.TempDir() |
| 14 | + dstDir := filepath.Join(t.TempDir(), "dst") |
| 15 | + |
| 16 | + // create a tree resembling the reported structure: |
| 17 | + // src/file1.txt, src/sub/file2.txt, src/sub/deep/file3.txt |
| 18 | + paths := []string{ |
| 19 | + filepath.Join(srcDir, "file1.txt"), |
| 20 | + filepath.Join(srcDir, "sub", "file2.txt"), |
| 21 | + filepath.Join(srcDir, "sub", "deep", "file3.txt"), |
| 22 | + } |
| 23 | + for _, p := range paths { |
| 24 | + if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + if err := os.WriteFile(p, []byte("data"), 0644); err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // verify pre-scan count matches |
| 33 | + stats, err := walk.ComputeStats([]string{srcDir}) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + if stats.TotalFiles != 3 { |
| 38 | + t.Errorf("ComputeStats: expected 3 files, got %d", stats.TotalFiles) |
| 39 | + } |
| 40 | + |
| 41 | + // verify copy operation count matches |
| 42 | + var cumulFiles int64 |
| 43 | + var cumulBytes int64 |
| 44 | + ec := errs.NewCollector() |
| 45 | + opts := CopyOptions{ |
| 46 | + Force: false, |
| 47 | + Errors: ec, |
| 48 | + CumulBytes: &cumulBytes, |
| 49 | + CumulFiles: &cumulFiles, |
| 50 | + } |
| 51 | + if err := Copy(srcDir, dstDir, opts); err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if cumulFiles != 3 { |
| 55 | + t.Errorf("CumulFiles after copy: expected 3, got %d", cumulFiles) |
| 56 | + } |
| 57 | + if cumulFiles != stats.TotalFiles { |
| 58 | + t.Errorf("count mismatch: pre-scan=%d, actual=%d", stats.TotalFiles, cumulFiles) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestCopyFileCountLargerTree(t *testing.T) { |
| 63 | + srcDir := t.TempDir() |
| 64 | + dstDir := filepath.Join(t.TempDir(), "dst") |
| 65 | + |
| 66 | + // create exactly 10 files in a nested structure |
| 67 | + files := []string{ |
| 68 | + "build/please", |
| 69 | + "justfile", |
| 70 | + "src/cli.odin", |
| 71 | + "src/main.odin", |
| 72 | + "src/pam/context.odin", |
| 73 | + "src/pam/ffi.odin", |
| 74 | + "src/pam/pam.odin", |
| 75 | + "src/ticket.odin", |
| 76 | + "src/timestamp.odin", |
| 77 | + "src/utils.odin", |
| 78 | + } |
| 79 | + for _, f := range files { |
| 80 | + p := filepath.Join(srcDir, f) |
| 81 | + if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + if err := os.WriteFile(p, []byte("content"), 0644); err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + stats, err := walk.ComputeStats([]string{srcDir}) |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + if stats.TotalFiles != 10 { |
| 94 | + t.Errorf("ComputeStats: expected 10 files, got %d", stats.TotalFiles) |
| 95 | + } |
| 96 | + |
| 97 | + var cumulFiles int64 |
| 98 | + var cumulBytes int64 |
| 99 | + ec := errs.NewCollector() |
| 100 | + opts := CopyOptions{ |
| 101 | + Force: false, |
| 102 | + Errors: ec, |
| 103 | + CumulBytes: &cumulBytes, |
| 104 | + CumulFiles: &cumulFiles, |
| 105 | + } |
| 106 | + if err := Copy(srcDir, dstDir, opts); err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + if cumulFiles != 10 { |
| 110 | + t.Errorf("CumulFiles: expected 10, got %d", cumulFiles) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestDeleteFileCount(t *testing.T) { |
| 115 | + dir := t.TempDir() |
| 116 | + |
| 117 | + // create dir with 1 file (the exact reported bug scenario) |
| 118 | + subdir := filepath.Join(dir, "something") |
| 119 | + if err := os.MkdirAll(subdir, 0755); err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + if err := os.WriteFile(filepath.Join(subdir, "meow"), []byte("x"), 0644); err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + stats, err := walk.ComputeStats([]string{subdir}) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + if stats.TotalFiles != 1 { |
| 131 | + t.Errorf("ComputeStats: expected 1 file, got %d", stats.TotalFiles) |
| 132 | + } |
| 133 | + |
| 134 | + // verify delete counts the file |
| 135 | + var cumulFiles int64 |
| 136 | + ec := errs.NewCollector() |
| 137 | + opts := DeleteOptions{ |
| 138 | + Recursive: true, |
| 139 | + Errors: ec, |
| 140 | + CumulFiles: &cumulFiles, |
| 141 | + } |
| 142 | + if err := Delete(subdir, opts); err != nil { |
| 143 | + t.Fatal(err) |
| 144 | + } |
| 145 | + // should count the file + subdir + root dir = 3 entries |
| 146 | + // but the summary shows filesDone which is cumulFiles |
| 147 | + // sendDeleteProgress increments for files, dirs, and root |
| 148 | + if cumulFiles < 1 { |
| 149 | + t.Errorf("CumulFiles after delete: expected >= 1, got %d", cumulFiles) |
| 150 | + } |
| 151 | +} |
0 commit comments