|
| 1 | +package fsutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCopyDirExcludesWholeDirectoryWhenPatternHasDoubleStar(t *testing.T) { |
| 11 | + src := t.TempDir() |
| 12 | + dst := t.TempDir() |
| 13 | + |
| 14 | + excludedDir := filepath.Join(src, "var", "lib", "postgresql", "data") |
| 15 | + if err := os.MkdirAll(excludedDir, 0o755); err != nil { |
| 16 | + t.Fatalf("failed to create excluded dir: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + excludedFile := filepath.Join(excludedDir, "file.txt") |
| 20 | + if err := os.WriteFile(excludedFile, []byte("x"), 0o644); err != nil { |
| 21 | + t.Fatalf("failed to create excluded file: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + keepFile := filepath.Join(src, "keep.txt") |
| 25 | + if err := os.WriteFile(keepFile, []byte("y"), 0o644); err != nil { |
| 26 | + t.Fatalf("failed to create keep file: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + pattern := excludedDir + "/**" |
| 30 | + clone := false |
| 31 | + copyRelPath := true |
| 32 | + skipErrors := true |
| 33 | + |
| 34 | + if err, errs := CopyDir(clone, src, dst, copyRelPath, skipErrors, []string{pattern}, nil, nil); err != nil { |
| 35 | + t.Fatalf("CopyDir returned error: %v", err) |
| 36 | + } else if len(errs) > 0 { |
| 37 | + t.Fatalf("CopyDir returned copy errors: %v", errs) |
| 38 | + } |
| 39 | + |
| 40 | + excludedTarget := filepath.Join(dst, "var", "lib", "postgresql", "data") |
| 41 | + if _, err := os.Stat(excludedTarget); err == nil { |
| 42 | + t.Fatalf("expected excluded directory %q to be skipped", excludedTarget) |
| 43 | + } else if !errors.Is(err, os.ErrNotExist) { |
| 44 | + t.Fatalf("unexpected error checking excluded directory: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + if _, err := os.Stat(filepath.Join(dst, "keep.txt")); err != nil { |
| 48 | + t.Fatalf("expected kept file to be copied, got error: %v", err) |
| 49 | + } |
| 50 | +} |
0 commit comments