mirror of
https://github.com/thegeeklab/wp-docker-buildx.git
synced 2024-11-21 13:50:39 +00:00
add tests for custom types (#13)
This commit is contained in:
parent
60965366c5
commit
82e7c318f0
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/thegeeklab/wp-docker-buildx/plugin"
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
"github.com/thegeeklab/wp-plugin-go/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@ -209,7 +209,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
|
||||
Name: "cache-from",
|
||||
EnvVars: []string{"PLUGIN_CACHE_FROM"},
|
||||
Usage: "images to consider as cache sources",
|
||||
Value: &wp.StringSliceFlag{},
|
||||
Value: &types.StringSliceFlag{},
|
||||
Category: category,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
@ -325,7 +325,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
|
||||
Name: "secrets",
|
||||
EnvVars: []string{"PLUGIN_SECRETS"},
|
||||
Usage: "exposes secrets to the build",
|
||||
Value: &wp.StringSliceFlag{},
|
||||
Value: &types.StringSliceFlag{},
|
||||
Category: category,
|
||||
},
|
||||
}
|
||||
|
52
cmd/wp-docker-buildx/config_test.go
Normal file
52
cmd/wp-docker-buildx/config_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thegeeklab/wp-docker-buildx/plugin"
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
)
|
||||
|
||||
func Test_pluginOptions(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envs map[string]string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "parse secrets list with escape",
|
||||
envs: map[string]string{
|
||||
"PLUGIN_SECRETS": "id=raw_file_secret\\,src=file.txt,id=SECRET_TOKEN",
|
||||
},
|
||||
want: []string{
|
||||
"id=raw_file_secret,src=file.txt",
|
||||
"id=SECRET_TOKEN",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
for key, value := range tt.envs {
|
||||
t.Setenv(key, value)
|
||||
}
|
||||
|
||||
settings := &plugin.Settings{}
|
||||
options := wp.Options{
|
||||
Name: "wp-docker-buildx",
|
||||
Flags: settingsFlags(settings, wp.FlagsPluginCategory),
|
||||
Execute: func(ctx context.Context) error { return nil },
|
||||
}
|
||||
|
||||
got := plugin.New(options, settings)
|
||||
// got.App.HideHelp = true
|
||||
|
||||
_ = got.App.Run([]string{"wp-docker-buildx"})
|
||||
_ = got.FlagsFromContext()
|
||||
|
||||
if !reflect.DeepEqual(got.Settings.Build.Secrets, tt.want) {
|
||||
t.Errorf("%q. Build.Secrets = %v, want %v", tt.name, got.Settings.Build.Secrets, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.21
|
||||
require (
|
||||
github.com/coreos/go-semver v0.3.1
|
||||
github.com/rs/zerolog v1.30.0
|
||||
github.com/thegeeklab/wp-plugin-go v0.3.1
|
||||
github.com/thegeeklab/wp-plugin-go v0.5.0
|
||||
github.com/urfave/cli/v2 v2.25.7
|
||||
golang.org/x/sys v0.11.0
|
||||
)
|
||||
|
6
go.sum
6
go.sum
@ -16,8 +16,10 @@ github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
|
||||
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/thegeeklab/wp-plugin-go v0.3.1 h1:hbtP8NuwsVTJL7fFBaVebhhJC4TcSy6tFY1I5ydqzU0=
|
||||
github.com/thegeeklab/wp-plugin-go v0.3.1/go.mod h1:7PWAZiTnZ2fAvjsedopQJ6w873KuSxUWsAml1Yib8a0=
|
||||
github.com/thegeeklab/wp-plugin-go v0.4.0 h1:eTkRH04gAAsYgCUekk8RBmPrIXk+UG+EJtRkoUFllLs=
|
||||
github.com/thegeeklab/wp-plugin-go v0.4.0/go.mod h1:y0k5zaKWjdGbgkaOHhEPFEL6p+yOLOjqHy0LWOrv0MM=
|
||||
github.com/thegeeklab/wp-plugin-go v0.5.0 h1:PEORQl365YxqqYSu8rFoQ3hltTcFYSJCs8s9MDCej8A=
|
||||
github.com/thegeeklab/wp-plugin-go v0.5.0/go.mod h1:y0k5zaKWjdGbgkaOHhEPFEL6p+yOLOjqHy0LWOrv0MM=
|
||||
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
|
||||
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
wp "github.com/thegeeklab/wp-plugin-go/plugin"
|
||||
"github.com/thegeeklab/wp-plugin-go/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
@ -18,24 +18,12 @@ var ErrTypeAssertionFailed = errors.New("type assertion failed")
|
||||
|
||||
const strictFilePerm = 0o600
|
||||
|
||||
// Execute provides the implementation of the plugin.
|
||||
//
|
||||
//nolint:revive
|
||||
func (p *Plugin) run(ctx context.Context, cCtx *cli.Context) error {
|
||||
cacheFrom, ok := cCtx.Generic("cache-from").(*wp.StringSliceFlag)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: failed to read cache-from input", ErrTypeAssertionFailed)
|
||||
func (p *Plugin) run(ctx context.Context) error {
|
||||
if err := p.FlagsFromContext(); err != nil {
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
p.Settings.Build.CacheFrom = cacheFrom.Get()
|
||||
|
||||
secrets, ok := cCtx.Generic("secrets").(*wp.StringSliceFlag)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: failed to read secrets input", ErrTypeAssertionFailed)
|
||||
}
|
||||
|
||||
p.Settings.Build.Secrets = secrets.Get()
|
||||
|
||||
if err := p.Validate(); err != nil {
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
@ -183,3 +171,21 @@ func (p *Plugin) Execute() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) FlagsFromContext() error {
|
||||
cacheFrom, ok := p.Context.Generic("cache-from").(*types.StringSliceFlag)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: failed to read cache-from input", ErrTypeAssertionFailed)
|
||||
}
|
||||
|
||||
p.Settings.Build.CacheFrom = cacheFrom.Get()
|
||||
|
||||
secrets, ok := p.Context.Generic("secrets").(*types.StringSliceFlag)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: failed to read secrets input", ErrTypeAssertionFailed)
|
||||
}
|
||||
|
||||
p.Settings.Build.Secrets = secrets.Get()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// Plugin implements provide the plugin implementation.
|
||||
// Plugin implements provide the plugin.
|
||||
type Plugin struct {
|
||||
*wp.Plugin
|
||||
Settings *Settings
|
||||
@ -79,7 +79,9 @@ type Build struct {
|
||||
func New(options wp.Options, settings *Settings) *Plugin {
|
||||
p := &Plugin{}
|
||||
|
||||
options.Execute = p.run
|
||||
if options.Execute == nil {
|
||||
options.Execute = p.run
|
||||
}
|
||||
|
||||
p.Plugin = wp.New(options)
|
||||
p.Settings = settings
|
||||
|
Loading…
Reference in New Issue
Block a user