1- package hooks
1+ package hooks_test
22
33import (
44 "testing"
55
6+ "github.com/docker/cli/cli-plugins/hooks"
67 "github.com/spf13/cobra"
78 "gotest.tools/v3/assert"
89)
910
11+ // TestParseTemplate tests parsing templates as returned by plugins.
12+ //
13+ // It uses fixed string fixtures to lock in compatibility with existing
14+ // plugin templates, so older formats continue to work even if we add new
15+ // template forms.
16+ //
17+ // For helper-backed cases, it also verifies that templates produced by the
18+ // current TemplateReplace* helpers parse to the same output. This lets us
19+ // evolve the emitted template format without breaking older plugins.
1020func TestParseTemplate (t * testing.T ) {
1121 type testFlag struct {
1222 name string
1323 value string
1424 }
15- testCases := []struct {
16- template string
25+ tests := []struct {
26+ doc string
27+ template string // compatibility fixture; keep even if helpers emit a newer form
28+ templateFunc func () string
1729 flags []testFlag
1830 args []string
1931 expectedOutput []string
2032 }{
2133 {
34+ doc : "empty template" ,
2235 template : "" ,
2336 expectedOutput : []string {"" },
2437 },
2538 {
39+ doc : "plain message" ,
2640 template : "a plain template message" ,
2741 expectedOutput : []string {"a plain template message" },
2842 },
2943 {
30- template : TemplateReplaceFlagValue ("tag" ),
44+ doc : "subcommand name" ,
45+ template : "hello {{.Name}}" , // NOTE: fixture; do not modify without considering plugin compatibility
46+ templateFunc : func () string { return "hello " + hooks .TemplateReplaceSubcommandName () },
47+
48+ expectedOutput : []string {"hello pull" },
49+ },
50+ {
51+ doc : "single flag" ,
52+ template : `{{flag . "tag"}}` , // NOTE: fixture; do not modify without considering plugin compatibility
53+ templateFunc : func () string { return hooks .TemplateReplaceFlagValue ("tag" ) },
3154 flags : []testFlag {
32- {
33- name : "tag" ,
34- value : "my-tag" ,
35- },
55+ {name : "tag" , value : "my-tag" },
3656 },
3757 expectedOutput : []string {"my-tag" },
3858 },
3959 {
40- template : TemplateReplaceFlagValue ("test-one" ) + " " + TemplateReplaceFlagValue ("test2" ),
60+ doc : "multiple flags" ,
61+ template : `{{flag . "test-one"}} {{flag . "test2"}}` , // NOTE: fixture; do not modify without considering plugin compatibility
62+ templateFunc : func () string {
63+ return hooks .TemplateReplaceFlagValue ("test-one" ) + " " + hooks .TemplateReplaceFlagValue ("test2" )
64+ },
4165 flags : []testFlag {
4266 {
4367 name : "test-one" ,
@@ -51,36 +75,51 @@ func TestParseTemplate(t *testing.T) {
5175 expectedOutput : []string {"value value2" },
5276 },
5377 {
54- template : TemplateReplaceArg (0 ) + " " + TemplateReplaceArg (1 ),
78+ doc : "multiple args" ,
79+ template : `{{arg . 0}} {{arg . 1}}` , // NOTE: fixture; do not modify without considering plugin compatibility
80+ templateFunc : func () string { return hooks .TemplateReplaceArg (0 ) + " " + hooks .TemplateReplaceArg (1 ) },
5581 args : []string {"zero" , "one" },
5682 expectedOutput : []string {"zero one" },
5783 },
5884 {
59- template : "You just pulled " + TemplateReplaceArg (0 ),
85+ doc : "arg in sentence" ,
86+ template : "You just pulled {{arg . 0}}" , // NOTE: fixture; do not modify without considering plugin compatibility
87+ templateFunc : func () string { return "You just pulled " + hooks .TemplateReplaceArg (0 ) },
6088 args : []string {"alpine" },
6189 expectedOutput : []string {"You just pulled alpine" },
6290 },
6391 {
92+ doc : "multiline output" ,
6493 template : "one line\n another line!" ,
6594 expectedOutput : []string {"one line" , "another line!" },
6695 },
6796 }
6897
69- for _ , tc := range testCases {
70- testCmd := & cobra.Command {
71- Use : "pull" ,
72- Args : cobra .ExactArgs (len (tc .args )),
73- }
74- for _ , f := range tc .flags {
75- _ = testCmd .Flags ().String (f .name , "" , "" )
76- err := testCmd .Flag (f .name ).Value .Set (f .value )
98+ for _ , tc := range tests {
99+ t .Run (tc .doc , func (t * testing.T ) {
100+ testCmd := & cobra.Command {
101+ Use : "pull" ,
102+ Args : cobra .ExactArgs (len (tc .args )),
103+ }
104+ for _ , f := range tc .flags {
105+ _ = testCmd .Flags ().String (f .name , "" , "" )
106+ err := testCmd .Flag (f .name ).Value .Set (f .value )
107+ assert .NilError (t , err )
108+ }
109+ err := testCmd .Flags ().Parse (tc .args )
110+ assert .NilError (t , err )
111+
112+ // Validate using fixtures.
113+ out , err := hooks .ParseTemplate (tc .template , testCmd )
77114 assert .NilError (t , err )
78- }
79- err := testCmd .Flags ().Parse (tc .args )
80- assert .NilError (t , err )
115+ assert .DeepEqual (t , out , tc .expectedOutput )
81116
82- out , err := ParseTemplate (tc .template , testCmd )
83- assert .NilError (t , err )
84- assert .DeepEqual (t , out , tc .expectedOutput )
117+ if tc .templateFunc != nil {
118+ // Validate using the current template function equivalent.
119+ out , err = hooks .ParseTemplate (tc .templateFunc (), testCmd )
120+ assert .NilError (t , err )
121+ assert .DeepEqual (t , out , tc .expectedOutput )
122+ }
123+ })
85124 }
86125}
0 commit comments