|
| 1 | +package pathutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func expectedRootPath(subpath string) string { |
| 11 | + switch runtime.GOOS { |
| 12 | + case "windows": |
| 13 | + // for Windows, there's no "root" user, so just return the original |
| 14 | + return "~root/" + subpath |
| 15 | + case "darwin": |
| 16 | + return filepath.Join("/var/root", subpath) |
| 17 | + default: |
| 18 | + return filepath.Join("/root", subpath) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestExpandTilde(t *testing.T) { |
| 23 | + // Get current user's home directory for testing |
| 24 | + home, err := os.UserHomeDir() |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Failed to get home directory: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + input string |
| 32 | + expected string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "no tilde", |
| 36 | + input: "/absolute/path", |
| 37 | + expected: "/absolute/path", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "relative path no tilde", |
| 41 | + input: "relative/path", |
| 42 | + expected: "relative/path", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "just tilde", |
| 46 | + input: "~", |
| 47 | + expected: home, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "tilde with path", |
| 51 | + input: "~/projects/test", |
| 52 | + expected: filepath.Join(home, "projects/test"), |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "tilde with dotfile", |
| 56 | + input: "~/.npmrc", |
| 57 | + expected: filepath.Join(home, ".npmrc"), |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "invalid username", |
| 61 | + input: "~nonexistentuser99999/path", |
| 62 | + expected: "~nonexistentuser99999/path", // Should return original |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "invalid username no slash", |
| 66 | + input: "~nonexistentuser99999", |
| 67 | + expected: "~nonexistentuser99999", // Should return original |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "root user home", |
| 71 | + input: "~root/foo", |
| 72 | + expected: expectedRootPath("foo"), |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "empty path", |
| 76 | + input: "", |
| 77 | + expected: "", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "special prefixes not affected", |
| 81 | + input: "docker-image://something", |
| 82 | + expected: "docker-image://something", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "git url not affected", |
| 86 | + input: "[email protected]:user/repo.git", |
| 87 | + expected: "[email protected]:user/repo.git", |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + result := ExpandTilde(tt.input) |
| 94 | + if result != tt.expected { |
| 95 | + t.Errorf("ExpandTilde(%q) = %q, want %q", tt.input, result, tt.expected) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestExpandTildePaths(t *testing.T) { |
| 102 | + home, err := os.UserHomeDir() |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to get home directory: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + tests := []struct { |
| 108 | + name string |
| 109 | + input []string |
| 110 | + expected []string |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "nil input", |
| 114 | + input: nil, |
| 115 | + expected: nil, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "empty slice", |
| 119 | + input: []string{}, |
| 120 | + expected: []string{}, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "mixed paths", |
| 124 | + input: []string{"~/path1", "/absolute/path", "relative/path", "~/.ssh/id_rsa"}, |
| 125 | + expected: []string{filepath.Join(home, "path1"), "/absolute/path", "relative/path", filepath.Join(home, ".ssh/id_rsa")}, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "all tildes", |
| 129 | + input: []string{"~/a", "~/b", "~/c"}, |
| 130 | + expected: []string{filepath.Join(home, "a"), filepath.Join(home, "b"), filepath.Join(home, "c")}, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "with invalid usernames", |
| 134 | + input: []string{"~/valid", "~invaliduser/path", "~/another"}, |
| 135 | + expected: []string{filepath.Join(home, "valid"), "~invaliduser/path", filepath.Join(home, "another")}, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + result := ExpandTildePaths(tt.input) |
| 142 | + if len(result) != len(tt.expected) { |
| 143 | + t.Errorf("ExpandTildePaths(%v) returned %d items, want %d", tt.input, len(result), len(tt.expected)) |
| 144 | + return |
| 145 | + } |
| 146 | + for i := range result { |
| 147 | + if result[i] != tt.expected[i] { |
| 148 | + t.Errorf("ExpandTildePaths[%d] = %q, want %q", i, result[i], tt.expected[i]) |
| 149 | + } |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments