1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-17 01:42:37 +02:00

refactor: unifying snapshot with skip-validate and skip-publish (#575)

* refactor: removed goreleaserlib: moved all to main

* refactor: wip: snapshot

* fix: more pipes

* fix: more pipes

* fix: git tests

* fix: some other validate and publish usages

* fix: git dirty check when snapshoting

* fix: nfpm: use tag instead of version

* test: docker: print docker run output if registry fails
This commit is contained in:
Carlos Alexandro Becker
2018-02-24 17:59:08 -03:00
committed by GitHub
parent 2bb509f5a8
commit fdc032ec15
24 changed files with 113 additions and 242 deletions

View File

@ -14,7 +14,7 @@ install:
- sudo snap install snapcraft --classic - sudo snap install snapcraft --classic
script: script:
- make ci - make ci
- test -n "$TRAVIS_TAG" || go run main.go --skip-validate --skip-publish - test -n "$TRAVIS_TAG" || go run main.go --snapshot
after_success: after_success:
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)
- test -n "$TRAVIS_TAG" && docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" - test -n "$TRAVIS_TAG" && docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"

View File

@ -32,8 +32,6 @@ type Context struct {
Artifacts artifact.Artifacts Artifacts artifact.Artifacts
ReleaseNotes string ReleaseNotes string
Version string Version string
Validate bool
Publish bool
Snapshot bool Snapshot bool
RmDist bool RmDist bool
Debug bool Debug bool

20
main.go
View File

@ -9,11 +9,11 @@ import (
"github.com/apex/log" "github.com/apex/log"
lcli "github.com/apex/log/handlers/cli" lcli "github.com/apex/log/handlers/cli"
"github.com/caarlos0/ctrlc"
yaml "gopkg.in/yaml.v2"
"github.com/caarlos0/ctrlc"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/urfave/cli" "github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"
"github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
@ -99,21 +99,13 @@ func main() {
Name: "release-notes", Name: "release-notes",
Usage: "Load custom release notes from a markdown `FILE`", Usage: "Load custom release notes from a markdown `FILE`",
}, },
cli.BoolFlag{
Name: "skip-validate",
Usage: "Skip all the validations against the release",
},
cli.BoolFlag{
Name: "skip-publish",
Usage: "Skip all publishing pipes of the release",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "snapshot", Name: "snapshot",
Usage: "Generate an unversioned snapshot release", Usage: "Generate an unversioned snapshot release",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "rm-dist", Name: "rm-dist",
Usage: "Remove ./dist before building", Usage: "Remove the dist folder before building",
}, },
cli.IntFlag{ cli.IntFlag{
Name: "parallelism, p", Name: "parallelism, p",
@ -184,8 +176,6 @@ func releaseProject(flags Flags) error {
ctx.Parallelism = flags.Int("parallelism") ctx.Parallelism = flags.Int("parallelism")
ctx.Debug = flags.Bool("debug") ctx.Debug = flags.Bool("debug")
log.Debugf("parallelism: %v", ctx.Parallelism) log.Debugf("parallelism: %v", ctx.Parallelism)
ctx.Validate = !flags.Bool("skip-validate")
ctx.Publish = !flags.Bool("skip-publish")
if notes != "" { if notes != "" {
bts, err := ioutil.ReadFile(notes) bts, err := ioutil.ReadFile(notes)
if err != nil { if err != nil {
@ -196,10 +186,6 @@ func releaseProject(flags Flags) error {
ctx.ReleaseNotes = string(bts) ctx.ReleaseNotes = string(bts)
} }
ctx.Snapshot = flags.Bool("snapshot") ctx.Snapshot = flags.Bool("snapshot")
if ctx.Snapshot {
log.Info("publishing disabled in snapshot mode")
ctx.Publish = false
}
ctx.RmDist = flags.Bool("rm-dist") ctx.RmDist = flags.Bool("rm-dist")
return doRelease(ctx) return doRelease(ctx)
} }

View File

@ -24,14 +24,6 @@ func TestReleaseProject(t *testing.T) {
assert.NoError(t, releaseProject(newFlags(t, testParams()))) assert.NoError(t, releaseProject(newFlags(t, testParams())))
} }
func TestSnapshotreleaseProject(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params["snapshot"] = "true"
assert.NoError(t, releaseProject(newFlags(t, params)))
}
func TestConfigFileIsSetAndDontExist(t *testing.T) { func TestConfigFileIsSetAndDontExist(t *testing.T) {
params := testParams() params := testParams()
params["config"] = "/this/wont/exist" params["config"] = "/this/wont/exist"
@ -67,12 +59,13 @@ func TestReleaseNotesFileDontExist(t *testing.T) {
} }
func TestCustomReleaseNotesFile(t *testing.T) { func TestCustomReleaseNotesFile(t *testing.T) {
folder, back := setup(t) _, back := setup(t)
defer back() defer back()
var releaseNotes = filepath.Join(folder, "notes.md") releaseNotes, err := ioutil.TempFile("", "")
createFile(t, releaseNotes, "nothing important at all") assert.NoError(t, err)
createFile(t, releaseNotes.Name(), "nothing important at all")
var params = testParams() var params = testParams()
params["release-notes"] = releaseNotes params["release-notes"] = releaseNotes.Name()
assert.NoError(t, releaseProject(newFlags(t, params))) assert.NoError(t, releaseProject(newFlags(t, params)))
} }
@ -154,8 +147,7 @@ func testParams() map[string]string {
return map[string]string{ return map[string]string{
"debug": "true", "debug": "true",
"parallelism": "4", "parallelism": "4",
"skip-publish": "true", "snapshot": "true",
"skip-validate": "true",
"timeout": "1m", "timeout": "1m",
} }
} }

View File

@ -108,8 +108,8 @@ func (Pipe) Run(ctx *context.Context) error {
} }
func doRun(ctx *context.Context) error { func doRun(ctx *context.Context) error {
if !ctx.Publish { if ctx.Snapshot {
return pipeline.ErrSkipPublish return pipeline.ErrSnapshotEnabled
} }
// Handle every configured artifactory instance // Handle every configured artifactory instance

View File

@ -192,7 +192,6 @@ func TestRunPipe_ModeBinary(t *testing.T) {
"ARTIFACTORY_PRODUCTION-US_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION-US_SECRET": "deployuser-secret",
"ARTIFACTORY_PRODUCTION-EU_SECRET": "productionuser-apikey", "ARTIFACTORY_PRODUCTION-EU_SECRET": "productionuser-apikey",
} }
ctx.Publish = true
for _, goos := range []string{"linux", "darwin"} { for _, goos := range []string{"linux", "darwin"} {
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Name: "mybin", Name: "mybin",
@ -232,7 +231,6 @@ func TestRunPipe_ModeArchive(t *testing.T) {
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
ctx.Publish = true
ctx.Version = "1.0.0" ctx.Version = "1.0.0"
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive, Type: artifact.UploadableArchive,
@ -328,7 +326,6 @@ func TestRunPipe_ArtifactoryDown(t *testing.T) {
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive, Type: artifact.UploadableArchive,
Name: "bin.tar.gz", Name: "bin.tar.gz",
@ -358,7 +355,6 @@ func TestRunPipe_TargetTemplateError(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -415,7 +411,6 @@ func TestRunPipe_BadCredentials(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -473,7 +468,6 @@ func TestRunPipe_UnparsableErrorResponse(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -528,7 +522,6 @@ func TestRunPipe_UnparsableResponse(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -556,7 +549,6 @@ func TestRunPipe_FileNotFound(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -594,7 +586,6 @@ func TestRunPipe_UnparsableTarget(t *testing.T) {
}, },
}, },
}) })
ctx.Publish = true
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
@ -623,10 +614,11 @@ func TestRunPipe_SkipWhenPublishFalse(t *testing.T) {
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
ctx.Snapshot = true
err := Pipe{}.Run(ctx) err := Pipe{}.Run(ctx)
assert.True(t, pipeline.IsSkip(err)) assert.True(t, pipeline.IsSkip(err))
assert.EqualError(t, err, pipeline.ErrSkipPublish.Error()) assert.EqualError(t, err, pipeline.ErrSnapshotEnabled.Error())
} }
func TestRunPipe_DirUpload(t *testing.T) { func TestRunPipe_DirUpload(t *testing.T) {
@ -652,7 +644,6 @@ func TestRunPipe_DirUpload(t *testing.T) {
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
} }
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Name: "mybin", Name: "mybin",
Path: filepath.Dir(binPath), Path: filepath.Dir(binPath),
@ -733,7 +724,6 @@ func TestArtifactoriesWithoutSecret(t *testing.T) {
func TestArtifactoriesWithInvalidMode(t *testing.T) { func TestArtifactoriesWithInvalidMode(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Publish: true,
Env: map[string]string{ Env: map[string]string{
"ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret", "ARTIFACTORY_PRODUCTION_SECRET": "deployuser-secret",
}, },

View File

@ -122,8 +122,8 @@ func doRun(ctx *context.Context, client client.Client) error {
if ctx.Config.Brew.SkipUpload { if ctx.Config.Brew.SkipUpload {
return pipeline.Skip("brew.skip_upload is set") return pipeline.Skip("brew.skip_upload is set")
} }
if !ctx.Publish { if ctx.Snapshot {
return pipeline.ErrSkipPublish return pipeline.ErrSnapshotEnabled
} }
if ctx.Config.Release.Draft { if ctx.Config.Release.Draft {
return pipeline.Skip("release is marked as draft") return pipeline.Skip("release is marked as draft")

View File

@ -128,7 +128,6 @@ func TestRunPipe(t *testing.T) {
Install: `bin.install "foo"`, Install: `bin.install "foo"`,
}, },
}, },
Publish: true,
} }
var path = filepath.Join(folder, "bin.tar.gz") var path = filepath.Join(folder, "bin.tar.gz")
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
@ -212,7 +211,6 @@ func TestRunPipeNoDarwin64Build(t *testing.T) {
}, },
}, },
}, },
Publish: true,
} }
client := &DummyClient{} client := &DummyClient{}
assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client)) assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client))
@ -233,7 +231,6 @@ func TestRunPipeMultipleDarwin64Build(t *testing.T) {
}, },
}, },
) )
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Name: "bin1", Name: "bin1",
Path: "doesnt mather", Path: "doesnt mather",
@ -256,7 +253,6 @@ func TestRunPipeMultipleDarwin64Build(t *testing.T) {
func TestRunPipeBrewNotSetup(t *testing.T) { func TestRunPipeBrewNotSetup(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
Publish: true,
} }
client := &DummyClient{} client := &DummyClient{}
testlib.AssertSkipped(t, doRun(ctx, client)) testlib.AssertSkipped(t, doRun(ctx, client))
@ -277,7 +273,6 @@ func TestRunPipeBinaryRelease(t *testing.T) {
}, },
}, },
) )
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Name: "bin", Name: "bin",
Path: "doesnt mather", Path: "doesnt mather",
@ -321,16 +316,14 @@ func TestRunPipeNoUpload(t *testing.T) {
assert.False(t, client.CreatedFile) assert.False(t, client.CreatedFile)
} }
t.Run("skip upload", func(tt *testing.T) { t.Run("skip upload", func(tt *testing.T) {
ctx.Publish = true
ctx.Config.Brew.SkipUpload = true ctx.Config.Brew.SkipUpload = true
assertNoPublish(tt) assertNoPublish(tt)
}) })
t.Run("skip publish", func(tt *testing.T) { t.Run("snapshot", func(tt *testing.T) {
ctx.Publish = false ctx.Snapshot = true
assertNoPublish(tt) assertNoPublish(tt)
}) })
t.Run("draft release", func(tt *testing.T) { t.Run("draft release", func(tt *testing.T) {
ctx.Publish = true
ctx.Config.Release.Draft = true ctx.Config.Release.Draft = true
assertNoPublish(tt) assertNoPublish(tt)
}) })

View File

@ -1,9 +1,13 @@
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
package checksums package checksums
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"text/template"
"github.com/apex/log" "github.com/apex/log"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@ -79,3 +83,25 @@ func checksums(ctx *context.Context, file *os.File, artifact artifact.Artifact)
_, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name)) _, err = file.WriteString(fmt.Sprintf("%v %v\n", sha, artifact.Name))
return err return err
} }
func filenameFor(ctx *context.Context) (string, error) {
var out bytes.Buffer
t, err := template.New("checksums").
Option("missingkey=error").
Parse(ctx.Config.Checksum.NameTemplate)
if err != nil {
return "", err
}
err = t.Execute(&out, struct {
ProjectName string
Tag string
Version string
Env map[string]string
}{
ProjectName: ctx.Config.ProjectName,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Env: ctx.Env,
})
return out.String(), err
}

View File

@ -1,3 +0,0 @@
// Package checksums provides a Pipe that creates .checksums files for
// each artifact.
package checksums

View File

@ -1,30 +0,0 @@
package checksums
import (
"bytes"
"text/template"
"github.com/goreleaser/goreleaser/context"
)
func filenameFor(ctx *context.Context) (string, error) {
var out bytes.Buffer
t, err := template.New("checksums").
Option("missingkey=error").
Parse(ctx.Config.Checksum.NameTemplate)
if err != nil {
return "", err
}
err = t.Execute(&out, struct {
ProjectName string
Tag string
Version string
Env map[string]string
}{
ProjectName: ctx.Config.ProjectName,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
Env: ctx.Env,
})
return out.String(), err
}

View File

@ -195,8 +195,9 @@ func link(src, dest string) error {
} }
func publish(ctx *context.Context, docker config.Docker, images []string) error { func publish(ctx *context.Context, docker config.Docker, images []string) error {
if !ctx.Publish { if ctx.Snapshot {
log.Warn("skipping push because --skip-publish is set") // TODO: this should be better handled
log.Warn(pipeline.ErrSnapshotEnabled.Error())
return nil return nil
} }
for _, image := range images { for _, image := range images {

View File

@ -31,10 +31,10 @@ func start(t *testing.T) {
if *it { if *it {
return return
} }
if err := exec.Command( if out, err := exec.Command(
"docker", "run", "-d", "-p", "5000:5000", "--name", "registry", "registry:2", "docker", "run", "-d", "-p", "5000:5000", "--name", "registry", "registry:2",
).Run(); err != nil { ).CombinedOutput(); err != nil {
t.Log("failed to start docker registry", err) t.Log("failed to start docker registry", string(out), err)
t.FailNow() t.FailNow()
} }
} }
@ -262,7 +262,8 @@ func TestRunPipe(t *testing.T) {
docker.docker, docker.docker,
}, },
}) })
ctx.Publish = docker.publish // TODO: ideally refactor this as well
ctx.Snapshot = !docker.publish
ctx.Env = map[string]string{ ctx.Env = map[string]string{
"FOO": "123", "FOO": "123",
} }

7
pipeline/env/env.go vendored
View File

@ -35,11 +35,8 @@ func (Pipe) Default(ctx *context.Context) error {
func (Pipe) Run(ctx *context.Context) error { func (Pipe) Run(ctx *context.Context) error {
token, err := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken) token, err := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken)
ctx.Token = token ctx.Token = token
if !ctx.Publish { if ctx.Snapshot {
return pipeline.Skip("publishing is disabled") return pipeline.ErrSnapshotEnabled
}
if !ctx.Validate {
return pipeline.Skip("--skip-validate is set")
} }
if ctx.Token == "" && err == nil { if ctx.Token == "" && err == nil {
return ErrMissingToken return ErrMissingToken

View File

@ -38,8 +38,6 @@ func TestValidEnv(t *testing.T) {
assert.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf")) assert.NoError(t, os.Setenv("GITHUB_TOKEN", "asdf"))
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
Validate: true,
Publish: true,
} }
assert.NoError(t, Pipe{}.Run(ctx)) assert.NoError(t, Pipe{}.Run(ctx))
} }
@ -48,8 +46,6 @@ func TestInvalidEnv(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN")) assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
Validate: true,
Publish: true,
} }
assert.Error(t, Pipe{}.Run(ctx)) assert.Error(t, Pipe{}.Run(ctx))
} }
@ -58,8 +54,6 @@ func TestEmptyFileEnv(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN")) assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
Validate: true,
Publish: true,
} }
assert.Error(t, Pipe{}.Run(ctx)) assert.Error(t, Pipe{}.Run(ctx))
} }
@ -75,37 +69,17 @@ func TestEmptyEnvFile(t *testing.T) {
GitHubToken: f.Name(), GitHubToken: f.Name(),
}, },
}, },
Validate: true,
Publish: true,
} }
assert.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load github token: open %s: permission denied", f.Name())) assert.EqualError(t, Pipe{}.Run(ctx), fmt.Sprintf("failed to load github token: open %s: permission denied", f.Name()))
} }
func TestInvalidEnvChecksSkipped(t *testing.T) { func TestInvalidEnvChecksSkipped(t *testing.T) {
for _, flag := range []struct {
Validate, Publish, Snapshot bool
}{
{
Validate: false,
Publish: true,
}, {
Validate: true,
Publish: false,
}, {
Validate: true,
},
} {
t.Run(fmt.Sprintf("%v", flag), func(t *testing.T) {
assert.NoError(t, os.Unsetenv("GITHUB_TOKEN")) assert.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
Validate: flag.Validate, Snapshot: true,
Publish: flag.Publish,
Snapshot: flag.Snapshot,
} }
testlib.AssertSkipped(t, Pipe{}.Run(ctx)) testlib.AssertSkipped(t, Pipe{}.Run(ctx))
})
}
} }
func TestLoadEnv(t *testing.T) { func TestLoadEnv(t *testing.T) {

View File

@ -10,7 +10,6 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/pipeline"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -38,9 +37,6 @@ func (Pipe) Run(ctx *context.Context) (err error) {
if err = setVersion(ctx, tag, commit); err != nil { if err = setVersion(ctx, tag, commit); err != nil {
return return
} }
if !ctx.Validate {
return pipeline.Skip("--skip-validate is set")
}
return validate(ctx, commit, tag) return validate(ctx, commit, tag)
} }
@ -80,13 +76,13 @@ func getSnapshotName(ctx *context.Context, tag, commit string) (string, error) {
} }
func validate(ctx *context.Context, commit, tag string) error { func validate(ctx *context.Context, commit, tag string) error {
if ctx.Snapshot {
return nil
}
out, err := git.Run("status", "--porcelain") out, err := git.Run("status", "--porcelain")
if strings.TrimSpace(out) != "" || err != nil { if strings.TrimSpace(out) != "" || err != nil {
return ErrDirty{out} return ErrDirty{out}
} }
if ctx.Snapshot {
return nil
}
if !regexp.MustCompile("^[0-9.]+").MatchString(ctx.Version) { if !regexp.MustCompile("^[0-9.]+").MatchString(ctx.Version) {
return ErrInvalidVersionFormat{ctx.Version} return ErrInvalidVersionFormat{ctx.Version}
} }

View File

@ -22,7 +22,7 @@ func TestNotAGitFolder(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
} }
assert.Error(t, Pipe{}.Run(ctx)) assert.EqualError(t, Pipe{}.Run(ctx), "fatal: Not a git repository (or any of the parent directories): .git\n")
} }
func TestSingleCommit(t *testing.T) { func TestSingleCommit(t *testing.T) {
@ -34,7 +34,7 @@ func TestSingleCommit(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
} }
testlib.AssertSkipped(t, Pipe{}.Run(ctx)) assert.NoError(t, Pipe{}.Run(ctx))
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag) assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
} }
@ -45,7 +45,8 @@ func TestNewRepository(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Config: config.Project{}, Config: config.Project{},
} }
assert.Error(t, Pipe{}.Run(ctx)) // TODO: improve this error handling
assert.Contains(t, Pipe{}.Run(ctx).Error(), `fatal: ambiguous argument 'HEAD'`)
} }
func TestNoTagsSnapshot(t *testing.T) { func TestNoTagsSnapshot(t *testing.T) {
@ -53,16 +54,13 @@ func TestNoTagsSnapshot(t *testing.T) {
defer back() defer back()
testlib.GitInit(t) testlib.GitInit(t)
testlib.GitCommit(t, "first") testlib.GitCommit(t, "first")
var ctx = &context.Context{ var ctx = context.New(config.Project{
Config: config.Project{
Snapshot: config.Snapshot{ Snapshot: config.Snapshot{
NameTemplate: "SNAPSHOT-{{.Commit}}", NameTemplate: "SNAPSHOT-{{.Commit}}",
}, },
}, })
Snapshot: true, ctx.Snapshot = true
Publish: false, assert.NoError(t, Pipe{}.Run(ctx))
}
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Contains(t, ctx.Version, "SNAPSHOT-") assert.Contains(t, ctx.Version, "SNAPSHOT-")
} }
@ -71,16 +69,13 @@ func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
defer back() defer back()
testlib.GitInit(t) testlib.GitInit(t)
testlib.GitCommit(t, "first") testlib.GitCommit(t, "first")
var ctx = &context.Context{ var ctx = context.New(config.Project{
Config: config.Project{
Snapshot: config.Snapshot{ Snapshot: config.Snapshot{
NameTemplate: "{{", NameTemplate: "{{",
}, },
}, })
Snapshot: true, ctx.Snapshot = true
Publish: false, assert.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: snapshot:1: unexpected unclosed action in command`)
}
assert.Error(t, Pipe{}.Run(ctx))
} }
// TestNoTagsNoSnapshot covers the situation where a repository // TestNoTagsNoSnapshot covers the situation where a repository
@ -91,16 +86,9 @@ func TestNoTagsNoSnapshot(t *testing.T) {
defer back() defer back()
testlib.GitInit(t) testlib.GitInit(t)
testlib.GitCommit(t, "first") testlib.GitCommit(t, "first")
var ctx = &context.Context{ var ctx = context.New(config.Project{})
Config: config.Project{ ctx.Snapshot = false
Snapshot: config.Snapshot{ assert.EqualError(t, Pipe{}.Run(ctx), `git doesn't contain any tags. Either add a tag or use --snapshot`)
NameTemplate: "SNAPSHOT-{{.Commit}}",
},
},
Snapshot: false,
Publish: false,
}
assert.Error(t, Pipe{}.Run(ctx))
} }
func TestInvalidTagFormat(t *testing.T) { func TestInvalidTagFormat(t *testing.T) {
@ -109,10 +97,7 @@ func TestInvalidTagFormat(t *testing.T) {
testlib.GitInit(t) testlib.GitInit(t)
testlib.GitCommit(t, "commit2") testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "sadasd") testlib.GitTag(t, "sadasd")
var ctx = &context.Context{ var ctx = context.New(config.Project{})
Config: config.Project{},
Validate: true,
}
assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format") assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
assert.Equal(t, "sadasd", ctx.Git.CurrentTag) assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
} }
@ -127,11 +112,7 @@ func TestDirty(t *testing.T) {
testlib.GitCommit(t, "commit2") testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1") testlib.GitTag(t, "v0.0.1")
assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644)) assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
var ctx = &context.Context{ err = Pipe{}.Run(context.New(config.Project{}))
Config: config.Project{},
Validate: true,
}
err = Pipe{}.Run(ctx)
assert.Error(t, err) assert.Error(t, err)
assert.Contains(t, err.Error(), "git is currently in a dirty state:") assert.Contains(t, err.Error(), "git is currently in a dirty state:")
} }
@ -143,11 +124,7 @@ func TestTagIsNotLastCommit(t *testing.T) {
testlib.GitCommit(t, "commit3") testlib.GitCommit(t, "commit3")
testlib.GitTag(t, "v0.0.1") testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4") testlib.GitCommit(t, "commit4")
var ctx = &context.Context{ err := Pipe{}.Run(context.New(config.Project{}))
Config: config.Project{},
Validate: true,
}
err := Pipe{}.Run(ctx)
assert.Error(t, err) assert.Error(t, err)
assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit") assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
} }
@ -160,39 +137,20 @@ func TestValidState(t *testing.T) {
testlib.GitTag(t, "v0.0.1") testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit4") testlib.GitCommit(t, "commit4")
testlib.GitTag(t, "v0.0.2") testlib.GitTag(t, "v0.0.2")
var ctx = &context.Context{ var ctx = context.New(config.Project{})
Config: config.Project{},
Validate: true,
}
assert.NoError(t, Pipe{}.Run(ctx)) assert.NoError(t, Pipe{}.Run(ctx))
assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag) assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
} }
func TestNoValidate(t *testing.T) {
_, back := testlib.Mktmp(t)
defer back()
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "commit5")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "commit6")
var ctx = &context.Context{
Config: config.Project{},
Validate: false,
}
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}
func TestSnapshot(t *testing.T) { func TestSnapshot(t *testing.T) {
_, back := testlib.Mktmp(t) _, back := testlib.Mktmp(t)
defer back() defer back()
testlib.GitInit(t) testlib.GitInit(t)
testlib.GitAdd(t) testlib.GitAdd(t)
testlib.GitCommit(t, "whatever") testlib.GitCommit(t, "whatever")
var ctx = &context.Context{ var ctx = context.New(config.Project{})
Config: config.Project{}, ctx.Snapshot = true
Validate: true,
Snapshot: true,
}
assert.NoError(t, Pipe{}.Run(ctx)) assert.NoError(t, Pipe{}.Run(ctx))
} }
// TODO: missing a test case for a dirty git tree and snapshot

View File

@ -108,7 +108,7 @@ func create(ctx *context.Context, format, arch string, binaries []artifact.Artif
Recommends: ctx.Config.NFPM.Recommends, Recommends: ctx.Config.NFPM.Recommends,
Suggests: ctx.Config.NFPM.Suggests, Suggests: ctx.Config.NFPM.Suggests,
Name: ctx.Config.ProjectName, Name: ctx.Config.ProjectName,
Version: ctx.Version, Version: ctx.Git.CurrentTag,
Section: "", Section: "",
Priority: "", Priority: "",
Maintainer: ctx.Config.NFPM.Maintainer, Maintainer: ctx.Config.NFPM.Maintainer,

View File

@ -20,7 +20,9 @@ func TestDescription(t *testing.T) {
func TestRunPipeNoFormats(t *testing.T) { func TestRunPipeNoFormats(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Version: "1.0.0", Git: context.GitInfo{
CurrentTag: "v1.0.0",
},
Config: config.Project{}, Config: config.Project{},
Parallelism: runtime.NumCPU(), Parallelism: runtime.NumCPU(),
} }
@ -83,7 +85,7 @@ func TestRunPipe(t *testing.T) {
}, },
}, },
}) })
ctx.Version = "1.0.0" ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
for _, goos := range []string{"linux", "darwin"} { for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} { for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{

View File

@ -14,9 +14,9 @@ type Piper interface {
Run(ctx *context.Context) error Run(ctx *context.Context) error
} }
// ErrSkipPublish happens when skip publish is set and a pipe is refusing // ErrSnapshotEnabled happens when goreleaser is running in snapshot mode.
// to proceed because of that. // It usually means that publishing and maybe some validations were skipped.
var ErrSkipPublish = Skip("--skip-publish is set") var ErrSnapshotEnabled = Skip("disabled during snapshot mode")
// IsSkip returns true if the error is an ErrSkip // IsSkip returns true if the error is an ErrSkip
func IsSkip(err error) bool { func IsSkip(err error) bool {

View File

@ -45,8 +45,8 @@ func (Pipe) Run(ctx *context.Context) error {
} }
func doRun(ctx *context.Context, c client.Client) error { func doRun(ctx *context.Context, c client.Client) error {
if !ctx.Publish { if ctx.Snapshot {
return pipeline.ErrSkipPublish return pipeline.ErrSnapshotEnabled
} }
log.WithField("tag", ctx.Git.CurrentTag). log.WithField("tag", ctx.Git.CurrentTag).
WithField("repo", ctx.Config.Release.GitHub.String()). WithField("repo", ctx.Config.Release.GitHub.String()).

View File

@ -38,7 +38,6 @@ func TestRunPipe(t *testing.T) {
} }
var ctx = context.New(config) var ctx = context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"} ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive, Type: artifact.UploadableArchive,
Name: "bin.tar.gz", Name: "bin.tar.gz",
@ -68,7 +67,6 @@ func TestRunPipeReleaseCreationFailed(t *testing.T) {
} }
var ctx = context.New(config) var ctx = context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"} ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Publish = true
client := &DummyClient{ client := &DummyClient{
FailToCreateRelease: true, FailToCreateRelease: true,
} }
@ -88,7 +86,6 @@ func TestRunPipeWithFileThatDontExist(t *testing.T) {
} }
var ctx = context.New(config) var ctx = context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"} ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive, Type: artifact.UploadableArchive,
Name: "bin.tar.gz", Name: "bin.tar.gz",
@ -115,7 +112,6 @@ func TestRunPipeUploadFailure(t *testing.T) {
} }
var ctx = context.New(config) var ctx = context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"} ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Publish = true
ctx.Artifacts.Add(artifact.Artifact{ ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive, Type: artifact.UploadableArchive,
Name: "bin.tar.gz", Name: "bin.tar.gz",
@ -129,9 +125,9 @@ func TestRunPipeUploadFailure(t *testing.T) {
assert.False(t, client.UploadedFile) assert.False(t, client.UploadedFile)
} }
func TestSkipPublish(t *testing.T) { func TestSnapshot(t *testing.T) {
var ctx = &context.Context{ var ctx = &context.Context{
Publish: false, Snapshot: true,
Parallelism: 1, Parallelism: 1,
} }
client := &DummyClient{} client := &DummyClient{}

View File

@ -68,8 +68,8 @@ func doRun(ctx *context.Context, client client.Client) error {
return err return err
} }
if !ctx.Publish { if ctx.Snapshot {
return pipeline.ErrSkipPublish return pipeline.ErrSnapshotEnabled
} }
if ctx.Config.Release.Draft { if ctx.Config.Release.Draft {
return pipeline.Skip("release is marked as draft") return pipeline.Skip("release is marked as draft")

View File

@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/client" "github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/testlib" "github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pipeline"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -108,7 +109,6 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -152,7 +152,6 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -195,7 +194,6 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -230,7 +228,6 @@ func Test_doRun(t *testing.T) {
}, },
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -273,7 +270,7 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: false, Snapshot: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -281,7 +278,7 @@ func Test_doRun(t *testing.T) {
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"}, {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},
{Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"}, {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"},
}, },
shouldErr("--skip-publish is set"), shouldErr(pipeline.ErrSnapshotEnabled.Error()),
}, },
{ {
"is draft", "is draft",
@ -313,7 +310,6 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -353,7 +349,6 @@ func Test_doRun(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
}, },
&DummyClient{}, &DummyClient{},
}, },
@ -408,7 +403,6 @@ func Test_buildManifest(t *testing.T) {
Homepage: "https://github.com/goreleaser", Homepage: "https://github.com/goreleaser",
}, },
}, },
Publish: true,
} }
out, err := buildManifest(ctx, &DummyClient{}, []artifact.Artifact{ out, err := buildManifest(ctx, &DummyClient{}, []artifact.Artifact{
{Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"}, {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"},