From 8128a35f130107153842810d580203422c1d3c33 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 8 Mar 2026 17:34:42 +0000 Subject: [PATCH 1/2] fix: handle nil input in embedFiles --- pkg/cmd/flagoptions.go | 7 +++++++ pkg/cmd/flagoptions_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/cmd/flagoptions.go b/pkg/cmd/flagoptions.go index b314e9a..3b018fe 100644 --- a/pkg/cmd/flagoptions.go +++ b/pkg/cmd/flagoptions.go @@ -42,6 +42,9 @@ const ( func embedFiles(obj any, embedStyle FileEmbedStyle) (any, error) { v := reflect.ValueOf(obj) + if !v.IsValid() { + return nil, nil + } result, err := embedFilesValue(v, embedStyle) if err != nil { return nil, err @@ -51,6 +54,10 @@ func embedFiles(obj any, embedStyle FileEmbedStyle) (any, error) { // Replace "@file.txt" with the file's contents inside a value func embedFilesValue(v reflect.Value, embedStyle FileEmbedStyle) (reflect.Value, error) { + if !v.IsValid() { + return v, nil + } + // Unwrap interface values to get the concrete type if v.Kind() == reflect.Interface { if v.IsNil() { diff --git a/pkg/cmd/flagoptions_test.go b/pkg/cmd/flagoptions_test.go index e5dad4b..dcbe0fa 100644 --- a/pkg/cmd/flagoptions_test.go +++ b/pkg/cmd/flagoptions_test.go @@ -212,6 +212,12 @@ func TestEmbedFiles(t *testing.T) { want: []any{1, 2, 3, 4, 5}, wantErr: false, }, + { + name: "nil input unchanged", + input: nil, + want: nil, + wantErr: false, + }, } for _, tt := range tests { From 71d0ffcb95c29777ef0241e03263ec5bccaf8020 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 8 Mar 2026 17:36:15 +0000 Subject: [PATCH 2/2] test: cover empty piped stdin in flagOptions --- pkg/cmd/flagoptions_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/cmd/flagoptions_test.go b/pkg/cmd/flagoptions_test.go index dcbe0fa..0567019 100644 --- a/pkg/cmd/flagoptions_test.go +++ b/pkg/cmd/flagoptions_test.go @@ -6,8 +6,10 @@ import ( "path/filepath" "testing" + "github.com/beeper/desktop-api-cli/internal/apiquery" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/urfave/cli/v3" ) func TestIsUTF8TextFile(t *testing.T) { @@ -242,6 +244,30 @@ func TestEmbedFiles(t *testing.T) { } } +func TestFlagOptionsWithEmptyPipedStdinAndNoFlags(t *testing.T) { + oldStdin := os.Stdin + r, w, err := os.Pipe() + require.NoError(t, err) + require.NoError(t, w.Close()) + os.Stdin = r + t.Cleanup(func() { + os.Stdin = oldStdin + _ = r.Close() + }) + + cmd := &cli.Command{Name: "test"} + + opts, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatRepeat, + EmptyBody, + false, + ) + require.NoError(t, err) + assert.Empty(t, opts) +} + func writeTestFile(t *testing.T, dir, filename, content string) { t.Helper() path := filepath.Join(dir, filename)