-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from chelnak/tests
Tests for changelog package
- Loading branch information
Showing
6 changed files
with
210 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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") | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -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]) | ||
} |
This file contains 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
This file contains 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