Skip to content

Commit

Permalink
fix: Print valid JSON/YAML when workflow list empty #10873 (#11681)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goorzhel committed Aug 26, 2023
1 parent 11a9313 commit cb8dbbc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion util/printer/workflow-printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import (

func PrintWorkflows(workflows wfv1.Workflows, out io.Writer, opts PrintOpts) error {
if len(workflows) == 0 {
_, _ = fmt.Fprintln(out, "No workflows found")
if opts.Output == "json" || opts.Output == "yaml" {
_, _ = fmt.Fprintln(out, "[]")
} else {
_, _ = fmt.Fprintln(out, "No workflows found")
}
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions util/printer/workflow-printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ func TestPrintWorkflows(t *testing.T) {
var b bytes.Buffer
assert.NoError(t, PrintWorkflows(emptyWorkflows, &b, PrintOpts{}))
assert.Equal(t, `No workflows found
`, b.String())
})
t.Run("EmptyJSON", func(t *testing.T) {
var b bytes.Buffer
assert.NoError(t, PrintWorkflows(emptyWorkflows, &b, PrintOpts{Output: "json"}))
assert.Equal(t, `[]
`, b.String())
})
t.Run("EmptyYAML", func(t *testing.T) {
var b bytes.Buffer
assert.NoError(t, PrintWorkflows(emptyWorkflows, &b, PrintOpts{Output: "yaml"}))
assert.Equal(t, `[]
`, b.String())
})
t.Run("Default", func(t *testing.T) {
Expand Down

0 comments on commit cb8dbbc

Please sign in to comment.