gitea.tea/cmd/flags/issue_pr_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

86 lines
2.7 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package flags
import (
stdctx "context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v3"
"gitea.dev/tea/modules/context"
)
func TestGetIssuePREditFlagsDropsEmptyCSVEntries(t *testing.T) {
cmd := cli.Command{
Name: "edit",
Flags: IssuePREditFlags,
Action: func(_ stdctx.Context, _ *cli.Command) error { return nil },
}
require.NoError(t, cmd.Run(t.Context(), []string{
"edit",
"--set-assignees", "alice, ,bob",
"--add-assignees", ",carol",
"--remove-assignees", "dave,,",
"--add-labels", " bug ,,feature ",
"--remove-labels", ",wontfix,",
}))
opts, err := GetIssuePREditFlags(&context.TeaContext{Command: &cmd})
require.NoError(t, err)
assert.Equal(t, []string{"alice", "bob"}, opts.SetAssignees,
"set-assignees should drop empty / whitespace-only entries")
assert.Equal(t, []string{"carol"}, opts.AddAssignees)
assert.Equal(t, []string{"dave"}, opts.RemoveAssignees)
assert.Equal(t, []string{"bug", "feature"}, opts.AddLabels,
"add-labels should trim whitespace AND drop empty entries")
assert.Equal(t, []string{"wontfix"}, opts.RemoveLabels)
}
func TestGetIssuePREditFlagsEmptyFlagsReturnNil(t *testing.T) {
// Regression: an empty/unset flag used to materialize as a slice
// containing one empty string (from strings.Split("", ",") -> [""]).
// After SplitCSV it must be nil so callers can use len(x) == 0
// unambiguously and downstream SDK calls don't send blank entries.
cmd := cli.Command{
Name: "edit",
Flags: IssuePREditFlags,
Action: func(_ stdctx.Context, _ *cli.Command) error { return nil },
}
require.NoError(t, cmd.Run(t.Context(), []string{"edit"}))
opts, err := GetIssuePREditFlags(&context.TeaContext{Command: &cmd})
require.NoError(t, err)
assert.Nil(t, opts.SetAssignees)
assert.Nil(t, opts.AddAssignees)
assert.Nil(t, opts.RemoveAssignees)
assert.Nil(t, opts.AddLabels)
assert.Nil(t, opts.RemoveLabels)
}
func TestGetIssuePREditFlagsPreservesSpacesInsideValues(t *testing.T) {
cmd := cli.Command{
Name: "edit",
Flags: IssuePREditFlags,
Action: func(_ stdctx.Context, _ *cli.Command) error { return nil },
}
require.NoError(t, cmd.Run(t.Context(), []string{
"edit",
"--add-labels", "Status/Need More Info,Kind/Bug",
"--set-assignees", "john doe,jane_doe",
}))
opts, err := GetIssuePREditFlags(&context.TeaContext{Command: &cmd})
require.NoError(t, err)
assert.Equal(t, []string{"Status/Need More Info", "Kind/Bug"}, opts.AddLabels,
"labels with internal spaces must be preserved")
assert.Equal(t, []string{"john doe", "jane_doe"}, opts.SetAssignees,
"assignees with internal spaces must be preserved")
}