File: template_test.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (86 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
		expectedOutput []string
	}{
		{
			template:       "",
			expectedOutput: []string{""},
		},
		{
			template:       "a plain template message",
			expectedOutput: []string{"a plain template message"},
		},
		{
			template: TemplateReplaceFlagValue("tag"),
			flags: []testFlag{
				{
					name:  "tag",
					value: "my-tag",
				},
			},
			expectedOutput: []string{"my-tag"},
		},
		{
			template: TemplateReplaceFlagValue("test-one") + " " + TemplateReplaceFlagValue("test2"),
			flags: []testFlag{
				{
					name:  "test-one",
					value: "value",
				},
				{
					name:  "test2",
					value: "value2",
				},
			},
			expectedOutput: []string{"value value2"},
		},
		{
			template:       TemplateReplaceArg(0) + " " + TemplateReplaceArg(1),
			args:           []string{"zero", "one"},
			expectedOutput: []string{"zero one"},
		},
		{
			template:       "You just pulled " + TemplateReplaceArg(0),
			args:           []string{"alpine"},
			expectedOutput: []string{"You just pulled alpine"},
		},
		{
			template:       "one line\nanother line!",
			expectedOutput: []string{"one line", "another line!"},
		},
	}

	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)
		assert.DeepEqual(t, out, tc.expectedOutput)
	}
}