TestParseTemplate tests parsing templates as returned by plugins. It uses fixed string fixtures to lock in compatibility with existing plugin templates, so older formats continue to work even if we add new template forms. For helper-backed cases, it also verifies that templates produced by the cur
(t *testing.T)
| 18 | // current TemplateReplace* helpers parse to the same output. This lets us |
| 19 | // evolve the emitted template format without breaking older plugins. |
| 20 | func TestParseTemplate(t *testing.T) { |
| 21 | type testFlag struct { |
| 22 | name string |
| 23 | value string |
| 24 | } |
| 25 | tests := []struct { |
| 26 | doc string |
| 27 | template string // compatibility fixture; keep even if helpers emit a newer form |
| 28 | templateFunc func() string |
| 29 | flags []testFlag |
| 30 | args []string |
| 31 | expectedOutput []string |
| 32 | }{ |
| 33 | { |
| 34 | doc: "empty template", |
| 35 | template: "", |
| 36 | expectedOutput: []string{""}, |
| 37 | }, |
| 38 | { |
| 39 | doc: "plain message", |
| 40 | template: "a plain template message", |
| 41 | expectedOutput: []string{"a plain template message"}, |
| 42 | }, |
| 43 | { |
| 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") }, |
| 54 | flags: []testFlag{ |
| 55 | {name: "tag", value: "my-tag"}, |
| 56 | }, |
| 57 | expectedOutput: []string{"my-tag"}, |
| 58 | }, |
| 59 | { |
| 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 | }, |
| 65 | flags: []testFlag{ |
| 66 | { |
| 67 | name: "test-one", |
| 68 | value: "value", |
| 69 | }, |
| 70 | { |
| 71 | name: "test2", |
| 72 | value: "value2", |
| 73 | }, |
| 74 | }, |
| 75 | expectedOutput: []string{"value value2"}, |
| 76 | }, |
| 77 | { |