File: formatter_test.go

package info (click to toggle)
golang-github-containers-common 0.50.1%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,440 kB
  • sloc: makefile: 118; sh: 46
file content (227 lines) | stat: -rw-r--r-- 7,276 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package report

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"
	"testing"
	"text/tabwriter"
	"text/template"

	"github.com/stretchr/testify/assert"
)

func TestFormatter_IsTableFalse(t *testing.T) {
	fmtr, err := New(os.Stdout, t.Name()).Parse(OriginPodman, "{{.ID}}")
	assert.NoError(t, err)
	assert.False(t, fmtr.RenderTable)
}

func TestFormatter_IsTableTrue(t *testing.T) {
	fmtr, err := New(os.Stdout, t.Name()).Parse(OriginPodman, "table {{.ID}}")
	assert.NoError(t, err)
	assert.True(t, fmtr.RenderTable)
}

func TestFormatter_HasTable(t *testing.T) {
	assert.True(t, HasTable("table foobar"))
	assert.False(t, HasTable("foobar"))
}

type testFormatterStruct struct {
	FieldA bool // camel case test
	Fieldb bool // no camel case
	fieldC bool // nolint // private field
	fieldd bool // nolint // private field
}

func TestFormatter_HeadersNoOverrides(t *testing.T) {
	expected := []map[string]string{{
		"FieldA": "FIELD A",
		"Fieldb": "FIELDB",
		"fieldC": "FIELD C",
		"fieldd": "FIELDD",
	}}
	assert.Equal(t, expected, Headers(testFormatterStruct{}, nil))
}

func TestFormatter_HeadersOverride(t *testing.T) {
	expected := []map[string]string{{
		"FieldA": "FIELD A",
		"Fieldb": "FIELD B",
		"fieldC": "FIELD C",
		"fieldd": "FIELD D",
	}}
	assert.Equal(t, expected, Headers(testFormatterStruct{}, map[string]string{
		"Fieldb": "field b",
		"fieldd": "field d",
	}))
}

func TestFormatter_ParseTable(t *testing.T) {
	testCase := []struct {
		Type     io.Writer
		Origin   Origin
		Format   string
		Expected string
	}{
		{&tabwriter.Writer{}, OriginUser, "table {{ .ID}}", "Identity\nc061a0839e\nf10fc2e11057\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\n"},
		{&tabwriter.Writer{}, OriginUser, "table {{ .ID}}\n", "Identity\nc061a0839e\nf10fc2e11057\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\n"},
		{&tabwriter.Writer{}, OriginUser, `table {{ .ID}}\n`, "Identity\nc061a0839e\nf10fc2e11057\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\n"},
		{&tabwriter.Writer{}, OriginUser, "table {{.ID}}", "Identity\nc061a0839e\nf10fc2e11057\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\n"},
		{
			&tabwriter.Writer{}, OriginUser, "table {{.ID}}\t{{.Value}}",
			"Identity                          Value\nc061a0839e                        one\nf10fc2e11057                      two\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a  three\n",
		},
		{&bytes.Buffer{}, OriginUser, "{{range .}}{{.ID}}\tID{{end}}", "c061a0839e\tIDf10fc2e11057\tID1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\tID\n"},
		{
			&tabwriter.Writer{}, OriginPodman, "Value\tIdent\n{{range .}}{{.ID}}\tID{{end}}",
			"Value       Ident\nIdentity    ID\nValue       Ident\nc061a0839e  IDf10fc2e11057  ID1eb6fab5aa8f4b5cbfd3e66aa35e9b2a  ID\n",
		},
		{&bytes.Buffer{}, OriginUser, "{{range .}}{{.ID}}\tID\n{{end}}", "c061a0839e\tID\nf10fc2e11057\tID\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a\tID\n\n"},
		{&bytes.Buffer{}, OriginUser, `{{range .}}{{.ID}}{{end -}}`, "c061a0839ef10fc2e110571eb6fab5aa8f4b5cbfd3e66aa35e9b2a"},
		// regression test for https://bugzilla.redhat.com/show_bug.cgi?id=2059658 and https://github.com/containers/podman/issues/13446
		{&bytes.Buffer{}, OriginUser, `{{range .}}{{printf "\n"}}{{end -}}`, "\n\n\n"},
	}

	for loop, tc := range testCase {
		tc := tc
		name := fmt.Sprintf("Loop#%d", loop)
		t.Run(name, func(t *testing.T) {
			buf := new(bytes.Buffer)

			rpt, err := New(buf, name).Parse(tc.Origin, tc.Format)
			assert.NoError(t, err)
			assert.Equal(t, tc.Origin, rpt.Origin)
			assert.IsType(t, tc.Type, rpt.Writer())

			if rpt.RenderHeaders {
				err = rpt.Execute([]map[string]string{{
					"ID":    "Identity",
					"Value": "Value",
				}})
				assert.NoError(t, err)
			}
			err = rpt.Execute([...]map[string]string{
				{"ID": "c061a0839e", "Value": "one"},
				{"ID": "f10fc2e11057", "Value": "two"},
				{"ID": "1eb6fab5aa8f4b5cbfd3e66aa35e9b2a", "Value": "three"},
			})
			assert.NoError(t, err, fmt.Sprintf("original %+q, cooked %+q", tc.Format, rpt.text))
			rpt.Flush()
			assert.Equal(t, tc.Expected, buf.String(), fmt.Sprintf("original %+q, cooked %+q", tc.Format, rpt.text))
		})
	}
}

func TestFormatter_Init(t *testing.T) {
	data := [...]map[string]string{
		{"ID": "c061a0839e", "Value": "one"},
		{"ID": "f10fc2e11057", "Value": "two"},
		{"ID": "1eb6fab5aa8f4b5cbfd3e66aa35e9b2a", "Value": "three"},
	}

	buf := new(bytes.Buffer)
	fmtr, err := New(buf, t.Name()).Parse(OriginPodman, "{{range .}}{{.ID}}\t{{.Value}}\n{{end -}}")
	assert.NoError(t, err)

	err = fmtr.Execute([]map[string]string{{
		"ID":    "Identity",
		"Value": "Value",
	}})
	assert.NoError(t, err)
	err = fmtr.Execute(data)
	assert.NoError(t, err)
	fmtr.Flush()
	assert.Equal(t,
		"Identity                          Value\nc061a0839e                        one\nf10fc2e11057                      two\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a  three\n", buf.String())

	buf = new(bytes.Buffer)
	fmtr = fmtr.Init(buf, 8, 1, 1, ' ', tabwriter.Debug)

	err = fmtr.Execute([]map[string]string{{
		"ID":    "Identity",
		"Value": "Value",
	}})
	assert.NoError(t, err)
	err = fmtr.Execute(data)
	assert.NoError(t, err)
	fmtr.Flush()
	assert.Equal(t, "Identity                         |Value\nc061a0839e                       |one\nf10fc2e11057                     |two\n1eb6fab5aa8f4b5cbfd3e66aa35e9b2a |three\n", buf.String())
}

func TestFormatter_FuncsTrim(t *testing.T) {
	buf := new(bytes.Buffer)
	fmtr := New(buf, t.Name())

	fmtr, err := fmtr.Funcs(template.FuncMap{"trim": strings.TrimSpace}).Parse(OriginPodman, "{{.ID |trim}}")
	assert.NoError(t, err)

	err = fmtr.Execute(map[string]string{
		"ID": "ident  ",
	})
	assert.NoError(t, err)
	assert.Equal(t, "ident\n", buf.String())
}

func TestFormatter_FuncsJoin(t *testing.T) {
	buf := new(bytes.Buffer)
	// Add 'trim' function to ensure default 'join' function is still available
	fmtr, e := New(buf, t.Name()).Funcs(template.FuncMap{"trim": strings.TrimSpace}).Parse(OriginPodman, `{{join .ID "-"}}`)
	assert.NoError(t, e)

	err := fmtr.Execute(map[string][]string{
		"ID": {"ident1", "ident2", "ident3"},
	})
	assert.NoError(t, err)
	assert.Equal(t, "ident1-ident2-ident3\n", buf.String())
}

func TestFormatter_FuncsReplace(t *testing.T) {
	buf := new(bytes.Buffer)
	rpt := New(buf, t.Name())

	// yes, we're overriding ToUpper with ToLower :-)
	tmpl, e := rpt.Funcs(template.FuncMap{"upper": strings.ToLower}).Parse(OriginPodman, `{{.ID | lower}}`)
	assert.NoError(t, e)

	err := tmpl.Execute(map[string]string{
		"ID": "IDENT",
	})
	assert.NoError(t, err)
	assert.Equal(t, "ident\n", buf.String())
}

func TestFormatter_FuncsJSON(t *testing.T) {
	buf := new(bytes.Buffer)
	rpt := New(buf, t.Name())

	rpt, e := rpt.Parse(OriginUser, `{{json .ID}}`)
	assert.NoError(t, e)

	err := rpt.Execute([]struct {
		ID []string
	}{{
		ID: []string{"ident1", "ident2", "ident3"},
	}})
	assert.NoError(t, err)
	assert.Equal(t, `["ident1","ident2","ident3"]`+"\n", buf.String(), fmt.Sprintf("cooked %+q", rpt.text))
}

// Verify compatible output
func TestFormatter_Compatible(t *testing.T) {
	buf := new(bytes.Buffer)

	rpt, err := New(buf, t.Name()).Parse(OriginUser, "ID\t{{.ID}}")
	assert.NoError(t, err)

	err = rpt.Execute([...]map[string]string{
		{"ID": "c061a0839e"},
	})
	assert.NoError(t, err)
	rpt.Flush()

	assert.Equal(t, "ID\tc061a0839e\n", buf.String(), fmt.Sprintf("cooked %+q", rpt.text))
}