Files
larksuite-cli/shortcuts/common/runner_args_test.go
MaxHuang22 7158dc2f3c fix: reject positional arguments in shortcuts (#227)
* fix: reject positional arguments in shortcuts with clear error

Shortcuts silently ignored positional arguments (e.g. `lark-cli docs
+search "hello"`), causing empty results. Add Args validator to all
declarative shortcuts so cobra prints usage and a clear error message
telling users to pass values via flags instead.

Change-Id: I7579f9c871138cf91dd5f5d8c1d51bda3f77a1db

* fix: address PR review comments

- Remove unused *Shortcut parameter from rejectPositionalArgs
- Show all positional args in error message instead of only the first
- Add test case for multiple positional arguments

Change-Id: Ifea92d09ddabcd35fbf2db98d9888d18af59b894
2026-04-08 15:11:36 +08:00

59 lines
1.5 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package common
import (
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestRejectPositionalArgs_WithArgs(t *testing.T) {
t.Parallel()
validator := rejectPositionalArgs()
err := validator(&cobra.Command{}, []string{"hello"})
if err == nil {
t.Fatal("expected error for positional arg, got nil")
}
if !strings.Contains(err.Error(), "positional arguments are not supported") {
t.Errorf("expected positional args rejection message, got: %v", err)
}
if !strings.Contains(err.Error(), `"hello"`) {
t.Errorf("expected the positional arg value in error, got: %v", err)
}
}
func TestRejectPositionalArgs_MultipleArgs(t *testing.T) {
t.Parallel()
validator := rejectPositionalArgs()
err := validator(&cobra.Command{}, []string{"hello", "world"})
if err == nil {
t.Fatal("expected error for multiple positional args, got nil")
}
if !strings.Contains(err.Error(), "positional arguments are not supported") {
t.Errorf("unexpected error message: %v", err)
}
if !strings.Contains(err.Error(), "hello") || !strings.Contains(err.Error(), "world") {
t.Errorf("expected all positional args in error, got: %v", err)
}
}
func TestRejectPositionalArgs_NoArgs(t *testing.T) {
t.Parallel()
validator := rejectPositionalArgs()
if err := validator(&cobra.Command{}, nil); err != nil {
t.Fatalf("expected no error for nil args, got: %v", err)
}
if err := validator(&cobra.Command{}, []string{}); err != nil {
t.Fatalf("expected no error for empty args, got: %v", err)
}
}