Skip to content

Commit

Permalink
Minor tweaks and more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaevans committed Apr 24, 2024
1 parent cf1feca commit 6dc6fe7
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 36 deletions.
20 changes: 1 addition & 19 deletions cmd/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ var bumpCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(bumpCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// bumpCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// bumpCmd.Flags().Bool("major", false, "Bump the major version")
// bumpCmd.Flags().Bool("minor", false, "Bump the minor version")
// bumpCmd.Flags().Bool("patch", false, "Bump the patch version")
// bumpCmd.Flags().Bool("prerelease", false, "Bump the prerelease version")
// bumpCmd.Flags().String("build", "", "Append the given string to the version as build information.")
// bumpCmd.Flags().String("from-message", "", "Extract the bump type from a commit message")
// bumpCmd.MarkFlagsMutuallyExclusive("major", "minor", "patch", "prerelease", "from-message")

addCommonBumpFlags(bumpCmd)
bumpCmd.Flags().String("metadata", "", "Append the given string to the version as metadata.")

Expand All @@ -84,8 +68,6 @@ func runBump(cmd *cobra.Command, args []string) {
fmt.Println("Error bumping version:", err)
return
}
// if viper.GetString("metadata") != "" {
// newV = fmt.Sprintf("%s+%s", newV, viper.GetString("metadata"))
// }

fmt.Println(newV)
}
36 changes: 19 additions & 17 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/go-git/go-git/v5/plumbing"
)

var ErrNoSemverTags = fmt.Errorf("no semver tags found")

// gitCmd represents the git command
var gitCmd = &cobra.Command{
Use: "git",
Expand Down Expand Up @@ -63,23 +65,35 @@ func getTags(repo *goget.Repository) ([]string, error) {
semverTags := make([]string, 0)
if err := iter.ForEach(func(ref *plumbing.Reference) error {
shortTag := ref.Name().Short()
v, err := semver.NewVersion(shortTag)
_, err := semver.NewVersion(shortTag)
if err != nil {
fmt.Printf("Could not parse tag %s as semver: %s\n", shortTag, err)
return nil
}
semverTags = append(semverTags, v.String())
semverTags = append(semverTags, shortTag)
return nil
}); err != nil {
return nil, err
}
return semverTags, nil
}

func gitBump(version string, repo *goget.Repository) (*semver.Version, error) {
func gitBump(repo *goget.Repository) (*semver.Version, error) {
bumpType := getBumpType()

newVersion, err := doBump(version, bumpType)
semverTags, err := getTags(repo)
if err != nil {
fmt.Println("Could not get tags", err)
return nil, err
}

if len(semverTags) == 0 {
fmt.Println("No semver tags found")
return nil, ErrNoSemverTags
}
latestSemverTag := semverTags[len(semverTags)-1]

newVersion, err := doBump(latestSemverTag, bumpType)
if err != nil {
fmt.Println("Could not bump version", err)
return nil, err
Expand Down Expand Up @@ -113,19 +127,7 @@ func runGit(cmd *cobra.Command, args []string) {
return
}

semverTags, err := getTags(repo)
if err != nil {
fmt.Println("Could not get tags", err)
return
}

if len(semverTags) == 0 {
fmt.Println("No semver tags found")
return
}
latestSemverTag := semverTags[len(semverTags)-1]

newVersion, err := gitBump(latestSemverTag, repo)
newVersion, err := gitBump(repo)
if err != nil {
fmt.Println("Could not bump version", err)
return
Expand Down
160 changes: 160 additions & 0 deletions cmd/git_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package cmd

import (
"fmt"
"testing"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

// func TestValidVersionAndRepository(t *testing.T) {
// // Mock the necessary dependencies
// repo := &goget.Repository{}
Expand All @@ -13,3 +25,151 @@ package cmd
// assert.NoError(t, err)
// assert.Equal(t, expectedVersion, result)
// }

func setupRepo() (*git.Repository, error) {
fs := memfs.New()
repo, err := git.Init(memory.NewStorage(), fs)

return repo, err
}

func commitFile(filename string, repo *git.Repository) (plumbing.Hash, error) {
w, err := repo.Worktree()
if err != nil {
return plumbing.Hash{}, err
}

fs := w.Filesystem

_, err = fs.Create(filename)
if err != nil {
return plumbing.Hash{}, err
}

_, err = w.Add(filename)
if err != nil {
return plumbing.Hash{}, err
}

return w.Commit(fmt.Sprintf("Commit of %s", filename), &git.CommitOptions{})
}
func TestValidVersionAndRepostory(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

commit, err := commitFile("file1.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.0.0", commit, nil)
assert.NoError(t, err)

commit, err = commitFile("file2.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("1.0.1", commit, nil)
assert.NoError(t, err)

expected := []string{"v1.0.0", "1.0.1"}
result, err := getTags(repo)
assert.NoError(t, err)
assert.Equal(t, expected, result)
}

func TestNoTags(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

expected := []string{}
result, err := getTags(repo)
assert.NoError(t, err)
assert.Equal(t, expected, result)
}

func TestInvalidVersionAndRepository(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

commit, err := commitFile("file1.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.0.0", commit, nil)
assert.NoError(t, err)

commit, err = commitFile("file2.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("foobarbaz", commit, nil)
assert.NoError(t, err)

expected := []string{"v1.0.0"}
result, err := getTags(repo)
assert.NoError(t, err)
fmt.Println(result)
assert.Equal(t, expected, result)
}

func TestGitBump_NoTags(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

_, err = gitBump(repo)
assert.Error(t, err)
assert.ErrorIs(t, err, ErrNoSemverTags)
}

func TestGitBump_OneTag(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

commit, err := commitFile("file1.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.0.0", commit, nil)
assert.NoError(t, err)

result, err := gitBump(repo)
expected := "v1.0.1"
assert.NoError(t, err)
assert.Equal(t, expected, result.Original())
}

func TestGitBump_MultipleTags(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

commit, err := commitFile("file1.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.0.0", commit, nil)
assert.NoError(t, err)

commit, err = commitFile("file2.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.1.0", commit, nil)
assert.NoError(t, err)

result, err := gitBump(repo)
expected := "v1.1.1"
assert.NoError(t, err)
assert.Equal(t, expected, result.Original())
}

func TestGitBump_WithHash(t *testing.T) {
repo, err := setupRepo()
assert.NoError(t, err)

commitHash, err := commitFile("file1.txt", repo)
assert.NoError(t, err)

_, err = repo.CreateTag("v1.0.0", commitHash, nil)
assert.NoError(t, err)

shortHash := commitHash.String()[:7]

viper.Set("hash", true)
result, err := gitBump(repo)
expected := "v1.0.1+sha." + shortHash
assert.NoError(t, err)
assert.Equal(t, expected, result.Original())
}

0 comments on commit 6dc6fe7

Please sign in to comment.