Skip to content

Commit

Permalink
Merge pull request #56 from chelnak/tests
Browse files Browse the repository at this point in the history
Tests for changelog package
  • Loading branch information
chelnak committed May 14, 2022
2 parents 1071fc6 + fc6acc8 commit daa2a46
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 41 deletions.
27 changes: 13 additions & 14 deletions internal/pkg/changelog/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/chelnak/gh-changelog/internal/pkg/utils"
)

var Now = time.Now // must be a better way to stub this

type ChangelogBuilder interface {
WithSpinner(enabled bool) ChangelogBuilder
WithGitClient(client gitclient.GitClient) ChangelogBuilder
WithGithubClient(client githubclient.GitHubClient) ChangelogBuilder
WithGitHubClient(client githubclient.GitHubClient) ChangelogBuilder
WithNextVersion(nextVersion string) ChangelogBuilder
Build() (Changelog, error)
}
Expand Down Expand Up @@ -49,7 +51,7 @@ func (builder *changelogBuilder) WithGitClient(git gitclient.GitClient) Changelo
}

// WithGitHubClient allows the consumer to use a custom github client that implements the GitHubClient interface
func (builder *changelogBuilder) WithGithubClient(client githubclient.GitHubClient) ChangelogBuilder {
func (builder *changelogBuilder) WithGitHubClient(client githubclient.GitHubClient) ChangelogBuilder {
builder.github = client
return builder
}
Expand Down Expand Up @@ -96,12 +98,7 @@ func (builder *changelogBuilder) Build() (Changelog, error) {

builder.tags = append(builder.tags, tags...)

c := &changelog{
repoName: builder.github.GetRepoName(),
repoOwner: builder.github.GetRepoOwner(),
unreleased: []string{},
entries: []Entry{},
}
c := NewChangelog(builder.github.GetRepoName(), builder.github.GetRepoOwner())

err = builder.buildChangeLog(c)
if err != nil {
Expand All @@ -112,12 +109,12 @@ func (builder *changelogBuilder) Build() (Changelog, error) {
return c, nil
}

func (builder *changelogBuilder) buildChangeLog(changelog *changelog) error {
func (builder *changelogBuilder) buildChangeLog(changelog Changelog) error {
if configuration.Config.ShowUnreleased && builder.nextVersion == "" {
builder.spinner.Suffix = " Getting unreleased entries"

nextTag := builder.tags[0]
pullRequests, err := builder.github.GetPullRequestsBetweenDates(nextTag.Date, time.Now())
pullRequests, err := builder.github.GetPullRequestsBetweenDates(nextTag.Date, Now())
if err != nil {
return err
}
Expand All @@ -131,7 +128,7 @@ func (builder *changelogBuilder) buildChangeLog(changelog *changelog) error {
return fmt.Errorf("could not process pull requests: %v", err)
}

changelog.unreleased = unreleased
changelog.AddUnreleased(unreleased)
}

for idx, currentTag := range builder.tags {
Expand Down Expand Up @@ -174,7 +171,8 @@ func (builder *changelogBuilder) buildChangeLog(changelog *changelog) error {
return fmt.Errorf("could not process pull requests: %v", err)
}

changelog.entries = append(changelog.entries, *entry)
//changelog.entries = append(changelog.entries, *entry)
changelog.AddEntry(*entry)
}

return nil
Expand Down Expand Up @@ -232,8 +230,9 @@ func (builder *changelogBuilder) populateReleasedEntry(currentTag string, previo
)

section := getSection(pr.Labels)

if section != "" {
err := entry.append(section, line)
err := entry.Append(section, line)
if err != nil {
return nil, err
}
Expand All @@ -258,7 +257,7 @@ func (builder *changelogBuilder) setNextVersion() error {
tag := githubclient.Tag{
Name: builder.nextVersion,
Sha: lastCommitSha,
Date: time.Now(),
Date: Now(),
}

builder.tags = append(builder.tags, tag)
Expand Down
101 changes: 101 additions & 0 deletions internal/pkg/changelog/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package changelog_test

import (
"fmt"
"testing"
"time"

"github.com/chelnak/gh-changelog/internal/pkg/changelog"
"github.com/chelnak/gh-changelog/internal/pkg/configuration"
"github.com/chelnak/gh-changelog/internal/pkg/githubclient"
"github.com/chelnak/gh-changelog/mocks"
"github.com/stretchr/testify/assert"
)

func safeParseTime() time.Time {
time, _ := time.Parse(time.RFC3339, time.Time{}.String())
return time
}

func setupMockGitClient() *mocks.GitClient {
mockGitClient := &mocks.GitClient{}
mockGitClient.On("GetFirstCommit").Return("42d4c93b23eaf307c5f9712f4c62014fe38332bd", nil)
mockGitClient.On("GetLastCommit").Return("0d724ba5b4235aa88d45a20f4ecd8db4b4695cf1", nil)
mockGitClient.On("GetDateOfHash", "42d4c93b23eaf307c5f9712f4c62014fe38332bd").Return(safeParseTime(), nil).Once()
return mockGitClient
}

func setupMockGitHubClient() *mocks.GitHubClient {
mockGitHubClient := &mocks.GitHubClient{}
mockGitHubClient.On("GetTags").Return([]githubclient.Tag{
{
Name: "v1.0.0",
Sha: "42d4c93b23eaf307c5f9712f4c62014fe38332bd",
Date: safeParseTime(),
},
{
Name: "v2.0.0",
Sha: "0d724ba5b4235aa88d45a20f4ecd8db4b4695cf1",
Date: safeParseTime(),
},
}, nil)

// bad ??
changelog.Now = func() time.Time {
return safeParseTime()
}

mockGitHubClient.On("GetPullRequestsBetweenDates", safeParseTime(), changelog.Now()).Return([]githubclient.PullRequest{}, nil).Once()
mockGitHubClient.On("GetPullRequestsBetweenDates", time.Time{}, time.Time{}).Return([]githubclient.PullRequest{
{
Number: 1,
Title: "this is a test pr",
User: "test-user",
Labels: []githubclient.Label{
{
Name: "enhancement",
},
},
},
{
Number: 2,
Title: "this is a test pr 2",
User: "test-user",
Labels: []githubclient.Label{
{
Name: "enhancement",
},
},
},
}, nil)

mockGitHubClient.On("GetRepoName").Return(repoName)
mockGitHubClient.On("GetRepoOwner").Return(repoOwner)

return mockGitHubClient
}

var testBuilder = changelog.NewChangelogBuilder()

func TestChangelogBuilder(t *testing.T) {
_ = configuration.InitConfig()

mockGitClient := setupMockGitClient()
mockGitHubClient := setupMockGitHubClient()

testBuilder.WithSpinner(true)
testBuilder.WithGitClient(mockGitClient)
testBuilder.WithGitHubClient(mockGitHubClient)

changelog, err := testBuilder.Build()
assert.NoError(t, err)

assert.Equal(t, changelog.GetRepoName(), "repo-name")
assert.Equal(t, changelog.GetRepoOwner(), "repo-owner")

assert.Len(t, changelog.GetUnreleased(), 0)
assert.Len(t, changelog.GetEntries(), 2)

fmt.Println(changelog.GetEntries())
assert.Equal(t, changelog.GetEntries()[0].Added[0], "this is a test pr [#1](https://github.com/repo-owner/repo-name/pull/1) ([test-user](https://github.com/test-user))\n")
}
23 changes: 21 additions & 2 deletions internal/pkg/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Entry struct {
Other []string
}

func (e *Entry) append(section string, entry string) error {
func (e *Entry) Append(section string, entry string) error {
switch strings.ToLower(section) {
case "added":
e.Added = append(e.Added, entry)
Expand All @@ -45,8 +45,10 @@ func (e *Entry) append(section string, entry string) error {
type Changelog interface {
GetRepoName() string
GetRepoOwner() string
GetUnreleased() []string
GetEntries() []Entry
AddEntry(Entry)
GetUnreleased() []string
AddUnreleased([]string)
}

type changelog struct {
Expand All @@ -56,6 +58,15 @@ type changelog struct {
entries []Entry
}

func NewChangelog(repoName string, repoOwner string) Changelog {
return &changelog{
repoName: repoName,
repoOwner: repoOwner,
unreleased: []string{},
entries: []Entry{},
}
}

func (c *changelog) GetRepoName() string {
return c.repoName
}
Expand All @@ -68,6 +79,14 @@ func (c *changelog) GetEntries() []Entry {
return c.entries
}

func (c *changelog) AddEntry(entry Entry) {
c.entries = append(c.entries, entry)
}

func (c *changelog) GetUnreleased() []string {
return c.unreleased
}

func (c *changelog) AddUnreleased(entry []string) {
c.unreleased = append(c.unreleased, entry...)
}
50 changes: 50 additions & 0 deletions internal/pkg/changelog/changelog_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package changelog_test

import (
"testing"
"time"

"github.com/chelnak/gh-changelog/internal/pkg/changelog"
"github.com/stretchr/testify/assert"
)

const (
repoName = "repo-name"
repoOwner = "repo-owner"
)

var testChangelog = changelog.NewChangelog(repoName, repoOwner)

func TetstNewChangelog(t *testing.T) {
assert.Equal(t, repoName, testChangelog.GetRepoName())
assert.Equal(t, repoOwner, testChangelog.GetRepoOwner())
assert.Equal(t, 0, len(testChangelog.GetEntries()))
assert.Equal(t, 0, len(testChangelog.GetUnreleased()))
}

func TestAddEntry(t *testing.T) {
entry := changelog.Entry{
CurrentTag: "v2.0.0",
PreviousTag: "v1.0.0",
Date: time.Time{},
}

err := entry.Append("added", "test")
assert.Nil(t, err)

testChangelog.AddEntry(entry)

entries := testChangelog.GetEntries()

assert.Equal(t, 1, len(entries))
assert.Equal(t, 1, len(entries[0].Added))
assert.Equal(t, "test", entries[0].Added[0])
}

func TestAddingAndRetrievingUnreleasedReturnsTheCorrectResponse(t *testing.T) {
testChangelog.AddUnreleased([]string{"test"})

unreleased := testChangelog.GetUnreleased()

assert.Equal(t, 1, len(unreleased))
assert.Equal(t, "test", unreleased[0])
}
4 changes: 2 additions & 2 deletions internal/pkg/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Test_ItReturnsTheCorrectRepoOwner(t *testing.T) {
assert.Equal(t, "TestOwner", repoName)
}

func newJsonResponder(status int, body string) httpmock.Responder {
func NewJSONResponder(status int, body string) httpmock.Responder {
resp := httpmock.NewStringResponse(status, body)
resp.Header.Set("Content-Type", "application/json")
return httpmock.ResponderFromResponse(resp)
Expand All @@ -41,7 +41,7 @@ func Test_GetTagsReturnsASliceOfTags(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", "https://api.github.com/graphql",
newJsonResponder(500, httpmock.File("data/get_tags_response.json").String()),
NewJSONResponder(500, httpmock.File("data/get_tags_response.json").String()),
)

t.Setenv("GITHUB_TOKEN", "xxxxxxxx")
Expand Down
46 changes: 23 additions & 23 deletions internal/pkg/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ import (

"github.com/chelnak/gh-changelog/internal/pkg/changelog"
"github.com/chelnak/gh-changelog/internal/pkg/writer"
"github.com/chelnak/gh-changelog/mocks"
"github.com/stretchr/testify/assert"
)

const (
repoName = "repo-name"
repoOwner = "repo-owner"
)

func Test_ItWritesOutAChangelogInTheCorrectFormat(t *testing.T) {
mockChangelog := &mocks.Changelog{}

mockChangelog.On("GetRepoName").Return("TestRepo")
mockChangelog.On("GetRepoOwner").Return("TestOwner")
mockChangelog.On("GetEntries").Return([]changelog.Entry{
{
CurrentTag: "v1.0.0",
PreviousTag: "v0.0.0",
Date: time.Now(),
Added: []string{"Added 1", "Added 2"},
Changed: []string{"Changed 1", "Changed 2"},
Deprecated: []string{"Deprecated 1", "Deprecated 2"},
Removed: []string{"Removed 1", "Removed 2"},
Fixed: []string{"Fixed 1", "Fixed 2"},
Security: []string{"Security 1", "Security 2"},
Other: []string{"Other 1", "Other 2"},
},
})

mockChangelog.On("GetUnreleased").Return([]string{"Unreleased 1", "Unreleased 2"})
mockChangelog := changelog.NewChangelog(repoName, repoOwner)

entry := changelog.Entry{
CurrentTag: "v1.0.0",
PreviousTag: "v0.0.0",
Date: time.Now(),
Added: []string{"Added 1", "Added 2"},
Changed: []string{"Changed 1", "Changed 2"},
Deprecated: []string{"Deprecated 1", "Deprecated 2"},
Removed: []string{"Removed 1", "Removed 2"},
Fixed: []string{"Fixed 1", "Fixed 2"},
Security: []string{"Security 1", "Security 2"},
Other: []string{"Other 1", "Other 2"},
}

mockChangelog.AddEntry(entry)
mockChangelog.AddUnreleased([]string{"Unreleased 1", "Unreleased 2"})

var buf bytes.Buffer
err := writer.Write(&buf, mockChangelog)

assert.NoError(t, err)
mockChangelog.AssertExpectations(t)

assert.Regexp(t, "## Unreleased", buf.String())
assert.Regexp(t, "- Unreleased 1", buf.String())
assert.Regexp(t, "- Unreleased 2", buf.String())

assert.Regexp(t, regexp.MustCompile(`## \[v1.0.0\]\(https:\/\/github.com\/TestOwner\/TestRepo\/tree\/v1.0.0\)`), buf.String())
assert.Regexp(t, regexp.MustCompile(`## \[v1.0.0\]\(https:\/\/github.com\/repo-owner\/repo-name\/tree\/v1.0.0\)`), buf.String())
assert.Regexp(t, "### Added", buf.String())
assert.Regexp(t, "- Added 1", buf.String())
assert.Regexp(t, "- Added 2", buf.String())
Expand Down

0 comments on commit daa2a46

Please sign in to comment.