Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/cmd/flagoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
32 changes: 32 additions & 0 deletions pkg/cmd/flagoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -212,6 +214,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 {
Expand All @@ -236,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)
Expand Down