1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +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,
content bytes.Buffer,
path string,
) (err error) {
) error {
options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String(commitAuthor.Name),
@ -64,7 +64,7 @@ func (c *githubClient) CreateFile(
&github.RepositoryContentGetOptions{},
)
if err != nil && res.StatusCode != 404 {
return
return err
}
if res.StatusCode == 404 {
@ -75,20 +75,20 @@ func (c *githubClient) CreateFile(
path,
options,
)
} else {
options.SHA = file.SHA
_, _, err = c.client.Repositories.UpdateFile(
ctx,
repo.Owner,
repo.Name,
path,
options,
)
return err
}
return
options.SHA = file.SHA
_, _, err = c.client.Repositories.UpdateFile(
ctx,
repo.Owner,
repo.Name,
path,
options,
)
return err
}
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
title, err := releaseTitle(ctx)
if err != nil {
@ -132,8 +132,8 @@ func (c *githubClient) Upload(
releaseID int64,
name string,
file *os.File,
) (err error) {
_, _, err = c.client.Repositories.UploadReleaseAsset(
) error {
_, _, err := c.client.Repositories.UploadReleaseAsset(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
@ -143,5 +143,5 @@ func (c *githubClient) Upload(
},
file,
)
return
return err
}

View File

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

View File

@ -49,11 +49,22 @@ func killAndRm(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 {
docker config.Docker
publish bool
expect []string
err string
docker config.Docker
publish bool
expect []string
assertError errChecker
}{
"valid": {
publish: true,
@ -79,7 +90,7 @@ func TestRunPipe(t *testing.T) {
registry + "goreleaser/test_run_pipe:v1.0",
registry + "goreleaser/test_run_pipe:latest",
},
err: "",
assertError: shouldNotErr,
},
"valid_no_latest": {
publish: true,
@ -99,7 +110,7 @@ func TestRunPipe(t *testing.T) {
expect: []string{
registry + "goreleaser/test_run_pipe:1.0.0",
},
err: "",
assertError: shouldNotErr,
},
"valid_dont_publish": {
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:latest",
},
err: "",
assertError: shouldNotErr,
},
"bad_dockerfile": {
publish: true,
@ -135,7 +146,7 @@ func TestRunPipe(t *testing.T) {
"{{.Version}}",
},
},
err: "pull access denied for nope, repository does not exist",
assertError: shouldErr("pull access denied for nope, repository does not exist"),
},
"template_error": {
publish: true,
@ -149,7 +160,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}",
},
},
err: `template: tag:1: unexpected "}" in operand`,
assertError: shouldErr(`template: tag:1: unexpected "}" in operand`),
},
"missing_env_on_template": {
publish: true,
@ -163,7 +174,7 @@ func TestRunPipe(t *testing.T) {
"{{.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": {
publish: true,
@ -183,7 +194,7 @@ func TestRunPipe(t *testing.T) {
"docker.io/nope:latest",
"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": {
publish: true,
@ -197,7 +208,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}}",
},
},
err: `failed to link dockerfile`,
assertError: shouldErr(`failed to link dockerfile`),
},
"extra_file_doesnt_exist": {
publish: true,
@ -214,7 +225,7 @@ func TestRunPipe(t *testing.T) {
"{{.Tag}}",
},
},
err: `failed to link extra file 'testdata/nope.txt'`,
assertError: shouldErr(`failed to link extra file 'testdata/nope.txt'`),
},
"no_matching_binaries": {
publish: true,
@ -225,7 +236,7 @@ func TestRunPipe(t *testing.T) {
Binary: "mybinnnn",
Dockerfile: "testdata/Dockerfile",
},
err: "",
assertError: shouldNotErr,
},
}
@ -279,15 +290,7 @@ func TestRunPipe(t *testing.T) {
_ = exec.Command("docker", "rmi", img).Run()
}
err = 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)
}
}
docker.assertError(t, Pipe{}.Run(ctx))
// this might should not fail as the image should have been created when
// the step ran

View File

@ -55,15 +55,25 @@ func TestDefault(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 {
ctx *context.Context
client client.Client
}
tests := []struct {
name string
args args
artifacts []artifact.Artifact
wantErr bool
name string
args args
artifacts []artifact.Artifact
assertError errChecker
}{
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
false,
shouldNotErr,
},
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
false,
shouldNotErr,
},
{
"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_386.tar.gz", Goos: "linux", Goarch: "386"},
},
true,
shouldErr("scoop requires a windows build"),
},
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
true,
shouldErr("scoop section is not configured"),
},
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
true,
shouldErr("--skip-publish is set"),
},
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
true,
shouldErr("release is marked as draft"),
},
{
"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_386.tar.gz", Goos: "windows", Goarch: "386"},
},
true,
shouldErr("archive format is binary"),
},
}
for _, tt := range tests {
@ -359,12 +369,7 @@ func Test_doRun(t *testing.T) {
for _, a := range tt.artifacts {
tt.args.ctx.Artifacts.Add(a)
}
err := doRun(tt.args.ctx, tt.args.client)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
tt.assertError(t, doRun(tt.args.ctx, tt.args.client))
})
}
}

View File

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