diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index aef31e62df..15dbea14ce 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -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 } diff --git a/cli/command/inspect/inspector_test.go b/cli/command/inspect/inspector_test.go index 702df3d8cc..74c49b2f82 100644 --- a/cli/command/inspect/inspector_test.go +++ b/cli/command/inspect/inspector_test.go @@ -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) + }) + } +}