// Copyright The Forgejo Authors. // SPDX-License-Identifier: MIT package forgejo import ( "fmt" "testing" "code.gitea.io/gitea/services/context" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/urfave/cli/v2" ) func TestActions_getLabels(t *testing.T) { type testCase struct { args []string hasLabels bool hasError bool labels []string } type resultType struct { labels *[]string err error } cases := []testCase{ { args: []string{"x"}, hasLabels: true, hasError: false, labels: []string{""}, }, { args: []string{"x", "--labels", "a,b"}, hasLabels: true, hasError: false, labels: []string{"a", "b"}, }, { args: []string{"x", "--keep-labels"}, hasLabels: false, hasError: false, }, { args: []string{"x", "--keep-labels", "--labels", "a,b"}, hasLabels: false, hasError: true, }, { // this edge-case exists because that's what actually happens // when no '--labels ...' options are present args: []string{"x", "--keep-labels", "--labels", ""}, hasLabels: false, hasError: false, }, } flags := SubcmdActionsRegister(context.Context{}).Flags for _, c := range cases { t.Run(fmt.Sprintf("args: %v", c.args), func(t *testing.T) { // Create a copy of command to test var result *resultType app := cli.NewApp() app.Flags = flags app.Action = func(ctx *cli.Context) error { labels, err := getLabels(ctx) result = &resultType{labels, err} return nil } // Run it _ = app.Run(c.args) // Test the results require.NotNil(t, result) if c.hasLabels { assert.NotNil(t, result.labels) assert.Equal(t, c.labels, *result.labels) } else { assert.Nil(t, result.labels) } if c.hasError { assert.NotNil(t, result.err) } else { assert.Nil(t, result.err) } }) } }