2023-07-20 11:25:36 -04:00
|
|
|
package hooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseTemplate(t *testing.T) {
|
|
|
|
type testFlag struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
template string
|
|
|
|
flags []testFlag
|
|
|
|
args []string
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput []string
|
2023-07-20 11:25:36 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
template: "",
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{""},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
template: "a plain template message",
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{"a plain template message"},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
template: TemplateReplaceFlagValue("tag"),
|
|
|
|
flags: []testFlag{
|
|
|
|
{
|
|
|
|
name: "tag",
|
|
|
|
value: "my-tag",
|
|
|
|
},
|
|
|
|
},
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{"my-tag"},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
template: TemplateReplaceFlagValue("test-one") + " " + TemplateReplaceFlagValue("test2"),
|
|
|
|
flags: []testFlag{
|
|
|
|
{
|
|
|
|
name: "test-one",
|
|
|
|
value: "value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test2",
|
|
|
|
value: "value2",
|
|
|
|
},
|
|
|
|
},
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{"value value2"},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
template: TemplateReplaceArg(0) + " " + TemplateReplaceArg(1),
|
|
|
|
args: []string{"zero", "one"},
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{"zero one"},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
template: "You just pulled " + TemplateReplaceArg(0),
|
|
|
|
args: []string{"alpine"},
|
2024-04-15 06:03:20 -04:00
|
|
|
expectedOutput: []string{"You just pulled alpine"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
template: "one line\nanother line!",
|
|
|
|
expectedOutput: []string{"one line", "another line!"},
|
2023-07-20 11:25:36 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
testCmd := &cobra.Command{
|
|
|
|
Use: "pull",
|
|
|
|
Args: cobra.ExactArgs(len(tc.args)),
|
|
|
|
}
|
|
|
|
for _, f := range tc.flags {
|
|
|
|
_ = testCmd.Flags().String(f.name, "", "")
|
|
|
|
err := testCmd.Flag(f.name).Value.Set(f.value)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
err := testCmd.Flags().Parse(tc.args)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
out, err := ParseTemplate(tc.template, testCmd)
|
|
|
|
assert.NilError(t, err)
|
2024-04-15 06:03:20 -04:00
|
|
|
assert.DeepEqual(t, out, tc.expectedOutput)
|
2023-07-20 11:25:36 -04:00
|
|
|
}
|
|
|
|
}
|