(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestConfigContextFormatWrite(t *testing.T) { |
| 15 | // Check default output format (verbose and non-verbose mode) for table headers |
| 16 | cases := []struct { |
| 17 | context formatter.Context |
| 18 | expected string |
| 19 | }{ |
| 20 | // Errors |
| 21 | { |
| 22 | formatter.Context{Format: "{{InvalidFunction}}"}, |
| 23 | `template parsing error: template: :1: function "InvalidFunction" not defined`, |
| 24 | }, |
| 25 | { |
| 26 | formatter.Context{Format: "{{nil}}"}, |
| 27 | `template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`, |
| 28 | }, |
| 29 | // Table format |
| 30 | { |
| 31 | formatter.Context{Format: newFormat("table", false)}, |
| 32 | `ID NAME CREATED UPDATED |
| 33 | 1 passwords Less than a second ago Less than a second ago |
| 34 | 2 id_rsa Less than a second ago Less than a second ago |
| 35 | `, |
| 36 | }, |
| 37 | { |
| 38 | formatter.Context{Format: newFormat("table {{.Name}}", true)}, |
| 39 | `NAME |
| 40 | passwords |
| 41 | id_rsa |
| 42 | `, |
| 43 | }, |
| 44 | { |
| 45 | formatter.Context{Format: newFormat("{{.ID}}-{{.Name}}", false)}, |
| 46 | `1-passwords |
| 47 | 2-id_rsa |
| 48 | `, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | res := client.ConfigListResult{ |
| 53 | Items: []swarm.Config{ |
| 54 | { |
| 55 | ID: "1", |
| 56 | Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, |
| 57 | Spec: swarm.ConfigSpec{Annotations: swarm.Annotations{Name: "passwords"}}, |
| 58 | }, |
| 59 | { |
| 60 | ID: "2", |
| 61 | Meta: swarm.Meta{CreatedAt: time.Now(), UpdatedAt: time.Now()}, |
| 62 | Spec: swarm.ConfigSpec{Annotations: swarm.Annotations{Name: "id_rsa"}}, |
| 63 | }, |
| 64 | }, |
| 65 | } |
| 66 | for _, tc := range cases { |
| 67 | t.Run(string(tc.context.Format), func(t *testing.T) { |
| 68 | var out bytes.Buffer |
| 69 | tc.context.Output = &out |
| 70 | if err := formatWrite(tc.context, res); err != nil { |
| 71 | assert.ErrorContains(t, err, tc.expected) |
nothing calls this directly
no test coverage detected
searching dependent graphs…