Skip to content

Commit 389739d

Browse files
committed
add unit tests
1 parent 31f8e05 commit 389739d

File tree

1 file changed

+122
-5
lines changed

1 file changed

+122
-5
lines changed

main_test.go

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ type mockGitHubClient struct {
129129
initCommentsError error
130130
addCommentError error
131131
approvePRError error
132+
AddCommentCalled bool
133+
AddCommentInput string
132134
}
133135

134136
func (m *mockGitHubClient) PR() *github.PullRequest {
@@ -218,6 +220,8 @@ func (m *mockGitHubClient) InitComments() error {
218220
}
219221

220222
func (m *mockGitHubClient) AddComment(comment string) error {
223+
m.AddCommentCalled = true
224+
m.AddCommentInput = comment
221225
if m.addCommentError != nil {
222226
return m.addCommentError
223227
}
@@ -247,6 +251,11 @@ func (m *mockGitHubClient) IsInComments(comment string, since *time.Time) (bool,
247251
return false, nil
248252
}
249253

254+
func (m *mockGitHubClient) ResetCommentCallTracking() {
255+
m.AddCommentCalled = false
256+
m.AddCommentInput = ""
257+
}
258+
250259
func (m *mockGitHubClient) IsSubstringInComments(substring string, since *time.Time) (bool, error) {
251260
if m.comments == nil {
252261
return false, nil
@@ -376,11 +385,12 @@ func TestNewApp(t *testing.T) {
376385
{
377386
name: "valid config",
378387
config: AppConfig{
379-
Token: "test-token",
380-
RepoDir: "/test/dir",
381-
PR: 123,
382-
Repo: "owner/repo",
383-
Verbose: true,
388+
Token: "test-token",
389+
RepoDir: "/test/dir",
390+
PR: 123,
391+
Repo: "owner/repo",
392+
Verbose: true,
393+
AddComments: false,
384394
},
385395
expectError: false,
386396
},
@@ -432,6 +442,9 @@ func TestNewApp(t *testing.T) {
432442
if app.config.Verbose != tc.config.Verbose {
433443
t.Errorf("expected verbose %v, got %v", tc.config.Verbose, app.config.Verbose)
434444
}
445+
if app.config.AddComments != tc.config.AddComments {
446+
t.Errorf("expected AddComments %v, got %v", tc.config.AddComments, app.config.AddComments)
447+
}
435448
})
436449
}
437450
}
@@ -628,6 +641,110 @@ func TestInitFlags(t *testing.T) {
628641
}
629642
}
630643

644+
func setupAppForCommentTest(t *testing.T, addComments bool) (*App, *mockGitHubClient) {
645+
t.Helper()
646+
647+
mockGH := &mockGitHubClient{}
648+
mockGH.ResetCommentCallTracking()
649+
650+
cfg := AppConfig{
651+
AddComments: addComments,
652+
}
653+
654+
conf := &owners.Config{
655+
HighPriorityLabels: []string{"high-prio"},
656+
}
657+
658+
app := &App{
659+
config: cfg,
660+
client: mockGH,
661+
conf: conf,
662+
codeowners: &mockCodeOwners{},
663+
gitDiff: mockGitDiff{},
664+
}
665+
666+
return app, mockGH
667+
}
668+
669+
func TestAddReviewStatusComment_ShortCircuit(t *testing.T) {
670+
app, mockClient := setupAppForCommentTest(t, false) // AddComments = false
671+
672+
// Prepare some data that *would* trigger a comment if AddComments were true
673+
unapproved := codeowners.ReviewerGroups{
674+
&codeowners.ReviewerGroup{Names: []string{"@pending-reviewer"}},
675+
}
676+
allRequired := codeowners.ReviewerGroups{
677+
&codeowners.ReviewerGroup{Names: []string{"@pending-reviewer"}},
678+
}
679+
680+
err := app.addReviewStatusComment(allRequired, unapproved, false)
681+
if err != nil {
682+
t.Errorf("Expected no error when AddComments is false, but got: %v", err)
683+
}
684+
685+
if mockClient.AddCommentCalled {
686+
t.Error("Expected AddComment not to be called when AddComments is false")
687+
}
688+
}
689+
690+
func TestAddOptionalCcComment_ShortCircuit(t *testing.T) {
691+
app, mockClient := setupAppForCommentTest(t, false) // AddComments = false
692+
693+
// Prepare some data that *would* trigger a comment if AddComments were true
694+
optionalReviewers := []string{"@optional-cc"}
695+
696+
err := app.addOptionalCcComment(optionalReviewers)
697+
if err != nil {
698+
t.Errorf("Expected no error when AddComments is false, but got: %v", err)
699+
}
700+
701+
if mockClient.AddCommentCalled {
702+
t.Error("Expected AddComment not to be called when AddComments is false")
703+
}
704+
}
705+
706+
func TestAddReviewStatusComment_AddsComment(t *testing.T) {
707+
app, mockClient := setupAppForCommentTest(t, true) // AddComments = true
708+
709+
unapproved := codeowners.ReviewerGroups{
710+
&codeowners.ReviewerGroup{Names: []string{"@user1"}},
711+
}
712+
allRequired := codeowners.ReviewerGroups{
713+
&codeowners.ReviewerGroup{Names: []string{"@user1"}},
714+
}
715+
expectedComment := allRequired.ToCommentString()
716+
717+
err := app.addReviewStatusComment(allRequired, unapproved, false)
718+
719+
if err != nil {
720+
t.Errorf("Unexpected error when adding comment: %v", err)
721+
}
722+
if !mockClient.AddCommentCalled {
723+
t.Error("Expected AddComment to be called when AddComments is true and unapproved exist")
724+
}
725+
if mockClient.AddCommentInput != expectedComment {
726+
t.Errorf("Expected comment body %q, got %q", expectedComment, mockClient.AddCommentInput)
727+
}
728+
}
729+
730+
func TestAddOptionalCcComment_AddsComment(t *testing.T) {
731+
app, mockClient := setupAppForCommentTest(t, true) // AddComments = true
732+
733+
optionalReviewers := []string{"@cc-user1", "@cc-user2"}
734+
expectedComment := "cc @cc-user1 @cc-user2"
735+
736+
err := app.addOptionalCcComment(optionalReviewers)
737+
if err != nil {
738+
t.Errorf("Unexpected error when adding comment: %v", err)
739+
}
740+
if !mockClient.AddCommentCalled {
741+
t.Error("Expected AddComment to be called when AddComments is true and viewers need pinging")
742+
}
743+
if mockClient.AddCommentInput != expectedComment {
744+
t.Errorf("Expected comment body %q, got %q", expectedComment, mockClient.AddCommentInput)
745+
}
746+
}
747+
631748
func TestProcessApprovalsAndReviewers(t *testing.T) {
632749
maxReviews := 2
633750
minReviews := 2

0 commit comments

Comments
 (0)