diff --git a/pipeline/git/fixtures/good-repo b/pipeline/git/fixtures/good-repo new file mode 160000 index 000000000..593de7f02 --- /dev/null +++ b/pipeline/git/fixtures/good-repo @@ -0,0 +1 @@ +Subproject commit 593de7f025f3817cc0a56bb11f5a6f0131c67452 diff --git a/pipeline/git/fixtures/invalid-tag-format-repo b/pipeline/git/fixtures/invalid-tag-format-repo new file mode 160000 index 000000000..7cafca4c3 --- /dev/null +++ b/pipeline/git/fixtures/invalid-tag-format-repo @@ -0,0 +1 @@ +Subproject commit 7cafca4c382e2d83b123281bb31dd7b4f0e19a8b diff --git a/pipeline/git/fixtures/new-repo/.gitkeep b/pipeline/git/fixtures/new-repo/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pipeline/git/fixtures/single-commit-no-tags-repo b/pipeline/git/fixtures/single-commit-no-tags-repo new file mode 160000 index 000000000..211cca43d --- /dev/null +++ b/pipeline/git/fixtures/single-commit-no-tags-repo @@ -0,0 +1 @@ +Subproject commit 211cca43da0ebbe5109c1cf09bee3ea0bb0bf04f diff --git a/pipeline/git/fixtures/single-commit-repo b/pipeline/git/fixtures/single-commit-repo new file mode 160000 index 000000000..4bf27bfd0 --- /dev/null +++ b/pipeline/git/fixtures/single-commit-repo @@ -0,0 +1 @@ +Subproject commit 4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 860a05244..f38553d13 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -3,6 +3,7 @@ package git import ( + "log" "os" "regexp" "strings" @@ -37,6 +38,7 @@ func (p Pipe) Run(ctx *context.Context) (err error) { } func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { + log.Println("git:", pwd) tag, err := cleanGit(pwd, "describe", "--tags", "--abbrev=0", "--always") if err != nil { return @@ -56,16 +58,16 @@ func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { PreviousTag: prev, Diff: log, } - // removes usual `v` prefix - ctx.Version = strings.TrimPrefix(tag, "v") - if versionErr := isVersionValid(ctx.Version); versionErr != nil { - return versionErr - } commit, err := cleanGit(pwd, "show", "--format='%H'", "HEAD") if err != nil { return } ctx.Git.Commit = commit + // removes usual `v` prefix + ctx.Version = strings.TrimPrefix(tag, "v") + if versionErr := isVersionValid(ctx.Version); versionErr != nil { + return versionErr + } return } diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 229f8469f..e30dd89ca 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,8 +1,8 @@ package git import ( - "io/ioutil" - "os/exec" + "os" + "path/filepath" "testing" "github.com/goreleaser/goreleaser/config" @@ -14,7 +14,7 @@ func TestDescription(t *testing.T) { assert.NotEmpty(t, Pipe{}.Description()) } -func TestValidVersion(t *testing.T) { +func TestRunPipe(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ @@ -26,49 +26,86 @@ func TestValidVersion(t *testing.T) { assert.NotEmpty(ctx.Git.Diff) } -func TestNotAGitFolder(t *testing.T) { +func TestGoodRepo(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) + var ctx = &context.Context{ Config: config.Project{}, } - assert.Error(Pipe{}.doRun(ctx, folder)) + assert.NoError(Pipe{}.doRun(ctx, getFixture("good-repo"))) + assert.Equal( + context.GitInfo{ + CurrentTag: "v0.0.2", + PreviousTag: "v0.0.1", + Diff: "593de7f commit5\nf365005 commit4\n3eb6c7b commit3\n", + Commit: "593de7f025f3817cc0a56bb11f5a6f0131c67452", + }, + ctx.Git, + ) +} + +func TestSingleCommitNoTags(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.NoError(Pipe{}.doRun(ctx, getFixture("single-commit-no-tags-repo"))) + assert.Equal( + context.GitInfo{ + CurrentTag: "211cca43da0ebbe5109c1cf09bee3ea0bb0bf04f", + PreviousTag: "211cca43da0ebbe5109c1cf09bee3ea0bb0bf04f", + Commit: "211cca43da0ebbe5109c1cf09bee3ea0bb0bf04f", + }, + ctx.Git, + ) } func TestSingleCommit(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) - assert.NoError(exec.Command("git", "-C", folder, "init").Run()) - assert.NoError(exec.Command("git", "-C", folder, "commit", "--allow-empty", "-m", "asd").Run()) - assert.NoError(exec.Command("git", "-C", folder, "tag", "v0.0.1").Run()) var ctx = &context.Context{ Config: config.Project{}, } - assert.NoError(Pipe{}.doRun(ctx, folder)) + assert.NoError(Pipe{}.doRun(ctx, getFixture("single-commit-repo"))) + assert.Equal( + context.GitInfo{ + CurrentTag: "v0.0.1", + PreviousTag: "4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe", + Commit: "4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe", + }, + ctx.Git, + ) } func TestNewRepository(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) - assert.NoError(exec.Command("git", "-C", folder, "init").Run()) var ctx = &context.Context{ Config: config.Project{}, } - assert.Error(Pipe{}.doRun(ctx, folder)) + assert.Error(Pipe{}.doRun(ctx, getFixture("new-repo"))) + assert.Equal(context.GitInfo{}, ctx.Git) } func TestInvalidTagFormat(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) - assert.NoError(exec.Command("git", "-C", folder, "init").Run()) - assert.NoError(exec.Command("git", "-C", folder, "commit", "--allow-empty", "-m", "asd").Run()) - assert.NoError(exec.Command("git", "-C", folder, "tag", "sadasd").Run()) var ctx = &context.Context{ Config: config.Project{}, } - assert.EqualError(Pipe{}.doRun(ctx, folder), "sadasd is not in a valid version format") + assert.EqualError( + Pipe{}.doRun(ctx, getFixture("invalid-tag-format-repo")), + "invalid-tag-name is not in a valid version format", + ) + assert.Equal( + context.GitInfo{ + CurrentTag: "invalid-tag-name", + PreviousTag: "v0.0.1", + Diff: "7cafca4 commit5\n1781c0e commit4\n633c559 commit3\n", + Commit: "7cafca4c382e2d83b123281bb31dd7b4f0e19a8b", + }, + ctx.Git, + ) +} + +func getFixture(name string) string { + wd, _ := os.Getwd() + return filepath.Join(wd, "fixtures", name) }