Skip to content
Permalink
Browse files
refactor: making it easier to merge with pro
  • Loading branch information
caarlos0 committed Oct 14, 2022
1 parent a15b7ec commit 2f72048e806664855d2960cdf943a5f2af14bf98
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
@@ -59,3 +59,19 @@ func Clean(output string, err error) (string, error) {
}
return output, err
}

// CleanAllLines returns all the non empty lines of the output, cleaned up.
func CleanAllLines(output string, err error) ([]string, error) {
var result []string
for _, line := range strings.Split(output, "\n") {
l := strings.TrimSpace(strings.ReplaceAll(line, "'", ""))
if l == "" {
continue
}
result = append(result, l)
}
if err != nil {
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
}
return result, err
}
@@ -36,10 +36,15 @@ func TestGitWarning(t *testing.T) {
testlib.GitCommit(t, "foobar")
testlib.GitBranch(t, "tags/1.2.3")
testlib.GitTag(t, "1.2.3")
testlib.GitTag(t, "nightly")

out, err := git.Run(ctx, "describe", "--tags", "--abbrev=0", "tags/1.2.3^")
require.NoError(t, err)
require.Equal(t, "1.2.2\n", out)

tags, err := git.CleanAllLines(git.Run(ctx, "tag", "--points-at", "HEAD", "--sort", "-version:refname"))
require.NoError(t, err)
require.ElementsMatch(t, []string{"1.2.3", "nightly"}, tags)
}

func TestRepo(t *testing.T) {
@@ -52,16 +57,32 @@ func TestRepo(t *testing.T) {

func TestClean(t *testing.T) {
ctx := context.Background()
out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
require.NoError(t, err)
require.Equal(t, "asdasd ssadas", out)

out, err = git.Clean(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
t.Run("success", func(t *testing.T) {
out, err := git.Clean("asdasd 'ssadas'\nadasd", nil)
require.NoError(t, err)
require.Equal(t, "asdasd ssadas", out)
})

t.Run("error", func(t *testing.T) {
out, err := git.Clean(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
})

t.Run("all lines error", func(t *testing.T) {
out, err := git.CleanAllLines(git.Run(ctx, "command-that-dont-exist"))
require.Error(t, err)
require.Empty(t, out)
require.Equal(
t,
"git: 'command-that-dont-exist' is not a git command. See 'git --help'.",
err.Error(),
)
})
}

0 comments on commit 2f72048

Please sign in to comment.