From fb9d49db34b774429bdcfd0403c6b004bd8749cf Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 12:18:00 -0300 Subject: [PATCH 01/20] more checksums tests --- pipeline/checksums/checksums_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 44e7dcb13..77b26d12c 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -45,3 +45,16 @@ func TestPipeFileNotExist(t *testing.T) { ctx.AddArtifact("nope") assert.Error(Pipe{}.Run(ctx)) } + +func TestPipeFileCantBeWritten(t *testing.T) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) + var ctx = &context.Context{ + Config: config.Project{ + Dist: folder, + }, + } + ctx.AddArtifact("nope") + assert.Error(Pipe{}.Run(ctx)) +} From 7b1bc7ce701001df5fac9db8ce0eb37517e317b3 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 12:18:09 -0300 Subject: [PATCH 02/20] more release tests --- pipeline/release/release_test.go | 69 ++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/pipeline/release/release_test.go b/pipeline/release/release_test.go index 3441735b7..e317ec828 100644 --- a/pipeline/release/release_test.go +++ b/pipeline/release/release_test.go @@ -2,6 +2,7 @@ package release import ( "bytes" + "errors" "io/ioutil" "os" "path/filepath" @@ -18,7 +19,7 @@ func TestPipeDescription(t *testing.T) { } func TestRunPipe(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) folder, err := ioutil.TempDir("", "gorelasertest") assert.NoError(err) tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz")) @@ -47,8 +48,31 @@ func TestRunPipe(t *testing.T) { assert.True(client.UploadedFile) } +func TestRunPipeReleaseCreationFailed(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Git: context.GitInfo{ + CurrentTag: "v1.0.0", + }, + Config: config.Project{ + Release: config.Release{ + GitHub: config.Repo{ + Owner: "test", + Name: "test", + }, + }, + }, + } + client := &DummyClient{ + FailToCreateRelease: true, + } + assert.Error(doRun(ctx, client)) + assert.False(client.CreatedRelease) + assert.False(client.UploadedFile) +} + func TestRunPipeWithFileThatDontExist(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) var ctx = &context.Context{ Git: context.GitInfo{ CurrentTag: "v1.0.0", @@ -69,9 +93,40 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) { assert.False(client.UploadedFile) } +func TestRunPipeUploadFailure(t *testing.T) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) + tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz")) + assert.NoError(err) + var ctx = &context.Context{ + Git: context.GitInfo{ + CurrentTag: "v1.0.0", + }, + Config: config.Project{ + Dist: folder, + Release: config.Release{ + GitHub: config.Repo{ + Owner: "test", + Name: "test", + }, + }, + }, + } + ctx.AddArtifact(tarfile.Name()) + client := &DummyClient{ + FailToUpload: true, + } + assert.Error(doRun(ctx, client)) + assert.True(client.CreatedRelease) + assert.False(client.UploadedFile) +} + type DummyClient struct { - CreatedRelease bool - UploadedFile bool + FailToCreateRelease bool + FailToUpload bool + CreatedRelease bool + UploadedFile bool } func (client *DummyClient) GetInfo(ctx *context.Context) (info client.Info, err error) { @@ -79,6 +134,9 @@ func (client *DummyClient) GetInfo(ctx *context.Context) (info client.Info, err } func (client *DummyClient) CreateRelease(ctx *context.Context) (releaseID int, err error) { + if client.FailToCreateRelease { + return 0, errors.New("release failed") + } client.CreatedRelease = true return } @@ -88,6 +146,9 @@ func (client *DummyClient) CreateFile(ctx *context.Context, content bytes.Buffer } func (client *DummyClient) Upload(ctx *context.Context, releaseID int, name string, file *os.File) (err error) { + if client.FailToUpload { + return errors.New("upload failed") + } client.UploadedFile = true return } From ceb5c1162c0661d5b9996b7fc96509f6dfcca00a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 13:05:54 -0300 Subject: [PATCH 03/20] improved git coverage, code, and function --- pipeline/git/commit.go | 21 ------------ pipeline/git/commit_test.go | 15 --------- pipeline/git/exec.go | 21 ++++++++++++ pipeline/git/git.go | 32 ++++++++++++++----- pipeline/git/git_test.go | 64 ++++++++++++++++++++++++++++++++++++- pipeline/git/log.go | 18 ----------- pipeline/git/log_test.go | 25 --------------- pipeline/git/tag.go | 33 ------------------- pipeline/git/tag_test.go | 30 ----------------- 9 files changed, 108 insertions(+), 151 deletions(-) delete mode 100644 pipeline/git/commit.go delete mode 100644 pipeline/git/commit_test.go create mode 100644 pipeline/git/exec.go delete mode 100644 pipeline/git/log.go delete mode 100644 pipeline/git/log_test.go delete mode 100644 pipeline/git/tag.go delete mode 100644 pipeline/git/tag_test.go diff --git a/pipeline/git/commit.go b/pipeline/git/commit.go deleted file mode 100644 index f0172bc40..000000000 --- a/pipeline/git/commit.go +++ /dev/null @@ -1,21 +0,0 @@ -package git - -import ( - "errors" - "os/exec" - "strings" -) - -func commitHash() (string, error) { - cmd := exec.Command( - "git", - "show", - "--format='%H'", - "HEAD", - ) - bts, err := cmd.CombinedOutput() - if err != nil { - return "", errors.New(err.Error() + ": " + string(bts)) - } - return strings.Replace(strings.Split(string(bts), "\n")[0], "'", "", -1), err -} diff --git a/pipeline/git/commit_test.go b/pipeline/git/commit_test.go deleted file mode 100644 index b46ef3542..000000000 --- a/pipeline/git/commit_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package git - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCommit(t *testing.T) { - assert := assert.New(t) - commit, err := commitHash() - assert.NoError(err) - assert.NotEmpty(commit) - assert.NotContains(commit, "'") -} diff --git a/pipeline/git/exec.go b/pipeline/git/exec.go new file mode 100644 index 000000000..b89a9b052 --- /dev/null +++ b/pipeline/git/exec.go @@ -0,0 +1,21 @@ +package git + +import ( + "errors" + "os/exec" + "strings" +) + +func git(args ...string) (output string, err error) { + var cmd = exec.Command("git", args...) + bts, err := cmd.CombinedOutput() + if err != nil { + return "", errors.New(string(bts)) + } + return string(bts), err +} + +func cleanGit(args ...string) (output string, err error) { + output, err = git(args...) + return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err +} diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 9961e3a12..d6ee8c76f 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -28,34 +28,50 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) (err error) { - tag, err := currentTag() + tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always") if err != nil { return } - previous, err := previousTag(tag) + prev, err := previous(tag) if err != nil { return } - log, err := log(previous, tag) + + log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) if err != nil { return } ctx.Git = context.GitInfo{ CurrentTag: tag, - PreviousTag: previous, + PreviousTag: prev, Diff: log, } // removes usual `v` prefix ctx.Version = strings.TrimPrefix(tag, "v") - matches, err := regexp.MatchString("^[0-9.]+", ctx.Version) - if err != nil || !matches { - return ErrInvalidVersionFormat{ctx.Version} + if versionErr := isVersionValid(ctx.Version); versionErr != nil { + return versionErr } - commit, err := commitHash() + commit, err := cleanGit("show", "--format='%H'", "HEAD") if err != nil { return } ctx.Git.Commit = commit return } + +func previous(tag string) (previous string, err error) { + previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^") + if err != nil { + previous, err = cleanGit("rev-list", "--max-parents=0", "HEAD") + } + return +} + +func isVersionValid(version string) error { + matches, err := regexp.MatchString("^[0-9.]+", version) + if err != nil || !matches { + return ErrInvalidVersionFormat{version} + } + return nil +} diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index dcefa4da0..3e07192de 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,6 +1,9 @@ package git import ( + "io/ioutil" + "os" + "os/exec" "testing" "github.com/goreleaser/goreleaser/config" @@ -13,7 +16,7 @@ func TestDescription(t *testing.T) { } func TestValidVersion(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{}, @@ -23,3 +26,62 @@ func TestValidVersion(t *testing.T) { assert.NotEmpty(ctx.Git.PreviousTag) assert.NotEmpty(ctx.Git.Diff) } + +func TestNotAGitFolder(t *testing.T) { + var assert = assert.New(t) + _, back := createAndChdir(t) + defer back() + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.Error(Pipe{}.Run(ctx)) +} + +func TestSingleCommit(t *testing.T) { + var assert = assert.New(t) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "tag", "v0.0.1").Run()) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.NoError(Pipe{}.Run(ctx)) +} + +func TestNewRepository(t *testing.T) { + var assert = assert.New(t) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.Error(Pipe{}.Run(ctx)) +} + +func TestInvalidTagFormat(t *testing.T) { + var assert = assert.New(t) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "tag", "sadasd").Run()) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format") +} + +func createAndChdir(t *testing.T) (current string, back func()) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) + previous, err := os.Getwd() + assert.NoError(err) + assert.NoError(os.Chdir(folder)) + return folder, func() { + assert.NoError(os.Chdir(previous)) + } +} diff --git a/pipeline/git/log.go b/pipeline/git/log.go deleted file mode 100644 index b4d4feeed..000000000 --- a/pipeline/git/log.go +++ /dev/null @@ -1,18 +0,0 @@ -package git - -import "os/exec" - -func log(previous, current string) (str string, err error) { - cmd := exec.Command( - "git", - "log", - "--pretty=oneline", - "--abbrev-commit", - previous+".."+current, - ) - bts, err := cmd.CombinedOutput() - if err != nil { - return str, err - } - return string(bts), err -} diff --git a/pipeline/git/log_test.go b/pipeline/git/log_test.go deleted file mode 100644 index 818bb337a..000000000 --- a/pipeline/git/log_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package git - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestLog(t *testing.T) { - assert := assert.New(t) - tag, err := currentTag() - assert.NoError(err) - tagb, err := previousTag(tag) - assert.NoError(err) - log, err := log(tagb, tag) - assert.NoError(err) - assert.NotEmpty(log) -} - -func TestLogInvalidRef(t *testing.T) { - assert := assert.New(t) - log, err := log("wtfff", "nope") - assert.Error(err) - assert.Empty(log) -} diff --git a/pipeline/git/tag.go b/pipeline/git/tag.go deleted file mode 100644 index d856c959d..000000000 --- a/pipeline/git/tag.go +++ /dev/null @@ -1,33 +0,0 @@ -package git - -import ( - "errors" - "os/exec" - "strings" -) - -func currentTag() (tag string, err error) { - return getTag("") -} - -func previousTag(base string) (tag string, err error) { - return getTag(base + "^") -} - -func getTag(ref string) (tag string, err error) { - cmd := exec.Command( - "git", - "describe", - "--tags", - "--abbrev=0", - "--always", - ) - if ref != "" { - cmd.Args = append(cmd.Args, ref) - } - bts, err := cmd.CombinedOutput() - if err != nil { - return tag, errors.New(err.Error() + ": " + string(bts)) - } - return strings.Split(string(bts), "\n")[0], err -} diff --git a/pipeline/git/tag_test.go b/pipeline/git/tag_test.go deleted file mode 100644 index 6d8ecd2f7..000000000 --- a/pipeline/git/tag_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package git - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCurrentTag(t *testing.T) { - assert := assert.New(t) - tag, err := currentTag() - assert.NoError(err) - assert.NotEmpty(tag) -} - -func TestPreviousTag(t *testing.T) { - assert := assert.New(t) - tag, err := currentTag() - assert.NoError(err) - tag, err = previousTag(tag) - assert.NoError(err) - assert.NotEmpty(tag) -} - -func TestInvalidRef(t *testing.T) { - assert := assert.New(t) - tag, err := previousTag("this-should-not-exist") - assert.Error(err) - assert.Empty(tag) -} From c15e8c810818e3512e9992fd15a7ba3bb15e5837 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 13:14:01 -0300 Subject: [PATCH 04/20] logs for travis debugging --- pipeline/git/git_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 3e07192de..a8500bbe3 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,6 +1,7 @@ package git import ( + "fmt" "io/ioutil" "os" "os/exec" @@ -81,7 +82,9 @@ func createAndChdir(t *testing.T) (current string, back func()) { previous, err := os.Getwd() assert.NoError(err) assert.NoError(os.Chdir(folder)) + fmt.Println("Changed dir to", folder) return folder, func() { assert.NoError(os.Chdir(previous)) + fmt.Println("Changed dir to", previous) } } From 36efad3f788bf069c568597f731cfab0e1a0e0a0 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 13:36:48 -0300 Subject: [PATCH 05/20] avoid cd-ing in tests --- pipeline/git/exec.go | 10 +++++--- pipeline/git/git.go | 25 +++++++++++++------ pipeline/git/git_test.go | 54 ++++++++++++++-------------------------- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/pipeline/git/exec.go b/pipeline/git/exec.go index b89a9b052..cbeeaa23f 100644 --- a/pipeline/git/exec.go +++ b/pipeline/git/exec.go @@ -6,8 +6,10 @@ import ( "strings" ) -func git(args ...string) (output string, err error) { - var cmd = exec.Command("git", args...) +func git(pwd string, args ...string) (output string, err error) { + var allArgs = []string{"-C", pwd} + allArgs = append(allArgs, args...) + var cmd = exec.Command("git", allArgs...) bts, err := cmd.CombinedOutput() if err != nil { return "", errors.New(string(bts)) @@ -15,7 +17,7 @@ func git(args ...string) (output string, err error) { return string(bts), err } -func cleanGit(args ...string) (output string, err error) { - output, err = git(args...) +func cleanGit(pwd string, args ...string) (output string, err error) { + output, err = git(pwd, args...) return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err } diff --git a/pipeline/git/git.go b/pipeline/git/git.go index d6ee8c76f..860a05244 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -3,6 +3,7 @@ package git import ( + "os" "regexp" "strings" @@ -27,17 +28,25 @@ func (Pipe) Description() string { } // Run the pipe -func (Pipe) Run(ctx *context.Context) (err error) { - tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always") +func (p Pipe) Run(ctx *context.Context) (err error) { + folder, err := os.Getwd() + if err != nil { + return err + } + return p.doRun(ctx, folder) +} + +func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { + tag, err := cleanGit(pwd, "describe", "--tags", "--abbrev=0", "--always") if err != nil { return } - prev, err := previous(tag) + prev, err := previous(pwd, tag) if err != nil { return } - log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) + log, err := git(pwd, "log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) if err != nil { return } @@ -52,7 +61,7 @@ func (Pipe) Run(ctx *context.Context) (err error) { if versionErr := isVersionValid(ctx.Version); versionErr != nil { return versionErr } - commit, err := cleanGit("show", "--format='%H'", "HEAD") + commit, err := cleanGit(pwd, "show", "--format='%H'", "HEAD") if err != nil { return } @@ -60,10 +69,10 @@ func (Pipe) Run(ctx *context.Context) (err error) { return } -func previous(tag string) (previous string, err error) { - previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^") +func previous(pwd, tag string) (previous string, err error) { + previous, err = cleanGit(pwd, "describe", "--tags", "--abbrev=0", "--always", tag+"^") if err != nil { - previous, err = cleanGit("rev-list", "--max-parents=0", "HEAD") + previous, err = cleanGit(pwd, "rev-list", "--max-parents=0", "HEAD") } return } diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index a8500bbe3..229f8469f 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,9 +1,7 @@ package git import ( - "fmt" "io/ioutil" - "os" "os/exec" "testing" @@ -30,61 +28,47 @@ func TestValidVersion(t *testing.T) { func TestNotAGitFolder(t *testing.T) { var assert = assert.New(t) - _, back := createAndChdir(t) - defer back() + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) var ctx = &context.Context{ Config: config.Project{}, } - assert.Error(Pipe{}.Run(ctx)) + assert.Error(Pipe{}.doRun(ctx, folder)) } func TestSingleCommit(t *testing.T) { var assert = assert.New(t) - _, back := createAndChdir(t) - defer back() - assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) - assert.NoError(exec.Command("git", "tag", "v0.0.1").Run()) + 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{}.Run(ctx)) + assert.NoError(Pipe{}.doRun(ctx, folder)) } func TestNewRepository(t *testing.T) { var assert = assert.New(t) - _, back := createAndChdir(t) - defer back() - assert.NoError(exec.Command("git", "init").Run()) + 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{}.Run(ctx)) + assert.Error(Pipe{}.doRun(ctx, folder)) } func TestInvalidTagFormat(t *testing.T) { var assert = assert.New(t) - _, back := createAndChdir(t) - defer back() - assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) - assert.NoError(exec.Command("git", "tag", "sadasd").Run()) + 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{}.Run(ctx), "sadasd is not in a valid version format") -} - -func createAndChdir(t *testing.T) (current string, back func()) { - var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) - previous, err := os.Getwd() - assert.NoError(err) - assert.NoError(os.Chdir(folder)) - fmt.Println("Changed dir to", folder) - return folder, func() { - assert.NoError(os.Chdir(previous)) - fmt.Println("Changed dir to", previous) - } + assert.EqualError(Pipe{}.doRun(ctx, folder), "sadasd is not in a valid version format") } From 0b7936eb0d8738ae5acacfad892b6aedad300be5 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 13:58:18 -0300 Subject: [PATCH 06/20] trying with test fixtures --- pipeline/git/fixtures/good-repo | 1 + pipeline/git/fixtures/invalid-tag-format-repo | 1 + pipeline/git/fixtures/new-repo/.gitkeep | 0 .../git/fixtures/single-commit-no-tags-repo | 1 + pipeline/git/fixtures/single-commit-repo | 1 + pipeline/git/git.go | 12 +-- pipeline/git/git_test.go | 83 ++++++++++++++----- 7 files changed, 71 insertions(+), 28 deletions(-) create mode 160000 pipeline/git/fixtures/good-repo create mode 160000 pipeline/git/fixtures/invalid-tag-format-repo create mode 100644 pipeline/git/fixtures/new-repo/.gitkeep create mode 160000 pipeline/git/fixtures/single-commit-no-tags-repo create mode 160000 pipeline/git/fixtures/single-commit-repo 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) } From e39dfa1ce9d287ae6e22e833fa61c63484a46f4d Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:00:29 -0300 Subject: [PATCH 07/20] Revert "trying with test fixtures" This reverts commit 0b7936eb0d8738ae5acacfad892b6aedad300be5. --- pipeline/git/fixtures/good-repo | 1 - pipeline/git/fixtures/invalid-tag-format-repo | 1 - pipeline/git/fixtures/new-repo/.gitkeep | 0 .../git/fixtures/single-commit-no-tags-repo | 1 - pipeline/git/fixtures/single-commit-repo | 1 - pipeline/git/git.go | 12 ++- pipeline/git/git_test.go | 83 +++++-------------- 7 files changed, 28 insertions(+), 71 deletions(-) delete mode 160000 pipeline/git/fixtures/good-repo delete mode 160000 pipeline/git/fixtures/invalid-tag-format-repo delete mode 100644 pipeline/git/fixtures/new-repo/.gitkeep delete mode 160000 pipeline/git/fixtures/single-commit-no-tags-repo delete mode 160000 pipeline/git/fixtures/single-commit-repo diff --git a/pipeline/git/fixtures/good-repo b/pipeline/git/fixtures/good-repo deleted file mode 160000 index 593de7f02..000000000 --- a/pipeline/git/fixtures/good-repo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 593de7f025f3817cc0a56bb11f5a6f0131c67452 diff --git a/pipeline/git/fixtures/invalid-tag-format-repo b/pipeline/git/fixtures/invalid-tag-format-repo deleted file mode 160000 index 7cafca4c3..000000000 --- a/pipeline/git/fixtures/invalid-tag-format-repo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7cafca4c382e2d83b123281bb31dd7b4f0e19a8b diff --git a/pipeline/git/fixtures/new-repo/.gitkeep b/pipeline/git/fixtures/new-repo/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/pipeline/git/fixtures/single-commit-no-tags-repo b/pipeline/git/fixtures/single-commit-no-tags-repo deleted file mode 160000 index 211cca43d..000000000 --- a/pipeline/git/fixtures/single-commit-no-tags-repo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 211cca43da0ebbe5109c1cf09bee3ea0bb0bf04f diff --git a/pipeline/git/fixtures/single-commit-repo b/pipeline/git/fixtures/single-commit-repo deleted file mode 160000 index 4bf27bfd0..000000000 --- a/pipeline/git/fixtures/single-commit-repo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe diff --git a/pipeline/git/git.go b/pipeline/git/git.go index f38553d13..860a05244 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -3,7 +3,6 @@ package git import ( - "log" "os" "regexp" "strings" @@ -38,7 +37,6 @@ 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 @@ -58,16 +56,16 @@ func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { PreviousTag: prev, Diff: log, } - 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 } + commit, err := cleanGit(pwd, "show", "--format='%H'", "HEAD") + if err != nil { + return + } + ctx.Git.Commit = commit return } diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index e30dd89ca..229f8469f 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,8 +1,8 @@ package git import ( - "os" - "path/filepath" + "io/ioutil" + "os/exec" "testing" "github.com/goreleaser/goreleaser/config" @@ -14,7 +14,7 @@ func TestDescription(t *testing.T) { assert.NotEmpty(t, Pipe{}.Description()) } -func TestRunPipe(t *testing.T) { +func TestValidVersion(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ @@ -26,86 +26,49 @@ func TestRunPipe(t *testing.T) { assert.NotEmpty(ctx.Git.Diff) } -func TestGoodRepo(t *testing.T) { +func TestNotAGitFolder(t *testing.T) { var assert = assert.New(t) - + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) var ctx = &context.Context{ Config: config.Project{}, } - 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, - ) + assert.Error(Pipe{}.doRun(ctx, folder)) } 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, getFixture("single-commit-repo"))) - assert.Equal( - context.GitInfo{ - CurrentTag: "v0.0.1", - PreviousTag: "4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe", - Commit: "4bf27bfd08049ae6187cefa5e9d50e2e0f205ebe", - }, - ctx.Git, - ) + assert.NoError(Pipe{}.doRun(ctx, folder)) } 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, getFixture("new-repo"))) - assert.Equal(context.GitInfo{}, ctx.Git) + assert.Error(Pipe{}.doRun(ctx, folder)) } 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, 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) + assert.EqualError(Pipe{}.doRun(ctx, folder), "sadasd is not in a valid version format") } From bf9f0e0b14fe4921ac07a1baa8c63908fb2a07af Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:00:49 -0300 Subject: [PATCH 08/20] Revert "avoid cd-ing in tests" This reverts commit 36efad3f788bf069c568597f731cfab0e1a0e0a0. --- pipeline/git/exec.go | 10 +++----- pipeline/git/git.go | 25 ++++++------------- pipeline/git/git_test.go | 54 ++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/pipeline/git/exec.go b/pipeline/git/exec.go index cbeeaa23f..b89a9b052 100644 --- a/pipeline/git/exec.go +++ b/pipeline/git/exec.go @@ -6,10 +6,8 @@ import ( "strings" ) -func git(pwd string, args ...string) (output string, err error) { - var allArgs = []string{"-C", pwd} - allArgs = append(allArgs, args...) - var cmd = exec.Command("git", allArgs...) +func git(args ...string) (output string, err error) { + var cmd = exec.Command("git", args...) bts, err := cmd.CombinedOutput() if err != nil { return "", errors.New(string(bts)) @@ -17,7 +15,7 @@ func git(pwd string, args ...string) (output string, err error) { return string(bts), err } -func cleanGit(pwd string, args ...string) (output string, err error) { - output, err = git(pwd, args...) +func cleanGit(args ...string) (output string, err error) { + output, err = git(args...) return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err } diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 860a05244..d6ee8c76f 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -3,7 +3,6 @@ package git import ( - "os" "regexp" "strings" @@ -28,25 +27,17 @@ func (Pipe) Description() string { } // Run the pipe -func (p Pipe) Run(ctx *context.Context) (err error) { - folder, err := os.Getwd() - if err != nil { - return err - } - return p.doRun(ctx, folder) -} - -func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { - tag, err := cleanGit(pwd, "describe", "--tags", "--abbrev=0", "--always") +func (Pipe) Run(ctx *context.Context) (err error) { + tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always") if err != nil { return } - prev, err := previous(pwd, tag) + prev, err := previous(tag) if err != nil { return } - log, err := git(pwd, "log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) + log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) if err != nil { return } @@ -61,7 +52,7 @@ func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { if versionErr := isVersionValid(ctx.Version); versionErr != nil { return versionErr } - commit, err := cleanGit(pwd, "show", "--format='%H'", "HEAD") + commit, err := cleanGit("show", "--format='%H'", "HEAD") if err != nil { return } @@ -69,10 +60,10 @@ func (Pipe) doRun(ctx *context.Context, pwd string) (err error) { return } -func previous(pwd, tag string) (previous string, err error) { - previous, err = cleanGit(pwd, "describe", "--tags", "--abbrev=0", "--always", tag+"^") +func previous(tag string) (previous string, err error) { + previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^") if err != nil { - previous, err = cleanGit(pwd, "rev-list", "--max-parents=0", "HEAD") + previous, err = cleanGit("rev-list", "--max-parents=0", "HEAD") } return } diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 229f8469f..a8500bbe3 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,7 +1,9 @@ package git import ( + "fmt" "io/ioutil" + "os" "os/exec" "testing" @@ -28,47 +30,61 @@ func TestValidVersion(t *testing.T) { func TestNotAGitFolder(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") - assert.NoError(err) + _, back := createAndChdir(t) + defer back() var ctx = &context.Context{ Config: config.Project{}, } - assert.Error(Pipe{}.doRun(ctx, folder)) + assert.Error(Pipe{}.Run(ctx)) } 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()) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "tag", "v0.0.1").Run()) var ctx = &context.Context{ Config: config.Project{}, } - assert.NoError(Pipe{}.doRun(ctx, folder)) + assert.NoError(Pipe{}.Run(ctx)) } 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()) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) var ctx = &context.Context{ Config: config.Project{}, } - assert.Error(Pipe{}.doRun(ctx, folder)) + assert.Error(Pipe{}.Run(ctx)) } 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()) + _, back := createAndChdir(t) + defer back() + assert.NoError(exec.Command("git", "init").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "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{}.Run(ctx), "sadasd is not in a valid version format") +} + +func createAndChdir(t *testing.T) (current string, back func()) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "gorelasertest") + assert.NoError(err) + previous, err := os.Getwd() + assert.NoError(err) + assert.NoError(os.Chdir(folder)) + fmt.Println("Changed dir to", folder) + return folder, func() { + assert.NoError(os.Chdir(previous)) + fmt.Println("Changed dir to", previous) + } } From e48434e8e0c1577b8b90ff082f7105886d16fbf5 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:00:58 -0300 Subject: [PATCH 09/20] Revert "logs for travis debugging" This reverts commit c15e8c810818e3512e9992fd15a7ba3bb15e5837. --- pipeline/git/git_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index a8500bbe3..3e07192de 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,7 +1,6 @@ package git import ( - "fmt" "io/ioutil" "os" "os/exec" @@ -82,9 +81,7 @@ func createAndChdir(t *testing.T) (current string, back func()) { previous, err := os.Getwd() assert.NoError(err) assert.NoError(os.Chdir(folder)) - fmt.Println("Changed dir to", folder) return folder, func() { assert.NoError(os.Chdir(previous)) - fmt.Println("Changed dir to", previous) } } From 9904be55a995e462b76fa6ff01216fa243db6271 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:03:50 -0300 Subject: [PATCH 10/20] more asserts --- pipeline/git/git_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 3e07192de..0b97386dc 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -48,6 +48,7 @@ func TestSingleCommit(t *testing.T) { Config: config.Project{}, } assert.NoError(Pipe{}.Run(ctx)) + assert.Equal("v0.0.1", ctx.Git.CurrentTag) } func TestNewRepository(t *testing.T) { @@ -72,6 +73,7 @@ func TestInvalidTagFormat(t *testing.T) { Config: config.Project{}, } assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format") + assert.Equal("sadasd", ctx.Git.CurrentTag) } func createAndChdir(t *testing.T) (current string, back func()) { From 26b8fbe850cd3abd35b4dbe66ea3c23de6a8931e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:05:47 -0300 Subject: [PATCH 11/20] sudo --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 222e54bed..014a75c48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: go go: 1.8 +sudo: true install: make setup script: - make test From cdab9262111a7fef14c4bf4392c66d8dd313052d Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:09:04 -0300 Subject: [PATCH 12/20] oops --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 014a75c48..eb8b5cb9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: 1.8 -sudo: true +sudo: required install: make setup script: - make test From 4c8c51cec4bfe4c6d97aa3dfc5fc955485b0e9de Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:18:17 -0300 Subject: [PATCH 13/20] fixed typo and added more logs --- checksum/checksum_test.go | 2 +- pipeline/brew/brew_test.go | 2 +- pipeline/build/build_test.go | 2 +- pipeline/checksums/checksums_test.go | 6 +++--- pipeline/git/git_test.go | 13 ++++++++++--- pipeline/release/release_test.go | 4 ++-- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/checksum/checksum_test.go b/checksum/checksum_test.go index ef7b0cb29..393f37306 100644 --- a/checksum/checksum_test.go +++ b/checksum/checksum_test.go @@ -10,7 +10,7 @@ import ( func TestChecksums(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) var file = filepath.Join(folder, "subject") assert.NoError(ioutil.WriteFile(file, []byte("lorem ipsum"), 0644)) diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index bd79b3897..da9f10049 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -89,7 +89,7 @@ func TestFormulaeSimple(t *testing.T) { func TestRunPipe(t *testing.T) { assert := assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) _, err = os.Create(filepath.Join(folder, "bin.tar.gz")) assert.NoError(err) diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index e48083ee4..64b809ea6 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -40,7 +40,7 @@ func TestBuild(t *testing.T) { func TestRunFullPipe(t *testing.T) { assert := assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) var binary = filepath.Join(folder, "testing") var pre = filepath.Join(folder, "pre") diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 77b26d12c..27c9d8d7a 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -16,7 +16,7 @@ func TestDescription(t *testing.T) { func TestPipe(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) var file = filepath.Join(folder, "binary") assert.NoError(ioutil.WriteFile(file, []byte("some string"), 0644)) @@ -35,7 +35,7 @@ func TestPipe(t *testing.T) { func TestPipeFileNotExist(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) var ctx = &context.Context{ Config: config.Project{ @@ -48,7 +48,7 @@ func TestPipeFileNotExist(t *testing.T) { func TestPipeFileCantBeWritten(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) var ctx = &context.Context{ Config: config.Project{ diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 0b97386dc..f73302346 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,6 +1,7 @@ package git import ( + "fmt" "io/ioutil" "os" "os/exec" @@ -42,8 +43,11 @@ func TestSingleCommit(t *testing.T) { _, back := createAndChdir(t) defer back() assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "commit1").Run()) assert.NoError(exec.Command("git", "tag", "v0.0.1").Run()) + out, err := git("log") + assert.NoError(err) + fmt.Print("git log:\n", out) var ctx = &context.Context{ Config: config.Project{}, } @@ -67,8 +71,11 @@ func TestInvalidTagFormat(t *testing.T) { _, back := createAndChdir(t) defer back() assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "asd").Run()) + assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "commit2").Run()) assert.NoError(exec.Command("git", "tag", "sadasd").Run()) + out, err := git("log") + assert.NoError(err) + fmt.Print("git log:\n", out) var ctx = &context.Context{ Config: config.Project{}, } @@ -78,7 +85,7 @@ func TestInvalidTagFormat(t *testing.T) { func createAndChdir(t *testing.T) (current string, back func()) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) previous, err := os.Getwd() assert.NoError(err) diff --git a/pipeline/release/release_test.go b/pipeline/release/release_test.go index e317ec828..a23e6fe44 100644 --- a/pipeline/release/release_test.go +++ b/pipeline/release/release_test.go @@ -20,7 +20,7 @@ func TestPipeDescription(t *testing.T) { func TestRunPipe(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz")) assert.NoError(err) @@ -95,7 +95,7 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) { func TestRunPipeUploadFailure(t *testing.T) { var assert = assert.New(t) - folder, err := ioutil.TempDir("", "gorelasertest") + folder, err := ioutil.TempDir("", "goreleasertest") assert.NoError(err) tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz")) assert.NoError(err) From 1af318400b5684bab761a446d724cf775045bd0d Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:31:55 -0300 Subject: [PATCH 14/20] more asserts and logs --- pipeline/git/git_test.go | 56 ++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index f73302346..cd71fb048 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -4,7 +4,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "testing" "github.com/goreleaser/goreleaser/config" @@ -42,12 +41,10 @@ func TestSingleCommit(t *testing.T) { var assert = assert.New(t) _, back := createAndChdir(t) defer back() - assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "commit1").Run()) - assert.NoError(exec.Command("git", "tag", "v0.0.1").Run()) - out, err := git("log") - assert.NoError(err) - fmt.Print("git log:\n", out) + gitInit(t) + gitCommit(t, "commit1") + gitTag(t, "v0.0.1") + gitLog(t) var ctx = &context.Context{ Config: config.Project{}, } @@ -59,7 +56,7 @@ func TestNewRepository(t *testing.T) { var assert = assert.New(t) _, back := createAndChdir(t) defer back() - assert.NoError(exec.Command("git", "init").Run()) + gitInit(t) var ctx = &context.Context{ Config: config.Project{}, } @@ -70,12 +67,10 @@ func TestInvalidTagFormat(t *testing.T) { var assert = assert.New(t) _, back := createAndChdir(t) defer back() - assert.NoError(exec.Command("git", "init").Run()) - assert.NoError(exec.Command("git", "commit", "--allow-empty", "-m", "commit2").Run()) - assert.NoError(exec.Command("git", "tag", "sadasd").Run()) - out, err := git("log") - assert.NoError(err) - fmt.Print("git log:\n", out) + gitInit(t) + gitCommit(t, "commit2") + gitTag(t, "sadasd") + gitLog(t) var ctx = &context.Context{ Config: config.Project{}, } @@ -83,6 +78,10 @@ func TestInvalidTagFormat(t *testing.T) { assert.Equal("sadasd", ctx.Git.CurrentTag) } +// +// helper functions +// + func createAndChdir(t *testing.T) (current string, back func()) { var assert = assert.New(t) folder, err := ioutil.TempDir("", "goreleasertest") @@ -94,3 +93,32 @@ func createAndChdir(t *testing.T) (current string, back func()) { assert.NoError(os.Chdir(previous)) } } + +func gitInit(t *testing.T) { + var assert = assert.New(t) + out, err := git("init") + assert.NoError(err) + assert.Contains(out, "Initialized empty Git repository") +} + +func gitCommit(t *testing.T, msg string) { + var assert = assert.New(t) + out, err := git("commit", "--allow-empty", "-m", msg) + assert.NoError(err) + assert.Contains(out, "master", msg) +} + +func gitTag(t *testing.T, tag string) { + var assert = assert.New(t) + out, err := git("tag", tag) + assert.NoError(err) + assert.Empty(out) +} + +func gitLog(t *testing.T) { + var assert = assert.New(t) + out, err := git("log") + assert.NoError(err) + assert.NotEmpty(out) + fmt.Print("\n\ngit log output:\n", out) +} From c0a310b5268cc9ba25a8fe398b42bb67c9becf4f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:38:54 -0300 Subject: [PATCH 15/20] fake git data --- pipeline/git/git_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index cd71fb048..986afd4b8 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -103,14 +103,14 @@ func gitInit(t *testing.T) { func gitCommit(t *testing.T, msg string) { var assert = assert.New(t) - out, err := git("commit", "--allow-empty", "-m", msg) + out, err := fakeGit("commit", "--allow-empty", "-m", msg) assert.NoError(err) assert.Contains(out, "master", msg) } func gitTag(t *testing.T, tag string) { var assert = assert.New(t) - out, err := git("tag", tag) + out, err := fakeGit("tag", tag) assert.NoError(err) assert.Empty(out) } @@ -122,3 +122,14 @@ func gitLog(t *testing.T) { assert.NotEmpty(out) fmt.Print("\n\ngit log output:\n", out) } + +func fakeGit(args ...string) (string, error) { + var allArgs = []string{ + "-c", + "user.name='GoReleaser'", + "-c", + "user.email='test@goreleaser.github.com'", + } + allArgs = append(allArgs, args...) + return git(allArgs...) +} From 1b03630080030fd2736066bcae1d0816d55e6481 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 14:39:42 -0300 Subject: [PATCH 16/20] removed sudo --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index eb8b5cb9c..222e54bed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: go go: 1.8 -sudo: required install: make setup script: - make test From 6b11beb4ee46ca9c428839b7a861b803c14bbcf5 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 15:26:05 -0300 Subject: [PATCH 17/20] added archive tests --- pipeline/archive/archive_test.go | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pipeline/archive/archive_test.go diff --git a/pipeline/archive/archive_test.go b/pipeline/archive/archive_test.go new file mode 100644 index 000000000..16d3de305 --- /dev/null +++ b/pipeline/archive/archive_test.go @@ -0,0 +1,58 @@ +package archive + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/context" + "github.com/stretchr/testify/assert" +) + +func TestDescription(t *testing.T) { + assert.NotEmpty(t, Pipe{}.Description()) +} + +func TestRunPipe(t *testing.T) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "archivetest") + assert.NoError(err) + current, err := os.Getwd() + assert.NoError(err) + assert.NoError(os.Chdir(folder)) + defer func() { + assert.NoError(os.Chdir(current)) + }() + var dist = filepath.Join(folder, "dist") + assert.NoError(os.Mkdir(dist, 0755)) + assert.NoError(os.Mkdir(filepath.Join(dist, "mybin"), 0755)) + _, err = os.Create(filepath.Join(dist, "mybin", "mybin")) + assert.NoError(err) + readme, err := os.Create(filepath.Join(folder, "README.md")) + assert.NoError(err) + var ctx = &context.Context{ + Archives: map[string]string{ + "darwinamd64": "mybin", + }, + Config: config.Project{ + Dist: dist, + Archive: config.Archive{ + Files: []string{ + "README.md", + }, + }, + }, + } + for _, format := range []string{"tar.gz", "zip"} { + t.Run("Archive format "+format, func(t *testing.T) { + ctx.Config.Archive.Format = format + assert.NoError(Pipe{}.Run(ctx)) + }) + } + t.Run("Removed README", func(t *testing.T) { + assert.NoError(os.Remove(readme.Name())) + assert.Error(Pipe{}.Run(ctx)) + }) +} From 8a5392cdf0ac6153f7d2d8667fab6f6ea3738042 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 15:41:41 -0300 Subject: [PATCH 18/20] added fpm tests --- .travis.yml | 6 ++-- pipeline/fpm/fpm_test.go | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 pipeline/fpm/fpm_test.go diff --git a/.travis.yml b/.travis.yml index 222e54bed..75bd57e4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,13 @@ language: go go: 1.8 -install: make setup +install: + - make setup + - gem install fpm script: - make test after_success: - go get github.com/mattn/goveralls - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken="$COVERALLS_TOKEN" - - test -n "$TRAVIS_TAG" && gem install fpm && go run main.go + - test -n "$TRAVIS_TAG" && go run main.go notifications: email: false diff --git a/pipeline/fpm/fpm_test.go b/pipeline/fpm/fpm_test.go new file mode 100644 index 000000000..ac10c9d16 --- /dev/null +++ b/pipeline/fpm/fpm_test.go @@ -0,0 +1,73 @@ +package fpm + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/context" + "github.com/stretchr/testify/assert" +) + +func TestDescription(t *testing.T) { + assert.NotEmpty(t, Pipe{}.Description()) +} + +func TestRunPipeNoFormats(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Config: config.Project{}, + } + assert.NoError(Pipe{}.Run(ctx)) +} + +func TestRunPipe(t *testing.T) { + var assert = assert.New(t) + folder, err := ioutil.TempDir("", "archivetest") + assert.NoError(err) + var dist = filepath.Join(folder, "dist") + assert.NoError(os.Mkdir(dist, 0755)) + assert.NoError(os.Mkdir(filepath.Join(dist, "mybin"), 0755)) + _, err = os.Create(filepath.Join(dist, "mybin", "mybin")) + assert.NoError(err) + var ctx = &context.Context{ + Archives: map[string]string{ + "linuxamd64": "mybin", + }, + Config: config.Project{ + Dist: dist, + Build: config.Build{ + Goarch: []string{ + "amd64", + "i386", + }, + Binary: "mybin", + }, + FPM: config.FPM{ + Formats: []string{"deb"}, + Dependencies: []string{"make"}, + Conflicts: []string{"git"}, + }, + }, + } + assert.NoError(Pipe{}.Run(ctx)) +} + +func TestNoFPMInPath(t *testing.T) { + var assert = assert.New(t) + var path = os.Getenv("PATH") + defer func() { + assert.NoError(os.Setenv("PATH", path)) + }() + os.Setenv("PATH", "") + var ctx = &context.Context{ + Config: config.Project{ + FPM: config.FPM{ + Formats: []string{"deb"}, + }, + }, + } + assert.EqualError(Pipe{}.Run(ctx), ErrNoFPM.Error()) +} From 0c94ae518a450d32eb1a0137d0853b895e3f5b71 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 16:11:47 -0300 Subject: [PATCH 19/20] moved source pipe into git pipe --- main.go | 6 ++-- pipeline/git/git.go | 30 ++++++++++++++++++-- pipeline/git/git_test.go | 58 ++++++++++++++++++++++++++------------- pipeline/source/source.go | 52 ----------------------------------- 4 files changed, 69 insertions(+), 77 deletions(-) delete mode 100644 pipeline/source/source.go diff --git a/main.go b/main.go index fa44f0c9e..640b04771 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ import ( "github.com/goreleaser/goreleaser/pipeline/fpm" "github.com/goreleaser/goreleaser/pipeline/git" "github.com/goreleaser/goreleaser/pipeline/release" - "github.com/goreleaser/goreleaser/pipeline/source" "github.com/urfave/cli" ) @@ -74,9 +73,8 @@ func pipes(buildOnly bool) []pipeline.Pipe { if !buildOnly { pipes = append( pipes, - git.Pipe{}, // get current tag info - env.Pipe{}, // load and validate environment variables - source.Pipe{}, // validate current git state + git.Pipe{}, // get and validate git repo state + env.Pipe{}, // load and validate environment variables ) } pipes = append( diff --git a/pipeline/git/git.go b/pipeline/git/git.go index d6ee8c76f..1ec81673f 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -18,12 +18,30 @@ func (e ErrInvalidVersionFormat) Error() string { return e.version + " is not in a valid version format" } +// ErrDirty happens when the repo has uncommitted/unstashed changes +type ErrDirty struct { + status string +} + +func (e ErrDirty) Error() string { + return "git is currently in a dirty state:\n" + e.status +} + +// ErrWrongRef happens when the HEAD reference is different from the tag being built +type ErrWrongRef struct { + status string +} + +func (e ErrWrongRef) Error() string { + return e.status +} + // Pipe for brew deployment type Pipe struct{} // Description of the pipe func (Pipe) Description() string { - return "Getting Git info" + return "Getting and validating git state" } // Run the pipe @@ -57,7 +75,15 @@ func (Pipe) Run(ctx *context.Context) (err error) { return } ctx.Git.Commit = commit - return + out, err := git("diff") + if strings.TrimSpace(out) != "" || err != nil { + return ErrDirty{out} + } + _, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag) + if err != nil { + return ErrWrongRef{err.Error()} + } + return nil } func previous(tag string) (previous string, err error) { diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index 986afd4b8..a6b61a362 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -1,9 +1,9 @@ package git import ( - "fmt" "io/ioutil" "os" + "path/filepath" "testing" "github.com/goreleaser/goreleaser/config" @@ -15,18 +15,6 @@ func TestDescription(t *testing.T) { assert.NotEmpty(t, Pipe{}.Description()) } -func TestValidVersion(t *testing.T) { - var assert = assert.New(t) - - var ctx = &context.Context{ - Config: config.Project{}, - } - assert.NoError(Pipe{}.Run(ctx)) - assert.NotEmpty(ctx.Git.CurrentTag) - assert.NotEmpty(ctx.Git.PreviousTag) - assert.NotEmpty(ctx.Git.Diff) -} - func TestNotAGitFolder(t *testing.T) { var assert = assert.New(t) _, back := createAndChdir(t) @@ -44,7 +32,6 @@ func TestSingleCommit(t *testing.T) { gitInit(t) gitCommit(t, "commit1") gitTag(t, "v0.0.1") - gitLog(t) var ctx = &context.Context{ Config: config.Project{}, } @@ -70,7 +57,6 @@ func TestInvalidTagFormat(t *testing.T) { gitInit(t) gitCommit(t, "commit2") gitTag(t, "sadasd") - gitLog(t) var ctx = &context.Context{ Config: config.Project{}, } @@ -78,6 +64,41 @@ func TestInvalidTagFormat(t *testing.T) { assert.Equal("sadasd", ctx.Git.CurrentTag) } +func TestDirty(t *testing.T) { + var assert = assert.New(t) + folder, back := createAndChdir(t) + defer back() + gitInit(t) + dummy, err := os.Create(filepath.Join(folder, "dummy")) + assert.NoError(err) + gitAdd(t) + gitCommit(t, "commit2") + gitTag(t, "v0.0.1") + assert.NoError(ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644)) + var ctx = &context.Context{ + Config: config.Project{}, + } + err = Pipe{}.Run(ctx) + assert.Error(err) + assert.Contains(err.Error(), "git is currently in a dirty state:") +} + +func TestTagIsNotLastCommit(t *testing.T) { + var assert = assert.New(t) + _, back := createAndChdir(t) + defer back() + gitInit(t) + gitCommit(t, "commit3") + gitTag(t, "v0.0.1") + gitCommit(t, "commit4") + var ctx = &context.Context{ + Config: config.Project{}, + } + err := Pipe{}.Run(ctx) + assert.Error(err) + assert.Contains(err.Error(), "fatal: no tag exactly matches") +} + // // helper functions // @@ -115,12 +136,11 @@ func gitTag(t *testing.T, tag string) { assert.Empty(out) } -func gitLog(t *testing.T) { +func gitAdd(t *testing.T) { var assert = assert.New(t) - out, err := git("log") + out, err := git("add", "-A") assert.NoError(err) - assert.NotEmpty(out) - fmt.Print("\n\ngit log output:\n", out) + assert.Empty(out) } func fakeGit(args ...string) (string, error) { diff --git a/pipeline/source/source.go b/pipeline/source/source.go deleted file mode 100644 index e14d6996b..000000000 --- a/pipeline/source/source.go +++ /dev/null @@ -1,52 +0,0 @@ -// Package source provides pipes to take care of validating the current -// git repo state. -// For the releasing process we need the files of the tag we are releasing. -package source - -import ( - "os/exec" - "strings" - - "github.com/goreleaser/goreleaser/context" -) - -// ErrDirty happens when the repo has uncommitted/unstashed changes -type ErrDirty struct { - status string -} - -func (e ErrDirty) Error() string { - return "git is currently in a dirty state:\n" + e.status -} - -// ErrWrongRef happens when the HEAD reference is different from the tag being built -type ErrWrongRef struct { - status string -} - -func (e ErrWrongRef) Error() string { - return e.status -} - -// Pipe to make sure we are in the latest Git tag as source. -type Pipe struct{} - -// Description of the pipe -func (p Pipe) Description() string { - return "Validating current git state" -} - -// Run errors we the repo is dirty or if the current ref is different from the -// tag ref -func (p Pipe) Run(ctx *context.Context) error { - bts, err := exec.Command("git", "diff").CombinedOutput() - if err != nil || strings.TrimSpace(string(bts)) != "" { - return ErrDirty{string(bts)} - } - - cmd := exec.Command("git", "describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag) - if bts, err := cmd.CombinedOutput(); err != nil { - return ErrWrongRef{string(bts)} - } - return nil -} From 086ad5691c0f3af32762ee2ec166908fabaf75f8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 16:12:32 -0300 Subject: [PATCH 20/20] godocs --- pipeline/git/git.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 1ec81673f..3a7569d80 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -1,5 +1,5 @@ -// Package git implements the Pipe interface extracting usefull data from -// git and putting it in the context. +// Package git implements the Pipe interface getting and validating the +// current git repository state package git import (