Skip to content

Commit

Permalink
More test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg committed Mar 29, 2023
1 parent b2cea92 commit ddd4fee
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions cmd/modver/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,46 @@ import (

func TestPRHelper(t *testing.T) {
var (
ctx = context.Background()
repos mockReposService
prs mockPRsService
issues mockIssuesService
ctx = context.Background()
repos mockReposService
prs mockPRsService
)
result, err := prHelper(ctx, repos, prs, &issues, mockComparer(modver.Minor), "owner", "repo", 17)
if err != nil {
t.Fatal(err)
}
if result.Code() != modver.Minor {
t.Fatalf("got result %s, want %s", result, modver.Minor)
}
if !strings.HasPrefix(issues.body, "# Modver result") {
t.Error("issues.body does not start with # Modver result")
}
if issues.commentID != 0 {
t.Errorf("issues.commentID is %d, want 0", issues.commentID)
}

t.Run("new-comment", func(t *testing.T) {
var issues mockIssuesService

result, err := prHelper(ctx, repos, prs, &issues, mockComparer(modver.Minor), "owner", "repo", 17)
if err != nil {
t.Fatal(err)
}
if result.Code() != modver.Minor {
t.Fatalf("got result %s, want %s", result, modver.Minor)
}
if !strings.HasPrefix(issues.body, "# Modver result") {
t.Error("issues.body does not start with # Modver result")
}
if issues.commentID != 0 {
t.Errorf("issues.commentID is %d, want 0", issues.commentID)
}
})

t.Run("new-comment", func(t *testing.T) {
issues := mockIssuesService{update: true}

result, err := prHelper(ctx, repos, prs, &issues, mockComparer(modver.Minor), "owner", "repo", 17)
if err != nil {
t.Fatal(err)
}
if result.Code() != modver.Minor {
t.Fatalf("got result %s, want %s", result, modver.Minor)
}
if !strings.HasPrefix(issues.body, "# Modver result") {
t.Error("issues.body does not start with # Modver result")
}
if issues.commentID != 2 {
t.Errorf("issues.commentID is %d, want 0", issues.commentID)
}
})
}

type mockReposService struct{}
Expand All @@ -55,6 +77,7 @@ func (mockPRsService) Get(ctx context.Context, owner, reponame string, number in
}

type mockIssuesService struct {
update bool
owner, repo string
commentID int64
body string
Expand All @@ -81,6 +104,12 @@ func (m *mockIssuesService) ListComments(ctx context.Context, owner, reponame st
ID: ptr(int64(1)),
Body: ptr("not a modver comment"),
}}
if m.update {
result = append(result, &github.IssueComment{
ID: ptr(int64(2)),
Body: ptr("# Modver result\n\nwoop"),
})
}
return result, nil, nil
}

Expand Down

0 comments on commit ddd4fee

Please sign in to comment.