|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRemoveStaleV2Shim(t *testing.T) { |
| 11 | + // Create a temporary directory for testing |
| 12 | + tmpDir, err := os.MkdirTemp("", "goenv-test-*") |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 15 | + } |
| 16 | + defer os.RemoveAll(tmpDir) |
| 17 | + |
| 18 | + shimsDir := filepath.Join(tmpDir, "shims") |
| 19 | + if err := os.MkdirAll(shimsDir, 0755); err != nil { |
| 20 | + t.Fatalf("Failed to create shims dir: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + t.Run("removes v2 shim with forward slash", func(t *testing.T) { |
| 24 | + // Create a v2 shim with forward slash |
| 25 | + shimPath := filepath.Join(shimsDir, "goenv") |
| 26 | + v2Content := `#!/usr/bin/env bash |
| 27 | +exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@" |
| 28 | +` |
| 29 | + if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil { |
| 30 | + t.Fatalf("Failed to create shim: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + removed, err := RemoveStaleV2Shim(shimsDir) |
| 34 | + if err != nil { |
| 35 | + t.Errorf("Unexpected error: %v", err) |
| 36 | + } |
| 37 | + if !removed { |
| 38 | + t.Error("Expected shim to be removed") |
| 39 | + } |
| 40 | + if _, err := os.Stat(shimPath); !os.IsNotExist(err) { |
| 41 | + t.Error("Shim file should not exist") |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("removes v2 shim with backslash", func(t *testing.T) { |
| 46 | + // Create a v2 shim with backslash (Windows-style) |
| 47 | + shimPath := filepath.Join(shimsDir, "goenv") |
| 48 | + v2Content := `@echo off |
| 49 | +"C:\Program Files\goenv\libexec\goenv" %* |
| 50 | +` |
| 51 | + if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil { |
| 52 | + t.Fatalf("Failed to create shim: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + removed, err := RemoveStaleV2Shim(shimsDir) |
| 56 | + if err != nil { |
| 57 | + t.Errorf("Unexpected error: %v", err) |
| 58 | + } |
| 59 | + if !removed { |
| 60 | + t.Error("Expected shim to be removed") |
| 61 | + } |
| 62 | + if _, err := os.Stat(shimPath); !os.IsNotExist(err) { |
| 63 | + t.Error("Shim file should not exist") |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("does not remove non-v2 shim", func(t *testing.T) { |
| 68 | + // Create a non-v2 shim |
| 69 | + shimPath := filepath.Join(shimsDir, "goenv") |
| 70 | + v3Content := `#!/usr/bin/env bash |
| 71 | +# This is a v3 shim |
| 72 | +exec "goenv" "$@" |
| 73 | +` |
| 74 | + if err := os.WriteFile(shimPath, []byte(v3Content), 0755); err != nil { |
| 75 | + t.Fatalf("Failed to create shim: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + removed, err := RemoveStaleV2Shim(shimsDir) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("Unexpected error: %v", err) |
| 81 | + } |
| 82 | + if removed { |
| 83 | + t.Error("Expected shim to NOT be removed") |
| 84 | + } |
| 85 | + if _, err := os.Stat(shimPath); os.IsNotExist(err) { |
| 86 | + t.Error("Shim file should still exist") |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("handles missing shim file", func(t *testing.T) { |
| 91 | + // Ensure no shim exists |
| 92 | + shimPath := filepath.Join(shimsDir, "goenv") |
| 93 | + os.Remove(shimPath) |
| 94 | + |
| 95 | + removed, err := RemoveStaleV2Shim(shimsDir) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("Unexpected error: %v", err) |
| 98 | + } |
| 99 | + if removed { |
| 100 | + t.Error("Expected no removal when file doesn't exist") |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("returns error when removal fails", func(t *testing.T) { |
| 105 | + // Skip on Windows - Unix permission model doesn't apply |
| 106 | + if runtime.GOOS == "windows" { |
| 107 | + t.Skip("Skipping permission test on Windows - different permission model") |
| 108 | + } |
| 109 | + |
| 110 | + shimPath := filepath.Join(shimsDir, "goenv") |
| 111 | + v2Content := `#!/usr/bin/env bash |
| 112 | +exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@" |
| 113 | +` |
| 114 | + if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil { |
| 115 | + t.Fatalf("Failed to create shim: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + // Make directory read-only (Unix-like systems only) |
| 119 | + if err := os.Chmod(shimsDir, 0555); err != nil { |
| 120 | + t.Skipf("Cannot test permission error: %v", err) |
| 121 | + } |
| 122 | + defer os.Chmod(shimsDir, 0755) // Restore permissions |
| 123 | + |
| 124 | + removed, err := RemoveStaleV2Shim(shimsDir) |
| 125 | + if err == nil { |
| 126 | + t.Error("Expected error when removal fails") |
| 127 | + } |
| 128 | + if removed { |
| 129 | + t.Error("Should not report as removed when error occurs") |
| 130 | + } |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +func TestV2ShimPattern(t *testing.T) { |
| 135 | + tests := []struct { |
| 136 | + name string |
| 137 | + content string |
| 138 | + matches bool |
| 139 | + }{ |
| 140 | + { |
| 141 | + name: "forward slash", |
| 142 | + content: `exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"`, |
| 143 | + matches: true, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "backslash", |
| 147 | + content: `"C:\Program Files\goenv\libexec\goenv" %*`, |
| 148 | + matches: true, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "no match", |
| 152 | + content: `exec "goenv" "$@"`, |
| 153 | + matches: false, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "libexec only", |
| 157 | + content: `exec "/usr/local/libexec/goenv-other" "$@"`, |
| 158 | + matches: false, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "goenv only", |
| 162 | + content: `exec "/usr/local/bin/goenv" "$@"`, |
| 163 | + matches: false, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, tt := range tests { |
| 168 | + t.Run(tt.name, func(t *testing.T) { |
| 169 | + matches := V2ShimPattern.MatchString(tt.content) |
| 170 | + if matches != tt.matches { |
| 171 | + t.Errorf("Expected match=%v, got match=%v for content: %s", tt.matches, matches, tt.content) |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments