fix: ensure branch has a default value set (#37)

This commit is contained in:
Robert Kaussow 2024-04-22 20:46:46 +02:00 committed by GitHub
parent 9b68258a67
commit 2f1564fa07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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,
},

4
go.mod
View File

@ -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
)

1
go.sum
View File

@ -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=

View File

@ -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 {

View File

@ -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