From 2f1564fa074d30064c0b35e2ee4ea63dc2956842 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 22 Apr 2024 20:46:46 +0200 Subject: [PATCH] fix: ensure branch has a default value set (#37) --- cmd/wp-git-clone/config_test.go | 47 +++++++++++++++++++++++++++++++++ cmd/wp-git-clone/flags.go | 1 - go.mod | 4 +++ go.sum | 1 + plugin/impl.go | 6 +++++ plugin/plugin.go | 4 ++- 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 cmd/wp-git-clone/config_test.go diff --git a/cmd/wp-git-clone/config_test.go b/cmd/wp-git-clone/config_test.go new file mode 100644 index 0000000..3e9c30b --- /dev/null +++ b/cmd/wp-git-clone/config_test.go @@ -0,0 +1,47 @@ +package main + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/thegeeklab/wp-git-clone/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: "ensure default branch is set", + envs: map[string]string{ + "CI_COMMIT_BRANCH": "", + }, + want: "main", + }, + } + + for _, tt := range tests { + for key, value := range tt.envs { + t.Setenv(key, value) + } + + settings := &plugin.Settings{} + options := wp.Options{ + Name: "wp-git-clone", + Flags: settingsFlags(settings, wp.FlagsPluginCategory), + Execute: func(_ context.Context) error { return nil }, + } + + got := plugin.New(options, settings) + + _ = got.App.Run([]string{"wp-git-clone"}) + _ = got.Validate() + _ = got.FlagsFromContext() + + assert.EqualValues(t, tt.want, got.Settings.Repo.Branch) + } +} diff --git a/cmd/wp-git-clone/flags.go b/cmd/wp-git-clone/flags.go index 9e0d7ac..6ebcce7 100644 --- a/cmd/wp-git-clone/flags.go +++ b/cmd/wp-git-clone/flags.go @@ -136,7 +136,6 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Name: "branch", Usage: "change branch name", EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"}, - Value: "main", Destination: &settings.Repo.Branch, Category: category, }, diff --git a/go.mod b/go.mod index eb9867c..e0a624a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22 require ( github.com/cenkalti/backoff/v4 v4.3.0 github.com/rs/zerolog v1.32.0 + github.com/stretchr/testify v1.9.0 github.com/thegeeklab/wp-plugin-go v1.7.1 github.com/urfave/cli/v2 v2.27.1 golang.org/x/sys v0.19.0 @@ -15,6 +16,7 @@ require ( github.com/Masterminds/semver/v3 v3.2.1 // indirect github.com/Masterminds/sprig/v3 v3.2.3 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.1.1 // indirect github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.11 // indirect @@ -23,10 +25,12 @@ require ( github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/reflectwalk v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shopspring/decimal v1.2.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/net v0.24.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 40b22c1..63ef955 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= diff --git a/plugin/impl.go b/plugin/impl.go index b3fc3d8..38a21ed 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -48,6 +48,12 @@ func (p *Plugin) run(ctx context.Context) error { // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { + // This default cannot be set in the cli flag, as the CI_* environment variables + // can be set empty, resulting in an empty default value. + if p.Settings.Repo.Branch == "" { + p.Settings.Repo.Branch = "main" + } + if p.Settings.WorkDir == "" { var err error if p.Settings.WorkDir, err = os.Getwd(); err != nil { diff --git a/plugin/plugin.go b/plugin/plugin.go index 9d967bc..38c73a9 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -37,7 +37,9 @@ type Settings 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