gitea.tea/cmd/flags/csvflag_test.go
Ross Golder c48e2af033
fix(flags): drop empty/whitespace entries when parsing CSV flags
Comma-separated flags (`--assignees`, `--labels`, `--add/remove-labels`,
`--add/remove-reviewers`, `--events`, login `--scopes`) silently accepted
stray commas and whitespace, producing values like `[""]` for empty input
or `["alice", "", "bob"]` for `--assignees=alice,,bob`. Those blanks
were then forwarded to the Gitea SDK, where they either triggered obscure
server errors or silently mutated state in unexpected ways.

Root cause: a mix of `strings.Split(s, ",")` and ad-hoc trim loops, none
of which rejected empty segments.

Introduce `modules/utils.SplitCSV` as the single parser for all CSV flag
values. It:

- returns `nil` for an empty or whitespace-only input (so callers can use
  `len(x) == 0` to mean "not set", and `cmd.IsSet("...")` keeps
  working),
- drops empty and whitespace-only segments anywhere in the input,
- otherwise trims surrounding whitespace from each remaining segment.

`cmd/flags.SplitCSV` is re-exported from `cmd/flags/csvflag.go` so
existing call sites read `flags.SplitCSV` without depending on a leaf
package that `modules/task` already imports them.

Convert the remaining raw `strings.Split(..., ",")` sites:

- `cmd/flags/issue_pr.go` (issue/pr --assignees, --labels and the
  set/add/remove variants)
- `cmd/pulls/edit.go` (`--add-reviewers`, `--remove-reviewers`)
- `cmd/webhooks/create.go`, `cmd/webhooks/update.go` (`--events`)
- `modules/task/login_create.go` (login `--scopes`)

Drop the now-unused `"strings"` imports from the three cmd/* files.

Tests:

- `modules/utils/splitcsv_test.go` — table-driven coverage for empty,
  whitespace-only, leading/trailing commas, internal empty segments,
  surrounding whitespace, and re-imported identity with `flags.SplitCSV`.
- `cmd/flags/csvflag_test.go` — `GetValues` happy path, error path,
  empty entry drop, and AvailableFields variants.
- `cmd/flags/issue_pr_test.go` — regression test for
  `GetIssuePREditFlags` proving `SetAssignees/AddAssignees/...`
  produce `nil` (not `[""]`) for empty input and trimmed slices for
  mixed inputs.
2026-09-10 17:31:08 +07:00

87 lines
2.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package flags
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v3"
)
func TestCsvFlagGetValues(t *testing.T) {
flag := NewCsvFlag("tags", "tag list", nil, nil, nil)
cmd := cli.Command{
Name: "test-csv",
Flags: []cli.Flag{flag},
Action: func(_ context.Context, _ *cli.Command) error { return nil },
}
cases := []struct {
name string
arg string
expected []string
}{
{name: "empty returns nil", arg: "", expected: nil},
{name: "single value", arg: "alice", expected: []string{"alice"}},
{name: "two values", arg: "alice,bob", expected: []string{"alice", "bob"}},
{name: "trailing comma dropped", arg: "alice,bob,", expected: []string{"alice", "bob"}},
{name: "leading comma dropped", arg: ",alice,bob", expected: []string{"alice", "bob"}},
{name: "consecutive commas dropped", arg: "alice,,bob", expected: []string{"alice", "bob"}},
{name: "only commas returns nil", arg: ",,,", expected: nil},
{name: "whitespace trimmed", arg: " alice , bob ", expected: []string{"alice", "bob"}},
{name: "whitespace-only entries dropped", arg: "alice, ,bob", expected: []string{"alice", "bob"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
require.NoError(t, cmd.Run(t.Context(), []string{"test-csv", "--tags", tc.arg}))
got, err := flag.GetValues(&cmd)
require.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
}
func TestCsvFlagGetValuesAvailableFields(t *testing.T) {
flag := NewCsvFlag("kind", "kind filter", []string{"K"}, []string{"issues", "pulls"}, nil)
cmd := cli.Command{
Name: "test-csv",
Flags: []cli.Flag{flag},
Action: func(_ context.Context, _ *cli.Command) error { return nil },
}
t.Run("valid value", func(t *testing.T) {
require.NoError(t, cmd.Run(t.Context(), []string{"test-csv", "--kind", "issues"}))
got, err := flag.GetValues(&cmd)
require.NoError(t, err)
assert.Equal(t, []string{"issues"}, got)
})
t.Run("invalid value", func(t *testing.T) {
require.NoError(t, cmd.Run(t.Context(), []string{"test-csv", "--kind", "bogus"}))
_, err := flag.GetValues(&cmd)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid field 'bogus'")
})
// Regression: a stray comma in the middle of an otherwise-valid value
// no longer makes the whole flag invalid.
t.Run("trailing comma after valid value", func(t *testing.T) {
require.NoError(t, cmd.Run(t.Context(), []string{"test-csv", "--kind", "issues,"}))
got, err := flag.GetValues(&cmd)
require.NoError(t, err)
assert.Equal(t, []string{"issues"}, got)
})
}
func TestSplitCSVReExport(t *testing.T) {
// The flags.SplitCSV re-export must behave identically to
// utils.SplitCSV; this guards against future drift between the
// two entry points.
assert.Equal(t, []string{"alice", "bob"}, SplitCSV("alice,bob"))
assert.Nil(t, SplitCSV(""))
}