mirror of
https://github.com/kopia/kopia.git
synced 2026-09-12 21:30:14 -04:00
The --parallel flag was declared with kingpin .IntVar on 12 commands, which accepted negative values and led to a panic at runtime (e.g. `snapshot migrate --parallel=-1`). Declare the --parallel family as uint so kingpin rejects negative input at parse time. The uint->int conversion the callee APIs need is done through a small helper . Add CLI test that asserts the rejection message (a bare failure check passed even without the fix since the commands also fail with no repository connected). Parse via ParseFloat like kingpin's built-in int flag so the only behavior change is rejecting negatives.
32 lines
864 B
Go
32 lines
864 B
Go
package cli
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
// TestParallelismAsInt verifies the uint->int conversion used by the
|
|
// --parallel flags to prevent large values from wrapping to a negative int.
|
|
func TestParallelismAsInt(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
input uint
|
|
want int
|
|
}{
|
|
{name: "zero", input: 0, want: 0},
|
|
{name: "one", input: 1, want: 1},
|
|
{name: "typical", input: 16, want: 16},
|
|
{name: "maxint32", input: math.MaxInt32, want: math.MaxInt32},
|
|
{name: "above maxint32 clamps", input: math.MaxInt32 + 1, want: math.MaxInt32},
|
|
{name: "maxuint does not wrap negative", input: math.MaxUint, want: math.MaxInt32},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := parallelismAsInt(tc.input); got != tc.want {
|
|
t.Fatalf("parallelismAsInt(%d) = %d, want %d", tc.input, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|