Validate trail list options before auth · Entire

Validate trail list options before auth

Sessions

e74ce951b679View transcript

Changes

166 unmodified lines

}

func runTrailListAll(ctx context.Context, w, errW io.Writer, opts trailListOptions) error {
    if err := validateTrailListOptions(opts); err != nil {
        return err
    }
    return runAuthenticatedDataAPI(ctx, errW, opts.InsecureHTTP, func(ctx context.Context, client *api.Client) error {
        return runTrailListAllWithClient(ctx, w, client, opts)
    })
}

func runTrailListAllWithClient(ctx context.Context, w io.Writer, client *api.Client, opts trailListOptions) error {
func validateTrailListOptions(opts trailListOptions) error {
    if opts.Limit <= 0 {
        return errors.New("limit must be greater than 0")
    }
    _, err := parseTrailStatusFilter(opts.Status)
    return err
}

func runTrailListAllWithClient(ctx context.Context, w io.Writer, client *api.Client, opts trailListOptions) error {
    statusFilters, err := parseTrailStatusFilter(opts.Status)
    if err != nil {
        return err
    }

Mcmd/entire/cli/trail_cmd.go+9/-1

51 unmodified lines

}

func TestRunTrailListAll_ValidatesOptionsBeforeAuth(t *testing.T) {
    // No t.Parallel: SetManagerForTest mutates package-level auth state.
    store := newAuthMemStore()
    mgr := newResolveTestManager(t, store, func(context.Context, sts.ExchangeRequest) (*tokens.TokenSet, error) {
        t.Fatal("exchange should not run for invalid local options")
        return nil, errors.New("unreachable")
    })
    t.Cleanup(auth.SetManagerForTest(t, mgr))
    t.Cleanup(auth.SetResolveContextForAPIForTest(t, auth.DiscoveryUnavailableForTest))

opts := defaultTrailListOptions(false)
    opts.Limit = 0

var out, errOut bytes.Buffer
    err := runTrailListAll(t.Context(), &out, &errOut, opts)
    if err == nil {
        t.Fatal("expected validation error")
    }
    if errors.Is(err, auth.ErrNotLoggedIn) {
        t.Fatalf("got auth error %v, want local validation error", err)
    }
    if got, want := err.Error(), "limit must be greater than 0"; got != want {
        t.Fatalf("error = %q, want %q", got, want)
    }
    if errOut.Len() != 0 {
        t.Fatalf("errOut = %q, want no auth hint", errOut.String())
    }
}

func TestTrailsBasePath(t *testing.T) {
    t.Parallel()
    tests := []struct {