|
| 1 | +package zstash |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCleanPath(t *testing.T) { |
| 15 | + t.Run("removes directory and contents", func(t *testing.T) { |
| 16 | + dir := t.TempDir() |
| 17 | + testDir := filepath.Join(dir, "cache") |
| 18 | + require.NoError(t, os.MkdirAll(filepath.Join(testDir, "subdir"), 0o755)) |
| 19 | + require.NoError(t, os.WriteFile(filepath.Join(testDir, "file.txt"), []byte("test"), 0o600)) |
| 20 | + require.NoError(t, os.WriteFile(filepath.Join(testDir, "subdir", "nested.txt"), []byte("nested"), 0o600)) |
| 21 | + |
| 22 | + err := cleanPath(context.Background(), testDir) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + _, err = os.Stat(testDir) |
| 26 | + assert.True(t, os.IsNotExist(err), "directory should be removed") |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("handles read-only directories (like go module cache)", func(t *testing.T) { |
| 30 | + dir := t.TempDir() |
| 31 | + testDir := filepath.Join(dir, "modcache") |
| 32 | + subdir := filepath.Join(testDir, "pkg") |
| 33 | + require.NoError(t, os.MkdirAll(subdir, 0o755)) |
| 34 | + require.NoError(t, os.WriteFile(filepath.Join(subdir, "mod.go"), []byte("package mod"), 0o400)) |
| 35 | + require.NoError(t, os.Chmod(subdir, 0o555)) |
| 36 | + require.NoError(t, os.Chmod(testDir, 0o555)) |
| 37 | + |
| 38 | + err := cleanPath(context.Background(), testDir) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + _, err = os.Stat(testDir) |
| 42 | + assert.True(t, os.IsNotExist(err), "directory should be removed") |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("succeeds on non-existent path", func(t *testing.T) { |
| 46 | + err := cleanPath(context.Background(), "/nonexistent/path/that/does/not/exist") |
| 47 | + require.NoError(t, err) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("rejects empty path", func(t *testing.T) { |
| 51 | + err := cleanPath(context.Background(), "") |
| 52 | + require.Error(t, err) |
| 53 | + assert.Contains(t, err.Error(), "empty directory path") |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("rejects root path", func(t *testing.T) { |
| 57 | + err := cleanPath(context.Background(), "/") |
| 58 | + require.Error(t, err) |
| 59 | + assert.Contains(t, err.Error(), "refusing to remove") |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("rejects current directory", func(t *testing.T) { |
| 63 | + err := cleanPath(context.Background(), ".") |
| 64 | + require.Error(t, err) |
| 65 | + assert.Contains(t, err.Error(), "refusing to remove") |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("rejects home directory", func(t *testing.T) { |
| 69 | + home, err := os.UserHomeDir() |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + err = cleanPath(context.Background(), home) |
| 73 | + require.Error(t, err) |
| 74 | + assert.Contains(t, err.Error(), "refusing to remove home directory") |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("respects context cancellation", func(t *testing.T) { |
| 78 | + dir := t.TempDir() |
| 79 | + testDir := filepath.Join(dir, "cache") |
| 80 | + require.NoError(t, os.MkdirAll(testDir, 0o755)) |
| 81 | + |
| 82 | + ctx, cancel := context.WithCancel(context.Background()) |
| 83 | + cancel() |
| 84 | + |
| 85 | + err := cleanPath(ctx, testDir) |
| 86 | + require.Error(t, err) |
| 87 | + assert.ErrorIs(t, err, context.Canceled) |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func TestCleanPathWindowsDriveRoot(t *testing.T) { |
| 92 | + if runtime.GOOS != "windows" { |
| 93 | + t.Skip("Windows-specific test") |
| 94 | + } |
| 95 | + |
| 96 | + err := cleanPath(context.Background(), "C:\\") |
| 97 | + require.Error(t, err) |
| 98 | + assert.Contains(t, err.Error(), "refusing to remove drive root") |
| 99 | +} |
0 commit comments