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:
parent
d977030f79
commit
c700bbcb4b
@ -38,7 +38,7 @@ func NewTemplateInspector(outputStream io.Writer, tmpl *template.Template) Inspe
|
|||||||
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
|
// NewTemplateInspectorFromString creates a new TemplateInspector from a string
|
||||||
// which is compiled into a template.
|
// which is compiled into a template.
|
||||||
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
|
func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, error) {
|
||||||
if tmplStr == "" {
|
if tmplStr == "" || tmplStr == "json" {
|
||||||
return NewIndentedInspector(out), nil
|
return NewIndentedInspector(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,3 +257,57 @@ func TestTemplateInspectorRawFallbackNumber(t *testing.T) {
|
|||||||
b.Reset()
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user