1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-15 01:34:21 +02:00

style: removed elses, improved test code

This commit is contained in:
Carlos Alexandro Becker
2018-02-16 10:35:44 -02:00
committed by Carlos Alexandro Becker
parent eef2647570
commit 66338de1bb
5 changed files with 67 additions and 60 deletions

View File

@ -44,7 +44,7 @@ func (c *githubClient) CreateFile(
repo config.Repo, repo config.Repo,
content bytes.Buffer, content bytes.Buffer,
path string, path string,
) (err error) { ) error {
options := &github.RepositoryContentFileOptions{ options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{ Committer: &github.CommitAuthor{
Name: github.String(commitAuthor.Name), Name: github.String(commitAuthor.Name),
@ -64,7 +64,7 @@ func (c *githubClient) CreateFile(
&github.RepositoryContentGetOptions{}, &github.RepositoryContentGetOptions{},
) )
if err != nil && res.StatusCode != 404 { if err != nil && res.StatusCode != 404 {
return return err
} }
if res.StatusCode == 404 { if res.StatusCode == 404 {
@ -75,7 +75,8 @@ func (c *githubClient) CreateFile(
path, path,
options, options,
) )
} else { return err
}
options.SHA = file.SHA options.SHA = file.SHA
_, _, err = c.client.Repositories.UpdateFile( _, _, err = c.client.Repositories.UpdateFile(
ctx, ctx,
@ -84,11 +85,10 @@ func (c *githubClient) CreateFile(
path, path,
options, options,
) )
} return err
return
} }
func (c *githubClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) { func (c *githubClient) CreateRelease(ctx *context.Context, body string) (int64, error) {
var release *github.RepositoryRelease var release *github.RepositoryRelease
title, err := releaseTitle(ctx) title, err := releaseTitle(ctx)
if err != nil { if err != nil {
@ -132,8 +132,8 @@ func (c *githubClient) Upload(
releaseID int64, releaseID int64,
name string, name string,
file *os.File, file *os.File,
) (err error) { ) error {
_, _, err = c.client.Repositories.UploadReleaseAsset( _, _, err := c.client.Repositories.UploadReleaseAsset(
ctx, ctx,
ctx.Config.Release.GitHub.Owner, ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name, ctx.Config.Release.GitHub.Name,
@ -143,5 +143,5 @@ func (c *githubClient) Upload(
}, },
file, file,
) )
return return err
} }

View File

@ -51,10 +51,9 @@ func (Pipe) Default(ctx *context.Context) error {
} }
} }
if archive.NameTemplate == "" { if archive.NameTemplate == "" {
archive.NameTemplate = defaultNameTemplate
if archive.Format == "binary" { if archive.Format == "binary" {
archive.NameTemplate = defaultBinaryNameTemplate archive.NameTemplate = defaultBinaryNameTemplate
} else {
archive.NameTemplate = defaultNameTemplate
} }
} }
return nil return nil

View File

@ -49,11 +49,22 @@ func killAndRm(t *testing.T) {
} }
func TestRunPipe(t *testing.T) { func TestRunPipe(t *testing.T) {
type errChecker func(*testing.T, error)
var shouldErr = func(msg string) errChecker {
return func(t *testing.T, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), msg)
}
}
var shouldNotErr = func(t *testing.T, err error) {
assert.NoError(t, err)
}
var table = map[string]struct { var table = map[string]struct {
docker config.Docker docker config.Docker
publish bool publish bool
expect []string expect []string
err string assertError errChecker
}{ }{
"valid": { "valid": {
publish: true, publish: true,
@ -79,7 +90,7 @@ func TestRunPipe(t *testing.T) {
registry + "goreleaser/test_run_pipe:v1.0", registry + "goreleaser/test_run_pipe:v1.0",
registry + "goreleaser/test_run_pipe:latest", registry + "goreleaser/test_run_pipe:latest",
}, },
err: "", assertError: shouldNotErr,
}, },
"valid_no_latest": { "valid_no_latest": {
publish: true, publish: true,
@ -99,7 +110,7 @@ func TestRunPipe(t *testing.T) {
expect: []string{ expect: []string{
registry + "goreleaser/test_run_pipe:1.0.0", registry + "goreleaser/test_run_pipe:1.0.0",
}, },
err: "", assertError: shouldNotErr,
}, },
"valid_dont_publish": { "valid_dont_publish": {
publish: false, publish: false,
@ -121,7 +132,7 @@ func TestRunPipe(t *testing.T) {
registry + "goreleaser/test_run_pipe:v1.0.0-123", registry + "goreleaser/test_run_pipe:v1.0.0-123",
registry + "goreleaser/test_run_pipe:latest", registry + "goreleaser/test_run_pipe:latest",
}, },
err: "", assertError: shouldNotErr,
}, },
"bad_dockerfile": { "bad_dockerfile": {
publish: true, publish: true,
@ -135,7 +146,7 @@ func TestRunPipe(t *testing.T) {
"{{.Version}}", "{{.Version}}",
}, },
}, },
err: "pull access denied for nope, repository does not exist", assertError: shouldErr("pull access denied for nope, repository does not exist"),
}, },
"template_error": { "template_error": {
publish: true, publish: true,
@ -149,7 +160,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}", "{{.Tag}",
}, },
}, },
err: `template: tag:1: unexpected "}" in operand`, assertError: shouldErr(`template: tag:1: unexpected "}" in operand`),
}, },
"missing_env_on_template": { "missing_env_on_template": {
publish: true, publish: true,
@ -163,7 +174,7 @@ func TestRunPipe(t *testing.T) {
"{{.Env.NOPE}}", "{{.Env.NOPE}}",
}, },
}, },
err: `template: tag:1:6: executing "tag" at <.Env.NOPE>: map has no entry for key "NOPE"`, assertError: shouldErr(`template: tag:1:6: executing "tag" at <.Env.NOPE>: map has no entry for key "NOPE"`),
}, },
"no_permissions": { "no_permissions": {
publish: true, publish: true,
@ -183,7 +194,7 @@ func TestRunPipe(t *testing.T) {
"docker.io/nope:latest", "docker.io/nope:latest",
"docker.io/nope:v1.0.0", "docker.io/nope:v1.0.0",
}, },
err: `requested access to the resource is denied`, assertError: shouldErr(`requested access to the resource is denied`),
}, },
"dockerfile_doesnt_exist": { "dockerfile_doesnt_exist": {
publish: true, publish: true,
@ -197,7 +208,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}}", "{{.Tag}}",
}, },
}, },
err: `failed to link dockerfile`, assertError: shouldErr(`failed to link dockerfile`),
}, },
"extra_file_doesnt_exist": { "extra_file_doesnt_exist": {
publish: true, publish: true,
@ -214,7 +225,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}}", "{{.Tag}}",
}, },
}, },
err: `failed to link extra file 'testdata/nope.txt'`, assertError: shouldErr(`failed to link extra file 'testdata/nope.txt'`),
}, },
"no_matching_binaries": { "no_matching_binaries": {
publish: true, publish: true,
@ -225,7 +236,7 @@ func TestRunPipe(t *testing.T) {
Binary: "mybinnnn", Binary: "mybinnnn",
Dockerfile: "testdata/Dockerfile", Dockerfile: "testdata/Dockerfile",
}, },
err: "", assertError: shouldNotErr,
}, },
} }
@ -279,15 +290,7 @@ func TestRunPipe(t *testing.T) {
_ = exec.Command("docker", "rmi", img).Run() _ = exec.Command("docker", "rmi", img).Run()
} }
err = Pipe{}.Run(ctx) docker.assertError(t, Pipe{}.Run(ctx))
if docker.err == "" {
assert.NoError(tt, err)
} else {
assert.Error(tt, err)
if err != nil {
assert.Contains(tt, err.Error(), docker.err)
}
}
// this might should not fail as the image should have been created when // this might should not fail as the image should have been created when
// the step ran // the step ran

View File

@ -55,6 +55,16 @@ func TestDefault(t *testing.T) {
} }
func Test_doRun(t *testing.T) { func Test_doRun(t *testing.T) {
type errChecker func(*testing.T, error)
var shouldErr = func(msg string) errChecker {
return func(t *testing.T, err error) {
assert.Error(t, err)
assert.EqualError(t, err, msg)
}
}
var shouldNotErr = func(t *testing.T, err error) {
assert.NoError(t, err)
}
type args struct { type args struct {
ctx *context.Context ctx *context.Context
client client.Client client client.Client
@ -63,7 +73,7 @@ func Test_doRun(t *testing.T) {
name string name string
args args args args
artifacts []artifact.Artifact artifacts []artifact.Artifact
wantErr bool assertError errChecker
}{ }{
{ {
"valid", "valid",
@ -106,7 +116,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"},
}, },
false, shouldNotErr,
}, },
{ {
"valid", "valid",
@ -150,7 +160,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"},
}, },
false, shouldNotErr,
}, },
{ {
"no windows build", "no windows build",
@ -193,7 +203,7 @@ func Test_doRun(t *testing.T) {
{Name: "foo_1.0.1_linux_amd64.tar.gz", Goos: "linux", Goarch: "amd64"}, {Name: "foo_1.0.1_linux_amd64.tar.gz", Goos: "linux", Goarch: "amd64"},
{Name: "foo_1.0.1_linux_386.tar.gz", Goos: "linux", Goarch: "386"}, {Name: "foo_1.0.1_linux_386.tar.gz", Goos: "linux", Goarch: "386"},
}, },
true, shouldErr("scoop requires a windows build"),
}, },
{ {
"no scoop", "no scoop",
@ -228,7 +238,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"},
}, },
true, shouldErr("scoop section is not configured"),
}, },
{ {
"no publish", "no publish",
@ -271,7 +281,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"},
}, },
true, shouldErr("--skip-publish is set"),
}, },
{ {
"is draft", "is draft",
@ -311,7 +321,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"},
}, },
true, shouldErr("release is marked as draft"),
}, },
{ {
"no archive", "no archive",
@ -351,7 +361,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"},
}, },
true, shouldErr("archive format is binary"),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -359,12 +369,7 @@ func Test_doRun(t *testing.T) {
for _, a := range tt.artifacts { for _, a := range tt.artifacts {
tt.args.ctx.Artifacts.Add(a) tt.args.ctx.Artifacts.Add(a)
} }
err := doRun(tt.args.ctx, tt.args.client) tt.assertError(t, doRun(tt.args.ctx, tt.args.client))
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}) })
} }
} }

View File

@ -128,10 +128,10 @@ func create(ctx *context.Context, arch string, binaries []artifact.Artifact) err
Architectures: []string{arch}, Architectures: []string{arch},
Apps: make(map[string]AppMetadata), Apps: make(map[string]AppMetadata),
} }
metadata.Name = ctx.Config.ProjectName
if ctx.Config.Snapcraft.Name != "" { if ctx.Config.Snapcraft.Name != "" {
metadata.Name = ctx.Config.Snapcraft.Name metadata.Name = ctx.Config.Snapcraft.Name
} else {
metadata.Name = ctx.Config.ProjectName
} }
for _, binary := range binaries { for _, binary := range binaries {