TestClone tests the Clone function
(t *testing.T)
| 9 | |
| 10 | // TestClone tests the Clone function |
| 11 | func TestClone(t *testing.T) { |
| 12 | // This test requires network access, so we'll only test error cases |
| 13 | // that don't require actual cloning |
| 14 | |
| 15 | t.Run("clone to read-only directory", func(t *testing.T) { |
| 16 | if runtime.GOOS == "windows" { |
| 17 | t.Skip("chmod does not restrict writes on Windows") |
| 18 | } |
| 19 | if os.Getuid() == 0 { |
| 20 | t.Skip("Cannot test read-only directory as root") |
| 21 | } |
| 22 | |
| 23 | // Create a temporary directory |
| 24 | tempDir, err := os.MkdirTemp("", "cheat-clone-test-*") |
| 25 | if err != nil { |
| 26 | t.Fatalf("failed to create temp dir: %v", err) |
| 27 | } |
| 28 | defer os.RemoveAll(tempDir) |
| 29 | |
| 30 | // Create a read-only subdirectory |
| 31 | readOnlyDir := filepath.Join(tempDir, "readonly") |
| 32 | if err := os.Mkdir(readOnlyDir, 0555); err != nil { |
| 33 | t.Fatalf("failed to create read-only dir: %v", err) |
| 34 | } |
| 35 | |
| 36 | // Attempt to clone to read-only directory |
| 37 | targetDir := filepath.Join(readOnlyDir, "cheatsheets") |
| 38 | err = Clone(targetDir) |
| 39 | |
| 40 | // Should fail because we can't write to read-only directory |
| 41 | if err == nil { |
| 42 | t.Error("expected error when cloning to read-only directory, got nil") |
| 43 | } |
| 44 | }) |
| 45 | |
| 46 | t.Run("clone to invalid path", func(t *testing.T) { |
| 47 | // Try to clone to a path with null bytes (invalid on most filesystems) |
| 48 | err := Clone("/tmp/invalid\x00path") |
| 49 | if err == nil { |
| 50 | t.Error("expected error with invalid path, got nil") |
| 51 | } |
| 52 | }) |
| 53 | } |