Skip to content
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

Validate next version #76

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/pkg/changelog/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (builder *changelogBuilder) Build() (Changelog, error) {
return nil, err
}

err = builder.setNextVersion()
err = builder.setNextVersion(tags[0].Name)
if err != nil {
builder.spinner.FinalMSG = ""
return nil, err
Expand Down Expand Up @@ -247,12 +247,16 @@ func (builder *changelogBuilder) populateReleasedEntry(currentTag string, previo
return entry, nil
}

func (builder *changelogBuilder) setNextVersion() error {
func (builder *changelogBuilder) setNextVersion(currentVersion string) error {
if builder.nextVersion != "" {
if !utils.IsValidSemanticVersion(builder.nextVersion) {
return fmt.Errorf("'%s' is not a valid semantic version", builder.nextVersion)
}

if !utils.VersionIsGreaterThan(currentVersion, builder.nextVersion) {
return fmt.Errorf("the next version should be greater than the former: '%s' ≤ '%s'", builder.nextVersion, currentVersion)
}

lastCommitSha, err := builder.git.GetLastCommit()
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/changelog/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,19 @@ func TestChangelogBuilder(t *testing.T) {
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")
}

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

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

testBuilder.WithSpinner(true)
testBuilder.WithGitClient(mockGitClient)
testBuilder.WithGitHubClient(mockGitHubClient)
testBuilder.WithNextVersion("v0.0.1")

_, err := testBuilder.Build()
assert.Error(t, err)
assert.Equal(t, err.Error(), "the next version should be greater than the former: 'v0.0.1' ≤ 'v1.0.0'")
}
4 changes: 2 additions & 2 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CheckForUpdate(currentVersion string) bool {

currentVersion = parseLocalVersion(currentVersion)

if versionIsGreaterThan(currentVersion, release.Version) {
if VersionIsGreaterThan(currentVersion, release.Version) {
color.Yellow("\nVersion %s is available ✨\n\n", release.Version)
fmt.Println("Run", color.GreenString(`gh extension upgrade chelnak/gh-changelog`), "to upgrade.")

Expand Down Expand Up @@ -72,7 +72,7 @@ func requestLatestRelease() (Release, error) {
return release, nil
}

func versionIsGreaterThan(currentVersion, nextVersion string) bool {
func VersionIsGreaterThan(currentVersion, nextVersion string) bool {
constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", currentVersion))
if err != nil {
return false
Expand Down
30 changes: 30 additions & 0 deletions internal/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ func TestIsValidSemanticVersion(t *testing.T) {
}
}

func TestVersionIsGreaterThan(t *testing.T) {
tests := []struct {
name string
value string
want bool
}{
{
name: "version is greater than",
value: "2.0.0",
want: true,
},
{
name: "version is not greater than",
value: "0.1.0",
want: false,
},
{
name: "when the version is equal",
value: "1.0.0",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, utils.VersionIsGreaterThan("1.0.0", tt.value))
})
}
}

func TestCheckForUpdates(t *testing.T) {
tests := []struct {
name string
Expand Down