Skip to content

Commit 30653c0

Browse files
committed
feat: fix tests
1 parent a6133ae commit 30653c0

5 files changed

Lines changed: 70 additions & 4 deletions

File tree

cli/cmd/bootstrap_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestBootstrapIntegration_GitCryptKey(t *testing.T) {
129129
secret := mockClient.GetSecret("argocd", "git-crypt-key")
130130
assert.NotNil(t, secret)
131131
assert.Equal(t, "git-crypt-key", secret.Name)
132-
assert.Equal(t, keyData, secret.Data["key"])
132+
assert.Equal(t, keyData, secret.Data["git-crypt-key"])
133133
}
134134

135135
// TestBootstrapIntegration_SequentialErrors tests recovery by retrying after errors.

cli/cmd/template.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ func applyReplacement(workspaceRoot string, r replacement, dryRun bool) (int, er
273273
// Replace
274274
newContent := strings.ReplaceAll(originalContent, r.pattern, r.replace)
275275

276+
// Skip if content unchanged (idempotence)
277+
if newContent == originalContent {
278+
continue
279+
}
280+
276281
if dryRun {
277282
fmt.Printf(" [DRY RUN] Would update: %s\n", relPath)
278283
changedCount++

cli/cmd/template_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88
"testing"
9+
"time"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -298,6 +299,66 @@ func TestApplyReplacement_NonExistentFile(t *testing.T) {
298299
assert.Equal(t, 0, count) // Should skip non-existent files gracefully
299300
}
300301

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+
301362
func TestListGoFiles(t *testing.T) {
302363
// Create temporary workspace with Go files
303364
tmpDir := t.TempDir()

cli/cmd/validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func validateRepoAccess(secrets *config.EnvironmentSecrets) validateResult {
273273
ctx, cancel := contextWithTimeout(validateRepoTimeout)
274274
defer cancel()
275275

276-
cmd := exec.CommandContext(ctx, path, "ls-remote", "--exit-code", secrets.Repo.URL, ref) // #nosec G204
276+
cmd := exec.CommandContext(ctx, path, "ls-remote", "--exit-code", "--", secrets.Repo.URL, ref) // #nosec G204
277277
output, err := cmd.CombinedOutput()
278278
if err != nil {
279279
return validateResult{name: "repo access", err: fmt.Errorf("git ls-remote failed: %w\n output: %s", err, string(output))}
@@ -325,7 +325,7 @@ func validateSSHRepoAccess(secrets *config.EnvironmentSecrets) validateResult {
325325
ctx, cancel := contextWithTimeout(validateRepoTimeout)
326326
defer cancel()
327327

328-
cmd := exec.CommandContext(ctx, path, "ls-remote", "--exit-code", repoURL, ref) // #nosec G204
328+
cmd := exec.CommandContext(ctx, path, "ls-remote", "--exit-code", "--", repoURL, ref) // #nosec G204
329329
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_SSH_COMMAND=ssh -i %q -o BatchMode=yes -o StrictHostKeyChecking=yes", keyFile.Name()))
330330
output, err := cmd.CombinedOutput()
331331
if err != nil {

cli/internal/k8s/client_mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (m *MockClient) CreateGitCryptKeySecret(ctx context.Context, keyData []byte
109109
},
110110
Type: corev1.SecretTypeOpaque,
111111
Data: map[string][]byte{
112-
"key": keyData,
112+
"git-crypt-key": keyData,
113113
},
114114
}
115115

0 commit comments

Comments
 (0)