-
Notifications
You must be signed in to change notification settings - Fork 1.3k
test: add unit tests for previously uncovered server components #6301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/enhance-unit-test-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
184d8cf
Initial plan
Copilot 9c21839
feat: add unit tests for proxy, automerger, pull_status_fetcher, not_…
Copilot 3a8ca5a
test: add unit tests for InstrumentedClient and DBUpdater
Copilot b325203
fix: rename test and remove contradictory comment in automerger_inter…
Copilot a49bb1d
Merge branch 'main' into copilot/enhance-unit-test-coverage
nitrocode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // Copyright 2025 The Atlantis Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Internal tests for AutoMerger (package events, not events_test) so that we | ||
| // can access the unexported automerge / automergeEnabled / deleteSourceBranchOnMergeEnabled | ||
| // methods directly. | ||
| package events | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| . "github.com/petergtz/pegomock/v4" | ||
| "github.com/runatlantis/atlantis/server/events/command" | ||
| "github.com/runatlantis/atlantis/server/events/models" | ||
| vcsmocks "github.com/runatlantis/atlantis/server/events/vcs/mocks" | ||
| "github.com/runatlantis/atlantis/server/logging" | ||
| . "github.com/runatlantis/atlantis/testing" | ||
| ) | ||
|
|
||
| // buildApplyContext builds a minimal *command.Context suitable for automerge tests. | ||
| func buildApplyContext(t *testing.T, vcsClient *vcsmocks.MockClient) *command.Context { | ||
| t.Helper() | ||
| return &command.Context{ | ||
| Log: logging.NewNoopLogger(t), | ||
| Pull: models.PullRequest{ | ||
| Num: 1, | ||
| BaseRepo: models.Repo{ | ||
| FullName: "owner/repo", | ||
| VCSHost: models.VCSHost{Type: models.Github}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // automerge | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| func TestAutomerge_AllApplied_MergesAndComments(t *testing.T) { | ||
| RegisterMockTestingT(t) | ||
| vcsClient := vcsmocks.NewMockClient() | ||
| am := &AutoMerger{VCSClient: vcsClient, GlobalAutomerge: true} | ||
| ctx := buildApplyContext(t, vcsClient) | ||
|
|
||
| pullStatus := models.PullStatus{ | ||
| Projects: []models.ProjectStatus{ | ||
| {Status: models.AppliedPlanStatus}, | ||
| {Status: models.AppliedPlanStatus}, | ||
| }, | ||
| } | ||
|
|
||
| am.automerge(ctx, pullStatus, false, "") | ||
|
|
||
| // Should have commented (once for the automerge announcement) and merged once. | ||
| vcsClient.VerifyWasCalledOnce().CreateComment( | ||
| Any[logging.SimpleLogging](), | ||
| Eq(ctx.Pull.BaseRepo), | ||
| Eq(ctx.Pull.Num), | ||
| Eq(automergeComment), | ||
| Eq(command.Apply.String()), | ||
| ) | ||
| vcsClient.VerifyWasCalledOnce().MergePull( | ||
| Any[logging.SimpleLogging](), | ||
| Eq(ctx.Pull), | ||
| Eq(models.PullRequestOptions{DeleteSourceBranchOnMerge: false, MergeMethod: ""}), | ||
| ) | ||
| } | ||
|
|
||
| func TestAutomerge_NotAllApplied_DoesNotMerge(t *testing.T) { | ||
| RegisterMockTestingT(t) | ||
| vcsClient := vcsmocks.NewMockClient() | ||
| am := &AutoMerger{VCSClient: vcsClient, GlobalAutomerge: true} | ||
| ctx := buildApplyContext(t, vcsClient) | ||
|
|
||
| pullStatus := models.PullStatus{ | ||
| Projects: []models.ProjectStatus{ | ||
| {Status: models.AppliedPlanStatus, RepoRelDir: "a", Workspace: "default"}, | ||
| {Status: models.PlannedPlanStatus, RepoRelDir: "b", Workspace: "default"}, | ||
| }, | ||
| } | ||
|
|
||
| am.automerge(ctx, pullStatus, false, "") | ||
|
|
||
| // Neither comment nor merge should have been called. | ||
| vcsClient.VerifyWasCalled(Never()).CreateComment( | ||
| Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string](), | ||
| ) | ||
| vcsClient.VerifyWasCalled(Never()).MergePull( | ||
| Any[logging.SimpleLogging](), Any[models.PullRequest](), Any[models.PullRequestOptions](), | ||
| ) | ||
| } | ||
|
|
||
| func TestAutomerge_MergeFails_PostsFailureComment(t *testing.T) { | ||
| RegisterMockTestingT(t) | ||
| vcsClient := vcsmocks.NewMockClient() | ||
| am := &AutoMerger{VCSClient: vcsClient, GlobalAutomerge: true} | ||
| ctx := buildApplyContext(t, vcsClient) | ||
|
|
||
| pullStatus := models.PullStatus{ | ||
| Projects: []models.ProjectStatus{ | ||
| {Status: models.AppliedPlanStatus}, | ||
| }, | ||
| } | ||
|
|
||
| mergeErr := errors.New("merge conflict") | ||
| When(vcsClient.MergePull(Any[logging.SimpleLogging](), Any[models.PullRequest](), Any[models.PullRequestOptions]())). | ||
| ThenReturn(mergeErr) | ||
|
|
||
| am.automerge(ctx, pullStatus, false, "") | ||
|
|
||
| // Two CreateComment calls expected: announcement + failure. | ||
| vcsClient.VerifyWasCalled(Twice()).CreateComment( | ||
| Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string](), | ||
| ) | ||
| } | ||
|
|
||
| func TestAutomerge_DeleteSourceBranch_PassedToMerge(t *testing.T) { | ||
| RegisterMockTestingT(t) | ||
| vcsClient := vcsmocks.NewMockClient() | ||
| am := &AutoMerger{VCSClient: vcsClient, GlobalAutomerge: true} | ||
| ctx := buildApplyContext(t, vcsClient) | ||
|
|
||
| pullStatus := models.PullStatus{ | ||
| Projects: []models.ProjectStatus{ | ||
| {Status: models.AppliedPlanStatus}, | ||
| }, | ||
| } | ||
|
|
||
| am.automerge(ctx, pullStatus, true, "squash") | ||
|
|
||
| vcsClient.VerifyWasCalledOnce().MergePull( | ||
| Any[logging.SimpleLogging](), | ||
| Eq(ctx.Pull), | ||
| Eq(models.PullRequestOptions{DeleteSourceBranchOnMerge: true, MergeMethod: "squash"}), | ||
| ) | ||
| } | ||
|
|
||
| func TestAutomerge_EmptyProjects_DoesNotMerge(t *testing.T) { | ||
| RegisterMockTestingT(t) | ||
| vcsClient := vcsmocks.NewMockClient() | ||
| am := &AutoMerger{VCSClient: vcsClient, GlobalAutomerge: true} | ||
| ctx := buildApplyContext(t, vcsClient) | ||
|
|
||
| // PullStatus with no projects – nothing to check, nothing to merge. | ||
| am.automerge(ctx, models.PullStatus{}, false, "") | ||
|
|
||
| // No projects == vacuously all applied → should still merge. | ||
|
nitrocode marked this conversation as resolved.
Outdated
|
||
| vcsClient.VerifyWasCalledOnce().MergePull( | ||
| Any[logging.SimpleLogging](), Any[models.PullRequest](), Any[models.PullRequestOptions](), | ||
| ) | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // automergeEnabled | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| func TestAutomergeEnabled_GlobalTrue_NoProjects(t *testing.T) { | ||
| am := &AutoMerger{GlobalAutomerge: true} | ||
| Equals(t, true, am.automergeEnabled(nil)) | ||
| Equals(t, true, am.automergeEnabled([]command.ProjectContext{})) | ||
| } | ||
|
|
||
| func TestAutomergeEnabled_GlobalFalse_NoProjects(t *testing.T) { | ||
| am := &AutoMerger{GlobalAutomerge: false} | ||
| Equals(t, false, am.automergeEnabled(nil)) | ||
| Equals(t, false, am.automergeEnabled([]command.ProjectContext{})) | ||
| } | ||
|
|
||
| func TestAutomergeEnabled_ProjectOverridesGlobal(t *testing.T) { | ||
| // Even when GlobalAutomerge is false, a project with AutomergeEnabled=true should win. | ||
| am := &AutoMerger{GlobalAutomerge: false} | ||
| Equals(t, true, am.automergeEnabled([]command.ProjectContext{ | ||
| {AutomergeEnabled: true}, | ||
| })) | ||
|
|
||
| // And when GlobalAutomerge is true, a project with AutomergeEnabled=false should win. | ||
| am2 := &AutoMerger{GlobalAutomerge: true} | ||
| Equals(t, false, am2.automergeEnabled([]command.ProjectContext{ | ||
| {AutomergeEnabled: false}, | ||
| })) | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // deleteSourceBranchOnMergeEnabled | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| func TestDeleteSourceBranchOnMergeEnabled_NoProjects_ReturnsFalse(t *testing.T) { | ||
| am := &AutoMerger{} | ||
| Equals(t, false, am.deleteSourceBranchOnMergeEnabled(nil)) | ||
| Equals(t, false, am.deleteSourceBranchOnMergeEnabled([]command.ProjectContext{})) | ||
| } | ||
|
|
||
| func TestDeleteSourceBranchOnMergeEnabled_WithFlag(t *testing.T) { | ||
| am := &AutoMerger{} | ||
| Equals(t, true, am.deleteSourceBranchOnMergeEnabled([]command.ProjectContext{ | ||
| {DeleteSourceBranchOnMerge: true}, | ||
| })) | ||
| } | ||
|
|
||
| func TestDeleteSourceBranchOnMergeEnabled_FlagFalse(t *testing.T) { | ||
| am := &AutoMerger{} | ||
| Equals(t, false, am.deleteSourceBranchOnMergeEnabled([]command.ProjectContext{ | ||
| {DeleteSourceBranchOnMerge: false}, | ||
| })) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.