Skip to content

Commit c2a3524

Browse files
committed
fix(tui): encode empty json rows consistently
1 parent f3fd8fc commit c2a3524

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

tui/tui.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func Browse(ctx context.Context, opts BrowseOptions) error {
7777
opts.Stdout = os.Stdout
7878
}
7979
if opts.JSON {
80+
if opts.Rows == nil {
81+
opts.Rows = []Row{}
82+
}
8083
enc := json.NewEncoder(opts.Stdout)
8184
enc.SetIndent("", " ")
8285
return enc.Encode(opts.Rows)

tui/tui_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ func TestBrowseJSONUsesUniversalRows(t *testing.T) {
3535
}
3636
}
3737

38+
func TestBrowseJSONEncodesNilRowsAsEmptyArray(t *testing.T) {
39+
var out bytes.Buffer
40+
if err := Browse(context.Background(), BrowseOptions{AppName: "discrawl", JSON: true, Stdout: &out}); err != nil {
41+
t.Fatalf("Browse json: %v", err)
42+
}
43+
if strings.TrimSpace(out.String()) != "[]" {
44+
t.Fatalf("json = %q, want []", out.String())
45+
}
46+
}
47+
3848
func TestRowItemUsesSharedArchiveShape(t *testing.T) {
3949
item := Row{
4050
Source: "discord",

0 commit comments

Comments
 (0)