Skip to content

Commit

Permalink
Merge pull request #341 from goreleaser/skip
Browse files Browse the repository at this point in the history
skip pipe as an error type and improved output
  • Loading branch information
caarlos0 committed Aug 24, 2017
2 parents 01622ad + 559106a commit 42446c8
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 48 deletions.
4 changes: 2 additions & 2 deletions context/context.go
Expand Up @@ -53,7 +53,7 @@ func (ctx *Context) AddArtifact(file string) {
defer artifactsLock.Unlock()
file = strings.TrimPrefix(file, ctx.Config.Dist+string(filepath.Separator))
ctx.Artifacts = append(ctx.Artifacts, file)
log.WithField("artifact", file).Info("new artifact")
log.WithField("artifact", file).Info("new release artifact")
}

// AddBinary adds a built binary to the current context
Expand All @@ -77,7 +77,7 @@ func (ctx *Context) AddBinary(platform, folder, name, path string) {
WithField("folder", folder).
WithField("name", name).
WithField("path", path).
Info("new binary")
Debug("new binary")
}

// New context
Expand Down
14 changes: 13 additions & 1 deletion goreleaserlib/goreleaser.go
Expand Up @@ -84,14 +84,26 @@ func Release(flags Flags) error {
ctx.RmDist = flags.Bool("rm-dist")
for _, pipe := range pipes {
log.Infof("\033[1m%s\033[0m", strings.ToUpper(pipe.Description()))
if err := pipe.Run(ctx); err != nil {
if err := handle(pipe.Run(ctx)); err != nil {
return err
}
}
log.Infof("\033[1mSUCCESS!\033[0m")
return nil
}

func handle(err error) error {
if err == nil {
return nil
}
skip, ok := err.(pipeline.ErrSkip)
if ok {
log.WithField("reason", skip.Error()).Warn("skipped")
return nil
}
return err
}

// InitProject creates an example goreleaser.yml in the current directory
func InitProject(filename string) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
Expand Down
4 changes: 2 additions & 2 deletions internal/buildtarget/targets.go
Expand Up @@ -10,12 +10,12 @@ func All(build config.Build) (targets []Target) {
for _, target := range allBuildTargets(build) {
if !valid(target) {
log.WithField("target", target.PrettyString()).
Warn("skipped invalid build")
Debug("skipped invalid build")
continue
}
if ignored(build, target) {
log.WithField("target", target.PrettyString()).
Warn("skipped ignored build")
Debug("skipped ignored build")
continue
}
targets = append(targets, target)
Expand Down
14 changes: 14 additions & 0 deletions internal/testlib/skip.go
@@ -0,0 +1,14 @@
package testlib

import (
"testing"

"github.com/goreleaser/goreleaser/pipeline"
"github.com/stretchr/testify/assert"
)

// AssertSkipped asserts that a pipe was skipped
func AssertSkipped(t *testing.T, err error) {
_, ok := err.(pipeline.ErrSkip)
assert.True(t, ok)
}
11 changes: 11 additions & 0 deletions internal/testlib/skip_test.go
@@ -0,0 +1,11 @@
package testlib

import (
"testing"

"github.com/goreleaser/goreleaser/pipeline"
)

func TestAssertSkipped(t *testing.T) {
AssertSkipped(t, pipeline.Skip("skip"))
}
13 changes: 5 additions & 8 deletions pipeline/brew/brew.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/archiveformat"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/pipeline"
)

// ErrNoDarwin64Build when there is no build for darwin_amd64 (goos doesn't
Expand All @@ -37,20 +38,16 @@ func (Pipe) Run(ctx *context.Context) error {

func doRun(ctx *context.Context, client client.Client) error {
if !ctx.Publish {
log.Warn("skipped because --skip-publish is set")
return nil
return pipeline.Skip("--skip-publish is set")
}
if ctx.Config.Brew.GitHub.Name == "" {
log.Warn("skipped because brew section is not configured")
return nil
return pipeline.Skip("brew section is not configured")
}
if ctx.Config.Release.Draft {
log.Warn("skipped because release is marked as draft")
return nil
return pipeline.Skip("release is marked as draft")
}
if ctx.Config.Archive.Format == "binary" {
log.Warn("skipped because archive format is binary")
return nil
return pipeline.Skip("archive format is binary")
}

var group = ctx.Binaries["darwinamd64"]
Expand Down
11 changes: 6 additions & 5 deletions pipeline/brew/brew_test.go
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -202,7 +203,7 @@ func TestRunPipeBrewNotSetup(t *testing.T) {
Publish: true,
}
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(client.CreatedFile)
}

Expand All @@ -224,7 +225,7 @@ func TestRunPipeBinaryRelease(t *testing.T) {
}
ctx.AddBinary("darwinamd64", "foo", "bar", "baz")
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(client.CreatedFile)
}

Expand All @@ -234,7 +235,7 @@ func TestRunPipeNoPublish(t *testing.T) {
Publish: false,
}
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(client.CreatedFile)
}

Expand All @@ -255,7 +256,7 @@ func TestRunPipeDraftRelease(t *testing.T) {
},
}
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(client.CreatedFile)
}

Expand All @@ -269,7 +270,7 @@ func TestRunPipeFormatBinary(t *testing.T) {
},
}
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
testlib.AssertSkipped(t, doRun(ctx, client))
assert.False(client.CreatedFile)
}

Expand Down
2 changes: 1 addition & 1 deletion pipeline/cleandist/dist.go
Expand Up @@ -27,7 +27,7 @@ func (Pipe) Run(ctx *context.Context) (err error) {
return nil
}
if ctx.RmDist {
log.Warn("rm-dist is set, removing ./dist")
log.Info("--rm-dist is set, removing ./dist")
return os.RemoveAll(ctx.Config.Dist)
}
files, err := ioutil.ReadDir(ctx.Config.Dist)
Expand Down
8 changes: 3 additions & 5 deletions pipeline/env/env.go
Expand Up @@ -6,8 +6,8 @@ import (
"errors"
"os"

"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/pipeline"
)

// ErrMissingToken indicates an error when GITHUB_TOKEN is missing in the environment
Expand All @@ -25,12 +25,10 @@ func (Pipe) Description() string {
func (Pipe) Run(ctx *context.Context) (err error) {
ctx.Token = os.Getenv("GITHUB_TOKEN")
if !ctx.Publish {
log.Warn("github token not validated because publishing has been disabled")
return nil
return pipeline.Skip("publishing is disabled")
}
if !ctx.Validate {
log.Warn("skipped validations because --skip-validate is set")
return nil
return pipeline.Skip("--skip-validate is set")
}
if ctx.Token == "" {
return ErrMissingToken
Expand Down
3 changes: 2 additions & 1 deletion pipeline/env/env_test.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func TestInvalidEnvChecksSkipped(t *testing.T) {
Publish: flag.Publish,
Snapshot: flag.Snapshot,
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
}
4 changes: 2 additions & 2 deletions pipeline/fpm/fpm.go
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/pipeline"
"golang.org/x/sync/errgroup"
)

Expand All @@ -27,8 +28,7 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if len(ctx.Config.FPM.Formats) == 0 {
log.Warn("skipping because no output formats configured")
return nil
return pipeline.Skip("no output formats configured")
}
_, err := exec.LookPath("fpm")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pipeline/fpm/fpm_test.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,12 +17,11 @@ func TestDescription(t *testing.T) {
}

func TestRunPipeNoFormats(t *testing.T) {
var assert = assert.New(t)
var ctx = &context.Context{
Version: "1.0.0",
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}

func TestRunPipe(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pipeline/git/git.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/pipeline"
)

// Pipe for brew deployment
Expand Down Expand Up @@ -43,8 +44,7 @@ func (Pipe) Run(ctx *context.Context) (err error) {
return
}
if !ctx.Validate {
log.Warn("skipped validations because --skip-validate is set")
return nil
return pipeline.Skip("--skip-validate is set")
}
return validate(ctx, commit, tag)
}
Expand Down
13 changes: 6 additions & 7 deletions pipeline/git/git_test.go
Expand Up @@ -37,7 +37,7 @@ func TestSingleCommit(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
}

Expand Down Expand Up @@ -67,7 +67,7 @@ func TestNoTagsSnapshot(t *testing.T) {
Snapshot: true,
Publish: false,
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Contains(ctx.Version, "SNAPSHOT-")
}

Expand Down Expand Up @@ -182,7 +182,6 @@ func TestValidState(t *testing.T) {
}

func TestNoValidate(t *testing.T) {
var assert = assert.New(t)
_, back := testlib.Mktmp(t)
defer back()
testlib.GitInit(t)
Expand All @@ -194,7 +193,7 @@ func TestNoValidate(t *testing.T) {
Config: config.Project{},
Validate: false,
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}

func TestChangelog(t *testing.T) {
Expand All @@ -210,7 +209,7 @@ func TestChangelog(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Equal("v0.0.2", ctx.Git.CurrentTag)
assert.Contains(ctx.ReleaseNotes, "## Changelog")
assert.NotContains(ctx.ReleaseNotes, "first")
Expand All @@ -236,7 +235,7 @@ func TestChangelogOfFirstRelease(t *testing.T) {
var ctx = &context.Context{
Config: config.Project{},
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
assert.Contains(ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
Expand All @@ -255,7 +254,7 @@ func TestCustomReleaseNotes(t *testing.T) {
Config: config.Project{},
ReleaseNotes: "custom",
}
assert.NoError(Pipe{}.Run(ctx))
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Equal("v0.0.1", ctx.Git.CurrentTag)
assert.Equal(ctx.ReleaseNotes, "custom")
}
15 changes: 15 additions & 0 deletions pipeline/pipe.go
Expand Up @@ -11,3 +11,18 @@ type Pipe interface {
// Run the pipe
Run(ctx *context.Context) error
}

// ErrSkip occurs when a pipe is skipped for some reason
type ErrSkip struct {
reason string
}

// Error implements the error interface. returns the reason the pipe was skipped
func (e ErrSkip) Error() string {
return e.reason
}

// Skip skips this pipe with the given reason
func Skip(reason string) ErrSkip {
return ErrSkip{reason}
}
15 changes: 15 additions & 0 deletions pipeline/pipe_test.go
@@ -0,0 +1,15 @@
package pipeline

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSkipPipe(t *testing.T) {
var assert = assert.New(t)
var reason = "this is a test"
var err = Skip(reason)
assert.Error(err)
assert.Equal(reason, err.Error())
}
4 changes: 2 additions & 2 deletions pipeline/release/release.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/pipeline"
"golang.org/x/sync/errgroup"
)

Expand All @@ -27,8 +28,7 @@ func (Pipe) Run(ctx *context.Context) error {

func doRun(ctx *context.Context, client client.Client) error {
if !ctx.Publish {
log.Warn("skipped because --skip-publish is set")
return nil
return pipeline.Skip("--skip-publish is set")
}
log.WithField("tag", ctx.Git.CurrentTag).
WithField("repo", ctx.Config.Release.GitHub.String()).
Expand Down

0 comments on commit 42446c8

Please sign in to comment.