TestRepositoryApply tests applying an environment as staged changes (equivalent to merge --squash)
(t *testing.T)
| 63 | |
| 64 | // TestRepositoryApply tests applying an environment as staged changes (equivalent to merge --squash) |
| 65 | func TestRepositoryApply(t *testing.T) { |
| 66 | WithRepository(t, "repository-apply", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) { |
| 67 | ctx := context.Background() |
| 68 | |
| 69 | // Create an environment and add content with multiple commits |
| 70 | env := user.CreateEnvironment("Test Apply", "Testing repository apply functionality") |
| 71 | user.FileWrite(env.ID, "apply-test.txt", "first version", "First commit") |
| 72 | user.FileWrite(env.ID, "apply-test.txt", "updated version", "Second commit") |
| 73 | user.FileWrite(env.ID, "another-file.txt", "another file", "Third commit") |
| 74 | |
| 75 | // Get initial branch |
| 76 | initialBranch, err := repository.RunGitCommand(ctx, repo.SourcePath(), "branch", "--show-current") |
| 77 | require.NoError(t, err) |
| 78 | initialBranch = strings.TrimSpace(initialBranch) |
| 79 | |
| 80 | // Apply the environment (squash merge) |
| 81 | var applyOutput bytes.Buffer |
| 82 | err = repo.Apply(ctx, env.ID, &applyOutput) |
| 83 | require.NoError(t, err, "Apply should succeed: %s", applyOutput.String()) |
| 84 | |
| 85 | // Verify we're still on the initial branch |
| 86 | currentBranch, err := repository.RunGitCommand(ctx, repo.SourcePath(), "branch", "--show-current") |
| 87 | require.NoError(t, err) |
| 88 | assert.Equal(t, initialBranch, strings.TrimSpace(currentBranch)) |
| 89 | |
| 90 | // Verify the files were applied to working directory |
| 91 | applyTestPath := filepath.Join(repo.SourcePath(), "apply-test.txt") |
| 92 | content, err := os.ReadFile(applyTestPath) |
| 93 | require.NoError(t, err) |
| 94 | assert.Equal(t, "updated version", string(content)) |
| 95 | |
| 96 | anotherFilePath := filepath.Join(repo.SourcePath(), "another-file.txt") |
| 97 | anotherContent, err := os.ReadFile(anotherFilePath) |
| 98 | require.NoError(t, err) |
| 99 | assert.Equal(t, "another file", string(anotherContent)) |
| 100 | |
| 101 | // With apply, changes should be staged but not committed yet |
| 102 | status, err := repository.RunGitCommand(ctx, repo.SourcePath(), "status", "--porcelain") |
| 103 | require.NoError(t, err) |
| 104 | // Files should be staged (prefixed with A or M) |
| 105 | assert.Contains(t, status, "apply-test.txt") |
| 106 | assert.Contains(t, status, "another-file.txt") |
| 107 | |
| 108 | // Verify no commits were made (original commit history should be discarded) |
| 109 | log, err := repository.RunGitCommand(ctx, repo.SourcePath(), "log", "--oneline", "-10") |
| 110 | require.NoError(t, err) |
| 111 | // Should NOT contain the individual environment commits |
| 112 | assert.NotContains(t, log, "First commit", "Apply should discard original commit history") |
| 113 | assert.NotContains(t, log, "Second commit", "Apply should discard original commit history") |
| 114 | assert.NotContains(t, log, "Third commit", "Apply should discard original commit history") |
| 115 | |
| 116 | // User can now commit manually |
| 117 | _, err = repository.RunGitCommand(ctx, repo.SourcePath(), "commit", "-m", "Apply environment changes") |
| 118 | require.NoError(t, err) |
| 119 | |
| 120 | // Verify the commit was made |
| 121 | finalLog, err := repository.RunGitCommand(ctx, repo.SourcePath(), "log", "--oneline", "-1") |
| 122 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected