From 6dc6fe70b7421698dc9ff25d928319f69b4adaf4 Mon Sep 17 00:00:00 2001 From: James Evans <54334+jaevans@users.noreply.github.com> Date: Tue, 23 Apr 2024 22:03:15 -0500 Subject: [PATCH] Minor tweaks and more testing --- cmd/bump.go | 20 +----- cmd/git.go | 36 ++++++----- cmd/git_test.go | 160 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 36 deletions(-) diff --git a/cmd/bump.go b/cmd/bump.go index 1caff69..ba63535 100644 --- a/cmd/bump.go +++ b/cmd/bump.go @@ -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.") @@ -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) } diff --git a/cmd/git.go b/cmd/git.go index d8155cf..bfebda7 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -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", @@ -63,12 +65,12 @@ 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 @@ -76,10 +78,22 @@ func getTags(repo *goget.Repository) ([]string, error) { 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 @@ -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 diff --git a/cmd/git_test.go b/cmd/git_test.go index 58fbfa3..33604ed 100644 --- a/cmd/git_test.go +++ b/cmd/git_test.go @@ -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{} @@ -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()) +}