Add specific "json" value to format flag with inspect commands to output json, as empty flag does.

Added tests on that new behavior.

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Silvin Lubecki 2021-01-18 11:19:37 +01:00 committed by Sebastiaan van Stijn
parent d977030f79
commit c700bbcb4b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 55 additions and 1 deletions

View File

@ -38,7 +38,7 @@ func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspe
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
// which is compiled into a template.
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
if tmplStr == "" {
if tmplStr == "" || tmplStr == "json" {
return NewIndentedInspector(out), nil
}

View File

@ -257,3 +257,57 @@ func TestTemplateInspectorRawFallbackNumber(t *testing.T) {
b.Reset()
}
}
func TestNewTemplateInspectorFromString(t *testing.T) {
testCases := []struct {
name string
template string
expected string
}{
{
name: "empty template outputs json by default",
template: "",
expected: `[
{
"Name": "test"
}
]
`,
},
{
name: "json specific value outputs json",
template: "json",
expected: `[
{
"Name": "test"
}
]
`,
},
{
name: "template is applied",
template: "{{.Name}}",
expected: "test\n",
},
}
value := struct {
Name string
}{
"test",
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b := new(bytes.Buffer)
i, err := NewTemplateInspectorFromString(b, tc.template)
assert.NilError(t, err)
err = i.Inspect(value, nil)
assert.NilError(t, err)
err = i.Flush()
assert.NilError(t, err)
assert.Equal(t, b.String(), tc.expected)
})
}
}