1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

Merge pull request #174 from goreleaser/tests

Increasing test coverage
This commit is contained in:
Carlos Alexandro Becker 2017-04-15 16:19:01 -03:00 committed by GitHub
commit 962534800f
19 changed files with 430 additions and 226 deletions

View File

@ -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

View File

@ -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))

View File

@ -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
git.Pipe{}, // get and validate git repo state
env.Pipe{}, // load and validate environment variables
source.Pipe{}, // validate current git state
)
}
pipes = append(

View File

@ -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))
})
}

View File

@ -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)

View File

@ -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")

View File

@ -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,20 @@ 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{
Dist: folder,
},
}
ctx.AddArtifact("nope")
assert.Error(Pipe{}.Run(ctx))
}
func TestPipeFileCantBeWritten(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
var ctx = &context.Context{
Config: config.Project{

73
pipeline/fpm/fpm_test.go Normal file
View File

@ -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())
}

View File

@ -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
}

View File

@ -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, "'")
}

21
pipeline/git/exec.go Normal file
View File

@ -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
}

View File

@ -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 (
@ -18,44 +18,86 @@ 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
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
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) {
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
}

View File

@ -1,6 +1,9 @@
package git
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/config"
@ -12,14 +15,141 @@ func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.Description())
}
func TestValidVersion(t *testing.T) {
assert := assert.New(t)
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()
gitInit(t)
gitCommit(t, "commit1")
gitTag(t, "v0.0.1")
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)
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
}
func TestNewRepository(t *testing.T) {
var assert = assert.New(t)
_, back := createAndChdir(t)
defer back()
gitInit(t)
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()
gitInit(t)
gitCommit(t, "commit2")
gitTag(t, "sadasd")
var ctx = &context.Context{
Config: config.Project{},
}
assert.EqualError(Pipe{}.Run(ctx), "sadasd is not in a valid version format")
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
//
func createAndChdir(t *testing.T) (current string, back func()) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
previous, err := os.Getwd()
assert.NoError(err)
assert.NoError(os.Chdir(folder))
return folder, 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 := 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 := fakeGit("tag", tag)
assert.NoError(err)
assert.Empty(out)
}
func gitAdd(t *testing.T) {
var assert = assert.New(t)
out, err := git("add", "-A")
assert.NoError(err)
assert.Empty(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...)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -2,6 +2,7 @@ package release
import (
"bytes"
"errors"
"io/ioutil"
"os"
"path/filepath"
@ -18,8 +19,8 @@ func TestPipeDescription(t *testing.T) {
}
func TestRunPipe(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "gorelasertest")
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(err)
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(err)
@ -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,7 +93,38 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) {
assert.False(client.UploadedFile)
}
func TestRunPipeUploadFailure(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")
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 {
FailToCreateRelease bool
FailToUpload bool
CreatedRelease bool
UploadedFile bool
}
@ -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
}

View File

@ -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
}