Skip to content

Commit

Permalink
refactor: simplify validateImager, validateManifester
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 12, 2024
1 parent dade01e commit 43aa96f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
10 changes: 4 additions & 6 deletions internal/pipe/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ func (Pipe) Default(ctx *context.Context) error {
}

func validateImager(use string) error {
valid := maps.Keys(imagers)
for s := range valid {
if s == use {
return nil
}
valid := slices.Sorted(maps.Keys(imagers))
if slices.Contains(valid, use) {
return nil
}
return fmt.Errorf("docker: invalid use: %s, valid options are %v", use, slices.Sorted(valid))
return fmt.Errorf("docker: invalid use: %s, valid options are %v", use, valid)
}

// Publish the docker images.
Expand Down
22 changes: 22 additions & 0 deletions internal/pipe/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1442,3 +1442,25 @@ func TestIsFileNotFoundError(t *testing.T) {
require.True(t, isFileNotFoundError(`./foo: not found: not found`))
})
}

func TestValidateImager(t *testing.T) {
tests := []struct {
use string
wantError string
}{
{use: "docker"},
{use: "buildx"},
{use: "notFound", wantError: "docker: invalid use: notFound, valid options are [buildx docker]"},
}

for _, tt := range tests {
t.Run(tt.use, func(t *testing.T) {
err := validateImager(tt.use)
if tt.wantError != "" {
require.EqualError(t, err, tt.wantError)
return
}
require.NoError(t, err)
})
}
}
10 changes: 4 additions & 6 deletions internal/pipe/docker/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ func (ManifestPipe) Publish(ctx *context.Context) error {
}

func validateManifester(use string) error {
valid := maps.Keys(manifesters)
for s := range valid {
if s == use {
return nil
}
valid := slices.Sorted(maps.Keys(manifesters))
if slices.Contains(valid, use) {
return nil
}
return fmt.Errorf("docker manifest: invalid use: %s, valid options are %v", use, slices.Sorted(valid))
return fmt.Errorf("docker manifest: invalid use: %s, valid options are %v", use, valid)
}

func manifestName(ctx *context.Context, manifest config.DockerManifest) (string, error) {
Expand Down
28 changes: 28 additions & 0 deletions internal/pipe/docker/manifest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package docker

import (
"testing"

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

func TestValidateManifester(t *testing.T) {
tests := []struct {
use string
wantError string
}{
{use: "docker"},
{use: "buildx", wantError: "docker manifest: invalid use: buildx, valid options are [docker]"},
}

for _, tt := range tests {
t.Run(tt.use, func(t *testing.T) {
err := validateManifester(tt.use)
if tt.wantError != "" {
require.EqualError(t, err, tt.wantError)
return
}
require.NoError(t, err)
})
}
}

0 comments on commit 43aa96f

Please sign in to comment.