Files
larksuite-cli/tests/cli_e2e/stdin_regression_test.go
liangshuo-1 619ec8c2cb fix(api): support stdin and quoted JSON inputs on Windows (#367)
* fix(api): add stdin and single-quote support for --params/--data on Windows (#64)

Windows PowerShell 5.x mangles JSON double-quotes when passing arguments
to native executables, causing --params and --data to fail with
"invalid JSON format". This commit adds two mitigations at the framework
level:

- stdin piping: `echo '{"k":"v"}' | lark-cli --params -` bypasses
  shell argument parsing entirely and works on all platforms/shells.
- single-quote stripping: cmd.exe passes literal single quotes which
  are now transparently removed before JSON parsing.

Implementation:
- New `cmdutil.ResolveInput(raw, stdin)` handles `-` (stdin), strip
  surrounding `'...'`, and plain passthrough.
- `ParseJSONMap` and `ParseOptionalBody` now accept an `io.Reader` and
  delegate to `ResolveInput` before JSON unmarshalling.
- `cmd/api` and `cmd/service` pass `IOStreams.In` and guard against
  simultaneous stdin usage by --params and --data.
- Empty stdin is rejected with a clear error message.

Closes #64

Change-Id: If21e735d0aed5c6a2d6674c1e6c898186fca3aba

* test: add stdin e2e regression coverage

Change-Id: I4e00bf1c6b6f3259f503e3414cae10fa4b34ba75
2026-04-09 19:10:50 +08:00

226 lines
6.2 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package clie2e
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCLIStdinRegression_SuccessCases(t *testing.T) {
setDryRunConfigEnv(t)
tests := []struct {
name string
req Request
assertions func(*testing.T, *Result)
}{
{
name: "api reads params from stdin",
req: Request{
Args: []string{"api", "GET", "/open-apis/test", "--params", "-", "--dry-run"},
Stdin: []byte(`{"a":"1","b":"2"}` + "\n"),
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, "GET", entry["method"])
assert.Equal(t, "/open-apis/test", entry["url"])
assert.Equal(t, map[string]any{"a": "1", "b": "2"}, entry["params"])
},
},
{
name: "api reads data from stdin",
req: Request{
Args: []string{"api", "POST", "/open-apis/test", "--data", "-", "--dry-run"},
Stdin: []byte(`{"text":"hello"}` + "\n"),
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, "POST", entry["method"])
assert.Equal(t, map[string]any{"text": "hello"}, entry["body"])
},
},
{
name: "api strips single quoted json",
req: Request{
Args: []string{"api", "GET", "/open-apis/test", "--params", `'{"a":"1"}'`, "--dry-run"},
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, map[string]any{"a": "1"}, entry["params"])
},
},
{
name: "service reads params from stdin",
req: Request{
Args: []string{
"calendar", "events", "instance_view",
"--as", "bot",
"--params", "-",
"--dry-run",
},
Stdin: []byte(`{"calendar_id":"primary","start_time":"1700000000","end_time":"1700003600"}` + "\n"),
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, "GET", entry["method"])
assert.Equal(t, "/open-apis/calendar/v4/calendars/primary/events/instance_view", entry["url"])
assert.Equal(t, map[string]any{
"start_time": "1700000000",
"end_time": "1700003600",
}, entry["params"])
},
},
{
name: "service reads data from stdin",
req: Request{
Args: []string{
"task", "tasks", "create",
"--as", "bot",
"--data", "-",
"--dry-run",
},
Stdin: []byte(`{"summary":"stdin regression"}` + "\n"),
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, "POST", entry["method"])
assert.Equal(t, "/open-apis/task/v2/tasks", entry["url"])
assert.Equal(t, map[string]any{"summary": "stdin regression"}, entry["body"])
},
},
{
name: "service strips single quoted json",
req: Request{
Args: []string{
"task", "tasks", "create",
"--as", "bot",
"--data", `'{"summary":"single quote"}'`,
"--dry-run",
},
},
assertions: func(t *testing.T, result *Result) {
entry := firstDryRunRequest(t, result.Stdout)
assert.Equal(t, map[string]any{"summary": "single quote"}, entry["body"])
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := RunCmd(context.Background(), tt.req)
require.NoError(t, err)
require.NoError(t, result.RunErr, "stderr:\n%s", result.Stderr)
result.AssertExitCode(t, 0)
tt.assertions(t, result)
})
}
}
func TestCLIStdinRegression_ErrorCases(t *testing.T) {
setDryRunConfigEnv(t)
tests := []struct {
name string
req Request
wantMessage string
}{
{
name: "api rejects empty stdin",
req: Request{
Args: []string{"api", "GET", "/open-apis/test", "--params", "-", "--dry-run"},
Stdin: []byte{},
},
wantMessage: "--params: stdin is empty (did you forget to pipe input?)",
},
{
name: "api rejects double stdin",
req: Request{
Args: []string{"api", "POST", "/open-apis/test", "--params", "-", "--data", "-", "--dry-run"},
Stdin: []byte(`{"x":1}` + "\n"),
},
wantMessage: "--params and --data cannot both read from stdin (-)",
},
{
name: "service rejects empty stdin",
req: Request{
Args: []string{
"calendar", "events", "instance_view",
"--as", "bot",
"--params", "-",
"--dry-run",
},
Stdin: []byte{},
},
wantMessage: "--params: stdin is empty (did you forget to pipe input?)",
},
{
name: "service rejects double stdin",
req: Request{
Args: []string{
"task", "tasks", "create",
"--as", "bot",
"--params", "-",
"--data", "-",
"--dry-run",
},
Stdin: []byte(`{"summary":"stdin regression"}` + "\n"),
},
wantMessage: "--params and --data cannot both read from stdin (-)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := RunCmd(context.Background(), tt.req)
require.NoError(t, err)
assert.Error(t, result.RunErr)
result.AssertExitCode(t, 2)
envelope, ok := result.StderrJSON(t).(map[string]any)
require.True(t, ok)
assert.Equal(t, false, envelope["ok"])
errDetail, ok := envelope["error"].(map[string]any)
require.True(t, ok)
assert.Equal(t, "validation", errDetail["type"])
assert.Equal(t, tt.wantMessage, errDetail["message"])
})
}
}
func setDryRunConfigEnv(t *testing.T) {
t.Helper()
t.Setenv("LARKSUITE_CLI_APP_ID", "app")
t.Setenv("LARKSUITE_CLI_APP_SECRET", "secret")
t.Setenv("LARKSUITE_CLI_BRAND", "feishu")
}
func firstDryRunRequest(t *testing.T, stdout string) map[string]any {
t.Helper()
const prefix = "=== Dry Run ===\n"
if !strings.HasPrefix(stdout, prefix) {
t.Fatalf("expected dry-run prefix, got:\n%s", stdout)
}
var payload map[string]any
if err := json.Unmarshal([]byte(strings.TrimPrefix(stdout, prefix)), &payload); err != nil {
t.Fatalf("parse dry-run payload: %v\nstdout:\n%s", err, stdout)
}
apiEntries, ok := payload["api"].([]any)
require.True(t, ok, "payload missing api array: %#v", payload)
require.Len(t, apiEntries, 1)
entry, ok := apiEntries[0].(map[string]any)
require.True(t, ok, "api entry is not an object: %#v", apiEntries[0])
return entry
}