(t *testing.T)
| 205 | } |
| 206 | |
| 207 | func TestUpdateShellConfigFile(t *testing.T) { |
| 208 | // Create a temporary file |
| 209 | tempFile, err := os.CreateTemp("", "jenv_test_config") |
| 210 | if err != nil { |
| 211 | t.Fatalf("Failed to create temp file: %v", err) |
| 212 | } |
| 213 | defer os.Remove(tempFile.Name()) |
| 214 | tempFile.Close() |
| 215 | |
| 216 | key := "TEST_VAR" |
| 217 | value := "test_value" |
| 218 | |
| 219 | err = updateShellConfigFile(tempFile.Name(), key, value, "export") |
| 220 | if err != nil { |
| 221 | t.Errorf("updateShellConfigFile failed: %v", err) |
| 222 | } |
| 223 | |
| 224 | // Read the file and verify content |
| 225 | content, err := os.ReadFile(tempFile.Name()) |
| 226 | if err != nil { |
| 227 | t.Fatalf("Failed to read temp file: %v", err) |
| 228 | } |
| 229 | |
| 230 | expectedLine := "export " + key + "=\"" + value + "\"" |
| 231 | if !strings.Contains(string(content), expectedLine) { |
| 232 | t.Errorf("Expected line '%s' not found in file content: %s", expectedLine, string(content)) |
| 233 | } |
| 234 | |
| 235 | // Test updating existing variable |
| 236 | newValue := "new_test_value" |
| 237 | err = updateShellConfigFile(tempFile.Name(), key, newValue, "export") |
| 238 | if err != nil { |
| 239 | t.Errorf("updateShellConfigFile update failed: %v", err) |
| 240 | } |
| 241 | |
| 242 | // Read again and verify the value was updated |
| 243 | content, err = os.ReadFile(tempFile.Name()) |
| 244 | if err != nil { |
| 245 | t.Fatalf("Failed to read temp file after update: %v", err) |
| 246 | } |
| 247 | |
| 248 | newExpectedLine := "export " + key + "=\"" + newValue + "\"" |
| 249 | if !strings.Contains(string(content), newExpectedLine) { |
| 250 | t.Errorf("Expected updated line '%s' not found in file content: %s", newExpectedLine, string(content)) |
| 251 | } |
| 252 | |
| 253 | // Ensure old value is not present |
| 254 | oldExpectedLine := "export " + key + "=\"" + value + "\"" |
| 255 | if strings.Contains(string(content), oldExpectedLine) { |
| 256 | t.Errorf("Old line '%s' should not be present in file content: %s", oldExpectedLine, string(content)) |
| 257 | } |
| 258 | } |
nothing calls this directly
no test coverage detected