fix(bonfire): read the =value forms of the global flags and use the parsed globals in the error block (IN-02, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:29:16 +01:00
parent 6294ac4141
commit e34d113c80
6 changed files with 140 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ package bonfire
import (
"bytes"
"context"
"errors"
"fmt"
"slices"
"strings"
"testing"
@@ -41,6 +43,23 @@ var peekCases = []struct {
{"combined -vq", []string{"-vq", "migrate"}, Globals{Verbosity: 1, Quiet: true, NoInteraction: true}, true},
{"combined -nvv", []string{"migrate", "-nvv"}, Globals{Verbosity: 2, NoInteraction: true}, true},
{"everything", []string{"-vv", "--env=dev", "migrate", "-n", "--no-ansi"}, Globals{Verbosity: 2, NoInteraction: true, Color: ColorNever, Env: "dev"}, true},
// IN-02: the =value forms, read with pflag's value rules.
{"--verbose=2", []string{"migrate", "--verbose=2"}, Globals{Verbosity: 2}, true},
{"-v=3", []string{"migrate", "-v=3"}, Globals{Verbosity: 3}, true},
{"-vv=1", []string{"migrate", "-vv=1"}, Globals{Verbosity: 1}, true},
{"-v --verbose=2 -v", []string{"migrate", "-v", "--verbose=2", "-v"}, Globals{Verbosity: 3}, true},
{"--quiet=true", []string{"migrate", "--quiet=true"}, Globals{Quiet: true, NoInteraction: true}, true},
{"-q --quiet=false", []string{"migrate", "-q", "--quiet=false"}, Globals{}, true},
{"-q=false", []string{"migrate", "-q=false"}, Globals{}, true},
{"--no-interaction=1", []string{"migrate", "--no-interaction=1"}, Globals{NoInteraction: true}, true},
{"-n=t", []string{"migrate", "-n=t"}, Globals{NoInteraction: true}, true},
{"-qn=false", []string{"migrate", "-qn=false"}, Globals{Quiet: true, NoInteraction: true}, true},
{"--ansi=false", []string{"migrate", "--ansi=false"}, Globals{Color: ColorAuto}, true},
{"--ansi --ansi=false", []string{"migrate", "--ansi", "--ansi=false"}, Globals{Color: ColorAuto}, true},
{"--ansi=true", []string{"migrate", "--ansi=true"}, Globals{Color: ColorAlways}, true},
{"--no-ansi=true", []string{"migrate", "--no-ansi=true"}, Globals{Color: ColorNever}, true},
{"--no-ansi --ansi=false", []string{"migrate", "--no-ansi", "--ansi=false"}, Globals{Color: ColorNever}, true},
{"unparsable values ignored", []string{"migrate", "-v", "--verbose=x", "--quiet=maybe", "--ansi=sure"}, Globals{Verbosity: 1}, false},
{"unknown flags ignored", []string{"--bogus", "-x", "migrate", "-", "--env"}, Globals{}, false},
{"--env without a value", []string{"migrate", "--env"}, Globals{}, false},
{"empty", nil, Globals{}, false},
@@ -121,3 +140,29 @@ func TestGlobalFlagsAgree(t *testing.T) {
})
}
}
// TestErrorBlockUsesParsedGlobals (IN-02): once cobra has parsed the
// command's flags, the error block uses cobra's globals. In
// "migrate --name -v" the -v is the value of --name, so the block stays at
// verbosity 0 although PeekGlobals reads -v.
func TestErrorBlockUsesParsedGlobals(t *testing.T) {
plainEnv(t)
cmd := Command{
Name: "migrate",
Description: "Run the migrations",
Flags: []Flag{{Name: "name", Description: "A name"}},
Run: func(_ context.Context, in Input, _ Output) error {
if v, _ := in.Flag("name"); v != "-v" {
t.Errorf("--name = %q, want -v", v)
}
return fmt.Errorf("outer: %w", errors.New("inner"))
},
}
_, errOut, code := runGolden(t, []Command{cmd}, "migrate", "--name", "-v")
if code != ExitFailure {
t.Fatalf("exit %d, want %d\n%s", code, ExitFailure, errOut)
}
if !strings.Contains(errOut, "Run with -v for details.") || strings.Contains(errOut, "Caused by:") {
t.Errorf("block does not use the parsed verbosity 0:\n%s", errOut)
}
}