mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-24 04:16:27 +02:00
avoid cd-ing in tests
This commit is contained in:
parent
c15e8c8108
commit
36efad3f78
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user