|
6 | 6 | "path/filepath" |
7 | 7 | "strings" |
8 | 8 | "testing" |
| 9 | + "time" |
9 | 10 |
|
10 | 11 | "github.com/stretchr/testify/assert" |
11 | 12 | "github.com/stretchr/testify/require" |
@@ -298,6 +299,66 @@ func TestApplyReplacement_NonExistentFile(t *testing.T) { |
298 | 299 | assert.Equal(t, 0, count) // Should skip non-existent files gracefully |
299 | 300 | } |
300 | 301 |
|
| 302 | +func TestApplyReplacement_Idempotence(t *testing.T) { |
| 303 | + // Test that applying the same replacement twice doesn't change the file |
| 304 | + tmpDir := t.TempDir() |
| 305 | + |
| 306 | + // Create test file with old pattern |
| 307 | + testFile := filepath.Join(tmpDir, "test.yaml") |
| 308 | + originalContent := `repoUrl: git@github.com:old-org/old-repo.git |
| 309 | +otherField: value |
| 310 | +` |
| 311 | + err := os.WriteFile(testFile, []byte(originalContent), 0644) |
| 312 | + require.NoError(t, err) |
| 313 | + |
| 314 | + r := replacement{ |
| 315 | + name: "Repository URLs", |
| 316 | + pattern: "git@github.com:old-org/old-repo.git", |
| 317 | + replace: "git@github.com:myorg/myrepo.git", |
| 318 | + files: []string{"test.yaml"}, |
| 319 | + } |
| 320 | + |
| 321 | + // First run: pattern matches, replacement made |
| 322 | + count, err := applyReplacement(tmpDir, r, false) |
| 323 | + require.NoError(t, err) |
| 324 | + assert.Equal(t, 1, count, "First run should update the file") |
| 325 | + |
| 326 | + // Verify content changed |
| 327 | + content, err := os.ReadFile(testFile) |
| 328 | + require.NoError(t, err) |
| 329 | + assert.Contains(t, string(content), "myorg/myrepo") |
| 330 | + assert.NotContains(t, string(content), "old-org/old-repo") |
| 331 | + |
| 332 | + // Get file info after first change |
| 333 | + info1, err := os.Stat(testFile) |
| 334 | + require.NoError(t, err) |
| 335 | + |
| 336 | + // Sleep to ensure we can detect modification time changes |
| 337 | + time.Sleep(10 * time.Millisecond) |
| 338 | + |
| 339 | + // Second run with same replacement: should detect no change needed |
| 340 | + r2 := replacement{ |
| 341 | + name: "Repository URLs", |
| 342 | + pattern: "git@github.com:myorg/myrepo.git", |
| 343 | + replace: "git@github.com:myorg/myrepo.git", // Same as pattern - no-op |
| 344 | + files: []string{"test.yaml"}, |
| 345 | + } |
| 346 | + |
| 347 | + count, err = applyReplacement(tmpDir, r2, false) |
| 348 | + require.NoError(t, err) |
| 349 | + assert.Equal(t, 0, count, "Second run should not update when content unchanged") |
| 350 | + |
| 351 | + // Verify content unchanged |
| 352 | + newContent, err := os.ReadFile(testFile) |
| 353 | + require.NoError(t, err) |
| 354 | + assert.Equal(t, string(content), string(newContent), "Content should remain unchanged") |
| 355 | + |
| 356 | + // Verify file modification time didn't change |
| 357 | + info2, err := os.Stat(testFile) |
| 358 | + require.NoError(t, err) |
| 359 | + assert.Equal(t, info1.ModTime(), info2.ModTime(), "File should not be modified when content unchanged") |
| 360 | +} |
| 361 | + |
301 | 362 | func TestListGoFiles(t *testing.T) { |
302 | 363 | // Create temporary workspace with Go files |
303 | 364 | tmpDir := t.TempDir() |
|
0 commit comments