Skip to content

Commit

Permalink
Merge pull request #174 from goreleaser/tests
Browse files Browse the repository at this point in the history
Increasing test coverage
  • Loading branch information
caarlos0 committed Apr 15, 2017
2 parents 1109e68 + 086ad56 commit 9625348
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 226 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion checksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(
Expand Down
58 changes: 58 additions & 0 deletions pipeline/archive/archive_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
2 changes: 1 addition & 1 deletion pipeline/brew/brew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pipeline/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 15 additions & 2 deletions pipeline/checksums/checksums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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{
Expand Down
73 changes: 73 additions & 0 deletions pipeline/fpm/fpm_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
21 changes: 0 additions & 21 deletions pipeline/git/commit.go

This file was deleted.

15 changes: 0 additions & 15 deletions pipeline/git/commit_test.go

This file was deleted.

21 changes: 21 additions & 0 deletions pipeline/git/exec.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 53 additions & 11 deletions pipeline/git/git.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
}
Loading

0 comments on commit 9625348

Please sign in to comment.