From 5e995efdaf516eacb3ca05d61c34d785eb6d7f0f Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 16 Nov 2026 15:13:42 +0000 Subject: [PATCH 1/7] selftest: add TUI commands for live-terminal testing Add hidden `databricks selftest tui` commands that drive the production cmdio, tableview and huh/appkit code paths so their observable behaviour can be checked in a live terminal. They unblock testing while the CLI migrates its Charm TUI libraries to the charm.land v2 modules: the same checks can run on the current v1 build and on the v2 build side by side. - keys: print each key bubbletea decodes; --raw prints raw stdin bytes - profile: print the color, background and env decisions for stdout/stderr - colors: --stream stdout|stderr, --lipgloss adds color depth swatches - pager: cmdio.RenderIterator over a fake iterator with --fail-after - table: tableview.Run over generated rows - huh: one form field with the appkit theme - spinner: --duration sets the total run time Only selftest code changes. No production behaviour changes. Co-authored-by: Isaac --- acceptance/selftest/tui/colors/out.test.toml | 1 + acceptance/selftest/tui/colors/output.txt | 62 ++++++++ acceptance/selftest/tui/colors/script | 18 +++ acceptance/selftest/tui/huh/out.test.toml | 1 + acceptance/selftest/tui/huh/output.txt | 7 + acceptance/selftest/tui/huh/script | 2 + acceptance/selftest/tui/keys/out.test.toml | 1 + acceptance/selftest/tui/keys/output.txt | 23 +++ acceptance/selftest/tui/keys/script | 8 + acceptance/selftest/tui/pager/out.test.toml | 1 + acceptance/selftest/tui/pager/output.txt | 29 ++++ acceptance/selftest/tui/pager/script | 11 ++ acceptance/selftest/tui/profile/out.test.toml | 1 + acceptance/selftest/tui/profile/output.txt | 30 ++++ acceptance/selftest/tui/profile/script | 5 + acceptance/selftest/tui/spinner/out.test.toml | 1 + acceptance/selftest/tui/spinner/output.txt | 5 + acceptance/selftest/tui/spinner/script | 2 + acceptance/selftest/tui/table/out.test.toml | 1 + acceptance/selftest/tui/table/output.txt | 12 ++ acceptance/selftest/tui/table/script | 3 + acceptance/selftest/tui/test.toml | 12 ++ cmd/selftest/tui/colors.go | 78 ++++++++- cmd/selftest/tui/huh.go | 79 +++++++++ cmd/selftest/tui/keys.go | 150 ++++++++++++++++++ cmd/selftest/tui/keys_test.go | 26 +++ cmd/selftest/tui/pager.go | 127 +++++++++++++++ cmd/selftest/tui/pager_test.go | 48 ++++++ cmd/selftest/tui/profile.go | 49 ++++++ cmd/selftest/tui/spinner.go | 12 +- cmd/selftest/tui/table.go | 49 ++++++ cmd/selftest/tui/tui.go | 5 + 32 files changed, 849 insertions(+), 10 deletions(-) create mode 100644 acceptance/selftest/tui/colors/out.test.toml create mode 100644 acceptance/selftest/tui/colors/output.txt create mode 100644 acceptance/selftest/tui/colors/script create mode 100644 acceptance/selftest/tui/huh/out.test.toml create mode 100644 acceptance/selftest/tui/huh/output.txt create mode 100644 acceptance/selftest/tui/huh/script create mode 100644 acceptance/selftest/tui/keys/out.test.toml create mode 100644 acceptance/selftest/tui/keys/output.txt create mode 100644 acceptance/selftest/tui/keys/script create mode 100644 acceptance/selftest/tui/pager/out.test.toml create mode 100644 acceptance/selftest/tui/pager/output.txt create mode 100644 acceptance/selftest/tui/pager/script create mode 100644 acceptance/selftest/tui/profile/out.test.toml create mode 100644 acceptance/selftest/tui/profile/output.txt create mode 100644 acceptance/selftest/tui/profile/script create mode 100644 acceptance/selftest/tui/spinner/out.test.toml create mode 100644 acceptance/selftest/tui/spinner/output.txt create mode 100644 acceptance/selftest/tui/spinner/script create mode 100644 acceptance/selftest/tui/table/out.test.toml create mode 100644 acceptance/selftest/tui/table/output.txt create mode 100644 acceptance/selftest/tui/table/script create mode 100644 acceptance/selftest/tui/test.toml create mode 100644 cmd/selftest/tui/huh.go create mode 100644 cmd/selftest/tui/keys.go create mode 100644 cmd/selftest/tui/keys_test.go create mode 100644 cmd/selftest/tui/pager.go create mode 100644 cmd/selftest/tui/pager_test.go create mode 100644 cmd/selftest/tui/profile.go create mode 100644 cmd/selftest/tui/table.go diff --git a/acceptance/selftest/tui/colors/out.test.toml b/acceptance/selftest/tui/colors/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/colors/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/colors/output.txt b/acceptance/selftest/tui/colors/output.txt new file mode 100644 index 00000000000..06e687db692 --- /dev/null +++ b/acceptance/selftest/tui/colors/output.txt @@ -0,0 +1,62 @@ + +=== Default stream is stderr, so stdout is empty + +>>> [CLI] selftest tui colors +red: the quick brown fox jumps over the lazy dog +green: the quick brown fox jumps over the lazy dog +yellow: the quick brown fox jumps over the lazy dog +blue: the quick brown fox jumps over the lazy dog +cyan: the quick brown fox jumps over the lazy dog +hiblack: the quick brown fox jumps over the lazy dog +hiblue: the quick brown fox jumps over the lazy dog + +=== --stream stdout writes the swatches to stdout +red: the quick brown fox jumps over the lazy dog +green: the quick brown fox jumps over the lazy dog +yellow: the quick brown fox jumps over the lazy dog +blue: the quick brown fox jumps over the lazy dog +cyan: the quick brown fox jumps over the lazy dog +hiblack: the quick brown fox jumps over the lazy dog +hiblue: the quick brown fox jumps over the lazy dog + +=== --stream stderr writes to stderr +red: the quick brown fox jumps over the lazy dog +green: the quick brown fox jumps over the lazy dog +yellow: the quick brown fox jumps over the lazy dog +blue: the quick brown fox jumps over the lazy dog +cyan: the quick brown fox jumps over the lazy dog +hiblack: the quick brown fox jumps over the lazy dog +hiblue: the quick brown fox jumps over the lazy dog + +=== Piped output has no escape codes, even with --lipgloss + +>>> NO_COLOR=1 [CLI] selftest tui colors --lipgloss +red: the quick brown fox jumps over the lazy dog +green: the quick brown fox jumps over the lazy dog +yellow: the quick brown fox jumps over the lazy dog +blue: the quick brown fox jumps over the lazy dog +cyan: the quick brown fox jumps over the lazy dog +hiblack: the quick brown fox jumps over the lazy dog +hiblue: the quick brown fox jumps over the lazy dog +truecolor: +256-color: + +=== --lipgloss prints both swatch rows + +>>> [CLI] selftest tui colors --lipgloss +red: the quick brown fox jumps over the lazy dog +green: the quick brown fox jumps over the lazy dog +yellow: the quick brown fox jumps over the lazy dog +blue: the quick brown fox jumps over the lazy dog +cyan: the quick brown fox jumps over the lazy dog +hiblack: the quick brown fox jumps over the lazy dog +hiblue: the quick brown fox jumps over the lazy dog +truecolor: +256-color: + +=== Unknown stream is rejected + +>>> [CLI] selftest tui colors --stream file +Error: --stream must be stdout or stderr, got "file" + +Exit code: 1 diff --git a/acceptance/selftest/tui/colors/script b/acceptance/selftest/tui/colors/script new file mode 100644 index 00000000000..c8f38dea3de --- /dev/null +++ b/acceptance/selftest/tui/colors/script @@ -0,0 +1,18 @@ +title "Default stream is stderr, so stdout is empty" +$CLI selftest tui colors 2>/dev/null | contains.py '!red:' +trace $CLI selftest tui colors 2>&1 >/dev/null | contains.py 'red:' 'hiblue:' + +title "--stream stdout writes the swatches to stdout" +$CLI selftest tui colors --stream stdout 2>/dev/null + +title "--stream stderr writes to stderr" +$CLI selftest tui colors --stream stderr 2>&1 >/dev/null + +title "Piped output has no escape codes, even with --lipgloss" +trace NO_COLOR=1 $CLI selftest tui colors --lipgloss 2>&1 | contains.py '!'$'\x1b' + +title "--lipgloss prints both swatch rows" +trace $CLI selftest tui colors --lipgloss 2>&1 | contains.py 'truecolor:' '256-color:' + +title "Unknown stream is rejected" +errcode trace $CLI selftest tui colors --stream file diff --git a/acceptance/selftest/tui/huh/out.test.toml b/acceptance/selftest/tui/huh/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/huh/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/huh/output.txt b/acceptance/selftest/tui/huh/output.txt new file mode 100644 index 00000000000..61c4b271c9c --- /dev/null +++ b/acceptance/selftest/tui/huh/output.txt @@ -0,0 +1,7 @@ + +=== Unknown field type is rejected + +>>> [CLI] selftest tui huh --field bogus +Error: --field must be one of input, select, confirm, multiselect, got "bogus" + +Exit code: 1 diff --git a/acceptance/selftest/tui/huh/script b/acceptance/selftest/tui/huh/script new file mode 100644 index 00000000000..2b4373105b3 --- /dev/null +++ b/acceptance/selftest/tui/huh/script @@ -0,0 +1,2 @@ +title "Unknown field type is rejected" +errcode trace $CLI selftest tui huh --field bogus diff --git a/acceptance/selftest/tui/keys/out.test.toml b/acceptance/selftest/tui/keys/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/keys/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/keys/output.txt b/acceptance/selftest/tui/keys/output.txt new file mode 100644 index 00000000000..d0e2c572ea4 --- /dev/null +++ b/acceptance/selftest/tui/keys/output.txt @@ -0,0 +1,23 @@ + +=== Each decoded key is printed; two Ctrl+C exit + +>>> [CLI] selftest tui keys --timeout 5s +[?25l[?2004hkey="a" type=-1 runes="a" alt=false paste=false +key="ctrl+c" type=3 runes="" alt=false paste=false +key="ctrl+c" type=3 runes="" alt=false paste=false + + + +[?2004l[?25h[?1002l[?1003l[?1006l +=== --raw prints the bytes read from stdin + +>>> [CLI] selftest tui keys --raw +raw bytes=61 62 03 03 text="ab\x03\x03" + +=== --timeout exits without any input + +>>> [CLI] selftest tui keys --timeout 1s +[?25l[?2004h + + +[?2004l[?25h[?1002l[?1003l[?1006l \ No newline at end of file diff --git a/acceptance/selftest/tui/keys/script b/acceptance/selftest/tui/keys/script new file mode 100644 index 00000000000..7ea18ecce5c --- /dev/null +++ b/acceptance/selftest/tui/keys/script @@ -0,0 +1,8 @@ +title "Each decoded key is printed; two Ctrl+C exit" +printf 'a\x03\x03' | trace $CLI selftest tui keys --timeout 5s 2>&1 | contains.py 'key="a" type=-1 runes="a"' 'key="ctrl+c" type=3' + +title "--raw prints the bytes read from stdin" +printf 'ab\x03\x03' | trace $CLI selftest tui keys --raw 2>&1 + +title "--timeout exits without any input" +sleep 2 | trace $CLI selftest tui keys --timeout 1s 2>&1 | contains.py '!key=' diff --git a/acceptance/selftest/tui/pager/out.test.toml b/acceptance/selftest/tui/pager/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/pager/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/pager/output.txt b/acceptance/selftest/tui/pager/output.txt new file mode 100644 index 00000000000..ed71399b270 --- /dev/null +++ b/acceptance/selftest/tui/pager/output.txt @@ -0,0 +1,29 @@ + +=== Non-interactive output uses the non-paged renderer + +>>> [CLI] selftest tui pager --items 3 +Name State +item-0001 RUNNING +item-0002 TERMINATED +item-0003 PENDING + +=== An injected error after 2 rows is returned + +>>> [CLI] selftest tui pager --items 5 --fail-after 2 +Error: injected iterator error after 2 rows + +Exit code: 1 + +=== --fail-after 0 fails on the first Next + +>>> [CLI] selftest tui pager --items 5 --fail-after 0 +Error: injected iterator error after 0 rows + +Exit code: 1 + +=== Negative --items is rejected + +>>> [CLI] selftest tui pager --items -1 +Error: --items must be non-negative, got -1 + +Exit code: 1 diff --git a/acceptance/selftest/tui/pager/script b/acceptance/selftest/tui/pager/script new file mode 100644 index 00000000000..28b33b1722b --- /dev/null +++ b/acceptance/selftest/tui/pager/script @@ -0,0 +1,11 @@ +title "Non-interactive output uses the non-paged renderer" +trace $CLI selftest tui pager --items 3 + +title "An injected error after 2 rows is returned" +errcode trace $CLI selftest tui pager --items 5 --fail-after 2 + +title "--fail-after 0 fails on the first Next" +errcode trace $CLI selftest tui pager --items 5 --fail-after 0 + +title "Negative --items is rejected" +errcode trace $CLI selftest tui pager --items -1 diff --git a/acceptance/selftest/tui/profile/out.test.toml b/acceptance/selftest/tui/profile/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/profile/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/profile/output.txt b/acceptance/selftest/tui/profile/output.txt new file mode 100644 index 00000000000..df1a714def2 --- /dev/null +++ b/acceptance/selftest/tui/profile/output.txt @@ -0,0 +1,30 @@ + +=== Piped stdout and stderr never get color + +>>> [CLI] selftest tui profile +stdout: supports_color=false profile=Ascii dark_background=true +stderr: supports_color=false profile=Ascii dark_background=true +env: + TERM=xterm-256color + COLORTERM=(unset or empty) + CLICOLOR=(unset or empty) + CLICOLOR_FORCE=(unset or empty) + NO_COLOR=(unset or empty) + CI=(unset or empty) + TERM_PROGRAM=(unset or empty) + WT_SESSION=(unset or empty) + +=== NO_COLOR is reported in the env section + +>>> NO_COLOR=1 [CLI] selftest tui profile +stdout: supports_color=false profile=Ascii dark_background=true +stderr: supports_color=false profile=Ascii dark_background=true +env: + TERM=xterm-256color + COLORTERM=(unset or empty) + CLICOLOR=(unset or empty) + CLICOLOR_FORCE=(unset or empty) + NO_COLOR=1 + CI=(unset or empty) + TERM_PROGRAM=(unset or empty) + WT_SESSION=(unset or empty) diff --git a/acceptance/selftest/tui/profile/script b/acceptance/selftest/tui/profile/script new file mode 100644 index 00000000000..499165d23a0 --- /dev/null +++ b/acceptance/selftest/tui/profile/script @@ -0,0 +1,5 @@ +title "Piped stdout and stderr never get color" +trace $CLI selftest tui profile + +title "NO_COLOR is reported in the env section" +trace NO_COLOR=1 $CLI selftest tui profile diff --git a/acceptance/selftest/tui/spinner/out.test.toml b/acceptance/selftest/tui/spinner/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/spinner/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/spinner/output.txt b/acceptance/selftest/tui/spinner/output.txt new file mode 100644 index 00000000000..62f27b8e95d --- /dev/null +++ b/acceptance/selftest/tui/spinner/output.txt @@ -0,0 +1,5 @@ + +=== --duration splits the total across the spinner messages + +>>> [CLI] selftest tui spinner --duration 500ms +Spinner test complete diff --git a/acceptance/selftest/tui/spinner/script b/acceptance/selftest/tui/spinner/script new file mode 100644 index 00000000000..c1a2f3b8f48 --- /dev/null +++ b/acceptance/selftest/tui/spinner/script @@ -0,0 +1,2 @@ +title "--duration splits the total across the spinner messages" +trace $CLI selftest tui spinner --duration 500ms diff --git a/acceptance/selftest/tui/table/out.test.toml b/acceptance/selftest/tui/table/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/selftest/tui/table/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/selftest/tui/table/output.txt b/acceptance/selftest/tui/table/output.txt new file mode 100644 index 00000000000..02719820acf --- /dev/null +++ b/acceptance/selftest/tui/table/output.txt @@ -0,0 +1,12 @@ + +=== Flag validation runs before the TTY is needed + +>>> [CLI] selftest tui table --cols 0 +Error: --cols must be at least 1, got 0 + +Exit code: 1 + +>>> [CLI] selftest tui table --rows -1 +Error: --rows must be non-negative, got -1 + +Exit code: 1 diff --git a/acceptance/selftest/tui/table/script b/acceptance/selftest/tui/table/script new file mode 100644 index 00000000000..4b008e4c30a --- /dev/null +++ b/acceptance/selftest/tui/table/script @@ -0,0 +1,3 @@ +title "Flag validation runs before the TTY is needed" +errcode trace $CLI selftest tui table --cols 0 +errcode trace $CLI selftest tui table --rows -1 diff --git a/acceptance/selftest/tui/test.toml b/acceptance/selftest/tui/test.toml new file mode 100644 index 00000000000..59b55cb46bb --- /dev/null +++ b/acceptance/selftest/tui/test.toml @@ -0,0 +1,12 @@ +# Pin the color and terminal env so the profile output does not depend on the host. +Env.TERM = "xterm-256color" +Env.COLORTERM = "" +Env.CLICOLOR = "" +Env.CLICOLOR_FORCE = "" +Env.NO_COLOR = "" +Env.CI = "" +Env.TERM_PROGRAM = "" +Env.WT_SESSION = "" + +# Only the non-interactive paths run here. The keys, table and huh commands need a +# TTY for their live behaviour, which is checked by hand under a PTY. diff --git a/cmd/selftest/tui/colors.go b/cmd/selftest/tui/colors.go index 9afc6fd693f..e2d73d199cf 100644 --- a/cmd/selftest/tui/colors.go +++ b/cmd/selftest/tui/colors.go @@ -1,25 +1,87 @@ package tui import ( + "fmt" + "io" + "strconv" + "strings" + + "github.com/charmbracelet/lipgloss" "github.com/databricks/cli/libs/cmdio" "github.com/spf13/cobra" ) +// swatchCount is the number of blocks per depth row in --lipgloss output. +const swatchCount = 32 + func newColorsCmd() *cobra.Command { - return &cobra.Command{ + var ( + stream string + swatches bool + ) + cmd := &cobra.Command{ Use: "colors", Short: "Print colored text to verify color support", + Long: `Print colored text to verify color support. + +The named colors are rendered with cmdio helpers, which check stdout for color +support regardless of --stream. --lipgloss adds truecolor and 256-color swatches +rendered through cmdio.NewRenderer for the chosen stream, so depth differences +are visible.`, + Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + switch stream { + case "", "stdout", "stderr": + return nil + default: + return fmt.Errorf("--stream must be stdout or stderr, got %q", stream) + } + }, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() + // Default keeps the historical cmdio.LogString path, which writes to stderr. + var line func(string) + var w io.Writer + switch stream { + case "stdout": + w = cmd.OutOrStdout() + default: + w = cmd.ErrOrStderr() + } + if stream == "" { + line = func(s string) { cmdio.LogString(ctx, s) } + } else { + line = func(s string) { fmt.Fprintln(w, s) } + } + swatch := "the quick brown fox jumps over the lazy dog" - cmdio.LogString(ctx, "red: "+cmdio.Red(ctx, swatch)) - cmdio.LogString(ctx, "green: "+cmdio.Green(ctx, swatch)) - cmdio.LogString(ctx, "yellow: "+cmdio.Yellow(ctx, swatch)) - cmdio.LogString(ctx, "blue: "+cmdio.Blue(ctx, swatch)) - cmdio.LogString(ctx, "cyan: "+cmdio.Cyan(ctx, swatch)) - cmdio.LogString(ctx, "hiblack: "+cmdio.HiBlack(ctx, swatch)) - cmdio.LogString(ctx, "hiblue: "+cmdio.HiBlue(ctx, swatch)) + line("red: " + cmdio.Red(ctx, swatch)) + line("green: " + cmdio.Green(ctx, swatch)) + line("yellow: " + cmdio.Yellow(ctx, swatch)) + line("blue: " + cmdio.Blue(ctx, swatch)) + line("cyan: " + cmdio.Cyan(ctx, swatch)) + line("hiblack: " + cmdio.HiBlack(ctx, swatch)) + line("hiblue: " + cmdio.HiBlue(ctx, swatch)) + + if !swatches { + return nil + } + r, _ := cmdio.NewRenderer(ctx, w) + truecolor := make([]string, 0, swatchCount) + ansi256 := make([]string, 0, swatchCount) + for i := range swatchCount { + v := i * 255 / (swatchCount - 1) + hex := fmt.Sprintf("#%02x%02x%02x", v, 0, 255-v) + truecolor = append(truecolor, r.NewStyle().Background(lipgloss.Color(hex)).Render(" ")) + index := 16 + i*7 + ansi256 = append(ansi256, r.NewStyle().Background(lipgloss.Color(strconv.Itoa(index))).Render(" ")) + } + line("truecolor: " + strings.Join(truecolor, "")) + line("256-color: " + strings.Join(ansi256, "")) return nil }, } + cmd.Flags().StringVar(&stream, "stream", "", "write output to stdout or stderr (default: cmdio.LogString, stderr)") + cmd.Flags().BoolVar(&swatches, "lipgloss", false, "also print truecolor and 256-color swatches") + return cmd } diff --git a/cmd/selftest/tui/huh.go b/cmd/selftest/tui/huh.go new file mode 100644 index 00000000000..6cbfb5afafb --- /dev/null +++ b/cmd/selftest/tui/huh.go @@ -0,0 +1,79 @@ +package tui + +import ( + "fmt" + "strconv" + + "github.com/charmbracelet/huh" + "github.com/databricks/cli/libs/apps/prompt" + "github.com/databricks/cli/libs/cmdio" + "github.com/spf13/cobra" +) + +func newHuhCmd() *cobra.Command { + var field string + cmd := &cobra.Command{ + Use: "huh", + Short: "Run one huh form field with the appkit theme", + Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + switch field { + case "input", "select", "confirm", "multiselect": + return nil + default: + return fmt.Errorf("--field must be one of input, select, confirm, multiselect, got %q", field) + } + }, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + theme := prompt.AppkitTheme() + features := databricksFeatures[:5] + options := make([]huh.Option[string], 0, len(features)) + for _, name := range features { + options = append(options, huh.NewOption(name, name)) + } + + var ( + text string + choice string + yes bool + picked []string + group huh.Field + ) + switch field { + case "input": + group = huh.NewInput().Title("Enter a value").Value(&text) + case "select": + group = huh.NewSelect[string]().Title("Pick an item").Options(options...).Value(&choice) + case "confirm": + group = huh.NewConfirm().Title("Continue?").Value(&yes) + case "multiselect": + group = huh.NewMultiSelect[string]().Title("Pick items").Options(options...).Value(&picked) + } + + if err := huh.NewForm(huh.NewGroup(group)).WithTheme(theme).Run(); err != nil { + return err + } + + var answer string + switch field { + case "input": + answer = text + case "select": + answer = choice + case "confirm": + answer = strconv.FormatBool(yes) + case "multiselect": + answer = fmt.Sprint(picked) + } + cmdio.LogString(ctx, "answer: "+answer) + + r, _ := cmdio.NewRenderer(ctx, cmd.ErrOrStderr()) + cmdio.LogString(ctx, fmt.Sprintf("dark_background=%t", r.HasDarkBackground())) + cmdio.LogString(ctx, fmt.Sprintf("blurred_button=%+v", theme.Focused.BlurredButton.GetForeground())) + return nil + }, + } + cmd.Flags().StringVar(&field, "field", "input", "field type: input, select, confirm or multiselect") + return cmd +} diff --git a/cmd/selftest/tui/keys.go b/cmd/selftest/tui/keys.go new file mode 100644 index 00000000000..c210754d069 --- /dev/null +++ b/cmd/selftest/tui/keys.go @@ -0,0 +1,150 @@ +package tui + +import ( + "context" + "errors" + "fmt" + "io" + "os" + "time" + + tea "github.com/charmbracelet/bubbletea" + "github.com/databricks/cli/libs/cmdio" + "github.com/spf13/cobra" + "golang.org/x/term" +) + +// keysExitCtrlC is the number of Ctrl+C presses that end the keys program. +// A single press is recorded like any other key so it can be inspected. +const keysExitCtrlC = 2 + +// keysModel prints one line per key message. It draws nothing itself so the +// output stays a plain log of decoded input. Lines are written directly from +// Update: tea.Println runs its commands concurrently, so the order is not kept. +type keysModel struct { + out io.Writer + ctrlC int +} + +func (m *keysModel) Init() tea.Cmd { return nil } + +func (m *keysModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + key, ok := msg.(tea.KeyMsg) + if !ok { + return m, nil + } + line := fmt.Sprintf("key=%q type=%d runes=%q alt=%t paste=%t", + key.String(), key.Type, string(key.Runes), key.Alt, key.Paste) + fmt.Fprintln(m.out, line) + if key.Type == tea.KeyCtrlC { + m.ctrlC++ + if m.ctrlC >= keysExitCtrlC { + return m, tea.Quit + } + } + return m, nil +} + +func (m *keysModel) View() string { return "" } + +func newKeysCmd() *cobra.Command { + var ( + raw bool + timeout time.Duration + ) + cmd := &cobra.Command{ + Use: "keys", + Short: "Print each key message bubbletea decodes from stdin", + Long: `Print one line per key message that bubbletea decodes from stdin. + +Uses the same tea.Program options as cmdio's prompts, so the output shows how +this terminal encodes Esc, Alt+x, Shift+Enter, Ctrl+H vs Backspace, AltGr and +bracketed paste. Exits on two Ctrl+C presses or after --timeout. + +With --raw, bubbletea is bypassed: stdin is put in raw mode and each chunk of +bytes read is printed as hex and quoted text.`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + in := cmd.InOrStdin() + out := cmd.ErrOrStderr() + if raw { + return runRawKeys(cmd.Context(), in, out, timeout) + } + p := tea.NewProgram(&keysModel{out: out}, + tea.WithInput(in), + tea.WithOutput(out), + tea.WithoutSignalHandler(), + ) + if timeout > 0 { + timer := time.AfterFunc(timeout, p.Quit) + defer timer.Stop() + } + _, err := p.Run() + return err + }, + } + cmd.Flags().BoolVar(&raw, "raw", false, "bypass bubbletea and print the raw bytes read from stdin") + cmd.Flags().DurationVar(&timeout, "timeout", 0, "exit after this duration (0 waits for Ctrl+C)") + return cmd +} + +// runRawKeys reads stdin in raw mode without bubbletea. The reader goroutine +// may stay blocked on Read after return; the process exits right after the +// command finishes, so that is harmless. +func runRawKeys(ctx context.Context, in io.Reader, out io.Writer, timeout time.Duration) error { + if f, ok := in.(*os.File); ok && cmdio.IsInputTTY(ctx) { + state, err := term.MakeRaw(int(f.Fd())) + if err != nil { + return err + } + defer func() { _ = term.Restore(int(f.Fd()), state) }() + } + + chunks := make(chan []byte) + readErr := make(chan error, 1) + go func() { + buf := make([]byte, 256) + for { + n, err := in.Read(buf) + if n > 0 { + chunk := make([]byte, n) + copy(chunk, buf[:n]) + chunks <- chunk + } + if err != nil { + readErr <- err + return + } + } + }() + + var timeoutC <-chan time.Time + if timeout > 0 { + timer := time.NewTimer(timeout) + defer timer.Stop() + timeoutC = timer.C + } + + ctrlC := 0 + for { + select { + case chunk := <-chunks: + fmt.Fprintf(out, "raw bytes=% x text=%q\n", chunk, string(chunk)) + for _, b := range chunk { + if b == 0x03 { + ctrlC++ + } + } + if ctrlC >= keysExitCtrlC { + return nil + } + case err := <-readErr: + if errors.Is(err, io.EOF) { + return nil + } + return err + case <-timeoutC: + return nil + } + } +} diff --git a/cmd/selftest/tui/keys_test.go b/cmd/selftest/tui/keys_test.go new file mode 100644 index 00000000000..9dbb2ffdc70 --- /dev/null +++ b/cmd/selftest/tui/keys_test.go @@ -0,0 +1,26 @@ +package tui + +import ( + "bytes" + "testing" + + "github.com/databricks/cli/libs/cmdio" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRunRawKeysPrintsChunksAndExitsOnDoubleCtrlC(t *testing.T) { + var out bytes.Buffer + ctx := cmdio.MockDiscard(t.Context()) + err := runRawKeys(ctx, bytes.NewReader([]byte("a\x03\x03b")), &out, 0) + require.NoError(t, err) + assert.Equal(t, "raw bytes=61 03 03 62 text=\"a\\x03\\x03b\"\n", out.String()) +} + +func TestRunRawKeysReturnsOnEOF(t *testing.T) { + var out bytes.Buffer + ctx := cmdio.MockDiscard(t.Context()) + err := runRawKeys(ctx, bytes.NewReader([]byte("hi")), &out, 0) + require.NoError(t, err) + assert.Equal(t, "raw bytes=68 69 text=\"hi\"\n", out.String()) +} diff --git a/cmd/selftest/tui/pager.go b/cmd/selftest/tui/pager.go new file mode 100644 index 00000000000..6dd238dc00a --- /dev/null +++ b/cmd/selftest/tui/pager.go @@ -0,0 +1,127 @@ +package tui + +import ( + "context" + "fmt" + "time" + + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/databricks-sdk-go/listing" + "github.com/spf13/cobra" +) + +// pagerDefaultHeader matches the header that real list commands render. +var pagerDefaultHeader = cmdio.Heredoc(` + {{header "Name"}} {{header "State"}}`) + +// pagerRowTemplate matches the row template used by real list commands. +var pagerRowTemplate = cmdio.Heredoc(` + {{range .}}{{.Name|green}} {{.State}} + {{end}}`) + +type pagerRow struct { + Name string + State string +} + +// pagerFakeIterator yields synthetic rows. It implements listing.Iterator so +// the production RenderIterator decides between paged and non-paged output. +type pagerFakeIterator struct { + rows []pagerRow + next int + failAfter int // -1 disables the injected error + delayFirst time.Duration + delay time.Duration +} + +var _ listing.Iterator[pagerRow] = (*pagerFakeIterator)(nil) + +func (it *pagerFakeIterator) HasNext(context.Context) bool { + if it.failAfter >= 0 && it.next == it.failAfter { + // Report more items so the caller reaches Next and sees the error. + return true + } + return it.next < len(it.rows) +} + +func (it *pagerFakeIterator) Next(ctx context.Context) (pagerRow, error) { + delay := it.delay + if it.next == 0 { + delay = it.delayFirst + } + if delay > 0 { + select { + case <-ctx.Done(): + return pagerRow{}, ctx.Err() + case <-time.After(delay): + } + } + if it.failAfter >= 0 && it.next == it.failAfter { + return pagerRow{}, fmt.Errorf("injected iterator error after %d rows", it.failAfter) + } + row := it.rows[it.next] + it.next++ + return row, nil +} + +var pagerStates = []string{"RUNNING", "TERMINATED", "PENDING"} + +func newPagerCmd() *cobra.Command { + var ( + items int + header string + delayFirst time.Duration + delay time.Duration + failAfter int + ) + cmd := &cobra.Command{ + Use: "pager", + Short: "cmdio.RenderIterator over synthetic rows (paged only when fully interactive)", + Args: func(cmd *cobra.Command, args []string) error { + // cobra runs Args before PersistentPreRunE, which is where cmdio reads + // these annotations, so the --header flag is applied here. + cmd.Annotations["headerTemplate"] = cmdio.Heredoc(fmt.Sprintf(` + {{header %q}} {{header "State"}}`, header)) + return cobra.NoArgs(cmd, args) + }, + PreRunE: func(cmd *cobra.Command, args []string) error { + if items < 0 { + return fmt.Errorf("--items must be non-negative, got %d", items) + } + if failAfter < -1 { + return fmt.Errorf("--fail-after must be -1 or greater, got %d", failAfter) + } + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + it := &pagerFakeIterator{ + rows: buildPagerRows(items), + failAfter: failAfter, + delayFirst: delayFirst, + delay: delay, + } + return cmdio.RenderIterator(cmd.Context(), it) + }, + Annotations: map[string]string{ + "headerTemplate": pagerDefaultHeader, + "template": pagerRowTemplate, + }, + } + cmd.Flags().IntVar(&items, "items", 50, "number of rows to yield") + cmd.Flags().StringVar(&header, "header", "Name", "text for the Name column header") + cmd.Flags().DurationVar(&delayFirst, "delay-first", 0, "delay before the first row") + cmd.Flags().DurationVar(&delay, "delay", 0, "delay before each following row") + cmd.Flags().IntVar(&failAfter, "fail-after", -1, "inject an iterator error after this many rows (0 fails on the first Next, -1 disables)") + return cmd +} + +func buildPagerRows(n int) []pagerRow { + rows := make([]pagerRow, 0, n) + for i := range n { + rows = append(rows, pagerRow{ + Name: fmt.Sprintf("item-%04d", i+1), + State: pagerStates[i%len(pagerStates)], + }) + } + return rows +} diff --git a/cmd/selftest/tui/pager_test.go b/cmd/selftest/tui/pager_test.go new file mode 100644 index 00000000000..a09e79934aa --- /dev/null +++ b/cmd/selftest/tui/pager_test.go @@ -0,0 +1,48 @@ +package tui + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPagerFakeIterator(t *testing.T) { + tests := []struct { + name string + rows int + failAfter int + // wantRows is the number of rows Next returns before it stops. + wantRows int + wantErr string + }{ + {name: "no failure yields every row", rows: 3, failAfter: -1, wantRows: 3}, + {name: "failure after two rows", rows: 5, failAfter: 2, wantRows: 2, wantErr: "injected iterator error after 2 rows"}, + {name: "failure on the first Next", rows: 5, failAfter: 0, wantRows: 0, wantErr: "injected iterator error after 0 rows"}, + {name: "failure past the end is never reached", rows: 2, failAfter: 5, wantRows: 2}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + it := &pagerFakeIterator{rows: buildPagerRows(tc.rows), failAfter: tc.failAfter} + + got := 0 + var err error + for it.HasNext(ctx) { + var row pagerRow + row, err = it.Next(ctx) + if err != nil { + break + } + require.Equal(t, buildPagerRows(tc.rows)[got], row) + got++ + } + assert.Equal(t, tc.wantRows, got) + if tc.wantErr == "" { + assert.NoError(t, err) + } else { + assert.EqualError(t, err, tc.wantErr) + } + }) + } +} diff --git a/cmd/selftest/tui/profile.go b/cmd/selftest/tui/profile.go new file mode 100644 index 00000000000..5b804453294 --- /dev/null +++ b/cmd/selftest/tui/profile.go @@ -0,0 +1,49 @@ +package tui + +import ( + "fmt" + "io" + + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/env" + "github.com/spf13/cobra" +) + +// profileEnvVars are the variables cmdio and lipgloss consult when deciding +// on color and background, so a mismatch in a live terminal can be traced. +var profileEnvVars = []string{ + "TERM", "COLORTERM", "CLICOLOR", "CLICOLOR_FORCE", "NO_COLOR", + "CI", "TERM_PROGRAM", "WT_SESSION", +} + +func newProfileCmd() *cobra.Command { + return &cobra.Command{ + Use: "profile", + Short: "Print the color decision cmdio makes for stdout and stderr", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + w := cmd.OutOrStdout() + for _, s := range []struct { + name string + out io.Writer + }{ + {"stdout", cmd.OutOrStdout()}, + {"stderr", cmd.ErrOrStderr()}, + } { + r, color := cmdio.NewRenderer(ctx, s.out) + fmt.Fprintf(w, "%s: supports_color=%t profile=%s dark_background=%t\n", + s.name, color, r.ColorProfile().Name(), r.HasDarkBackground()) + } + fmt.Fprintln(w, "env:") + for _, name := range profileEnvVars { + value := env.Get(ctx, name) + if value == "" { + value = "(unset or empty)" + } + fmt.Fprintf(w, " %s=%s\n", name, value) + } + return nil + }, + } +} diff --git a/cmd/selftest/tui/spinner.go b/cmd/selftest/tui/spinner.go index 799ce0585d1..788c445fc57 100644 --- a/cmd/selftest/tui/spinner.go +++ b/cmd/selftest/tui/spinner.go @@ -8,7 +8,10 @@ import ( ) func newSpinnerCmd() *cobra.Command { - var elapsed bool + var ( + elapsed bool + duration time.Duration + ) cmd := &cobra.Command{ Use: "spinner", Short: "cmdio.NewSpinner (progress indicator)", @@ -23,7 +26,11 @@ func newSpinnerCmd() *cobra.Command { for _, msg := range spinnerMessages { sp.Update(msg.text) - time.Sleep(msg.duration) + d := msg.duration + if duration > 0 { + d = duration / time.Duration(len(spinnerMessages)) + } + time.Sleep(d) } sp.Close() @@ -33,5 +40,6 @@ func newSpinnerCmd() *cobra.Command { }, } cmd.Flags().BoolVar(&elapsed, "elapsed", false, "show an MM:SS elapsed-time prefix on the spinner") + cmd.Flags().DurationVar(&duration, "duration", 0, "total time the spinner runs, split across its messages (0 keeps the built-in per-message timing)") return cmd } diff --git a/cmd/selftest/tui/table.go b/cmd/selftest/tui/table.go new file mode 100644 index 00000000000..4de12daa35d --- /dev/null +++ b/cmd/selftest/tui/table.go @@ -0,0 +1,49 @@ +package tui + +import ( + "fmt" + + "github.com/databricks/cli/libs/tableview" + "github.com/spf13/cobra" +) + +func newTableCmd() *cobra.Command { + var ( + rows int + cols int + ) + cmd := &cobra.Command{ + Use: "table", + Short: "tableview.Run over generated rows (interactive browser)", + Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + if rows < 0 { + return fmt.Errorf("--rows must be non-negative, got %d", rows) + } + if cols < 1 { + return fmt.Errorf("--cols must be at least 1, got %d", cols) + } + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + columns := make([]string, cols) + for c := range cols { + columns[c] = fmt.Sprintf("col-%02d", c+1) + } + data := make([][]string, rows) + for r := range rows { + data[r] = make([]string, cols) + for c := range cols { + data[r][c] = fmt.Sprintf("r%04d-c%02d", r+1, c+1) + } + } + // tableview.Run does not report how the user exited, only the error. + err := tableview.Run(cmd.Context(), cmd.OutOrStdout(), columns, data) + fmt.Fprintf(cmd.ErrOrStderr(), "tableview.Run returned: %v\n", err) + return err + }, + } + cmd.Flags().IntVar(&rows, "rows", 100, "number of generated rows") + cmd.Flags().IntVar(&cols, "cols", 4, "number of generated columns") + return cmd +} diff --git a/cmd/selftest/tui/tui.go b/cmd/selftest/tui/tui.go index f1b807fc930..8476b69ffb5 100644 --- a/cmd/selftest/tui/tui.go +++ b/cmd/selftest/tui/tui.go @@ -12,12 +12,17 @@ func New() *cobra.Command { newAskCmd(), newAskYesOrNoCmd(), newColorsCmd(), + newHuhCmd(), + newKeysCmd(), + newPagerCmd(), + newProfileCmd(), newPromptCmd(), newRunSelectCmd(), newSecretCmd(), newSelectCmd(), newSelectOrderedCmd(), newSpinnerCmd(), + newTableCmd(), ) return cmd From d4de5f246a01cdef2b515b12355c9d05594f3121 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 16 Nov 2026 15:22:07 +0000 Subject: [PATCH 2/7] selftest: address review of TUI commands - keys --raw prints CRLF while raw mode is on, so lines start at column 0 - keys and spinner reject negative --timeout and --duration in PreRunE - colors: replace the inline closure with a named helper - huh: document that the field is built here and only the theme is shared - Expected failures in acceptance tests use musterr instead of errcode - Add a --header case to the pager acceptance test - Gate the tui acceptance tests off Windows Co-authored-by: Isaac --- acceptance/selftest/tui/colors/out.test.toml | 1 + acceptance/selftest/tui/colors/output.txt | 2 - acceptance/selftest/tui/colors/script | 2 +- acceptance/selftest/tui/huh/out.test.toml | 1 + acceptance/selftest/tui/huh/output.txt | 2 - acceptance/selftest/tui/huh/script | 2 +- acceptance/selftest/tui/keys/out.test.toml | 1 + acceptance/selftest/tui/keys/script | 2 + acceptance/selftest/tui/pager/out.test.toml | 1 + acceptance/selftest/tui/pager/output.txt | 13 +++--- acceptance/selftest/tui/pager/script | 9 ++-- acceptance/selftest/tui/profile/out.test.toml | 1 + acceptance/selftest/tui/profile/output.txt | 2 + acceptance/selftest/tui/spinner/out.test.toml | 1 + acceptance/selftest/tui/spinner/output.txt | 2 +- acceptance/selftest/tui/spinner/script | 2 +- acceptance/selftest/tui/table/out.test.toml | 1 + acceptance/selftest/tui/table/output.txt | 4 -- acceptance/selftest/tui/table/script | 4 +- acceptance/selftest/tui/test.toml | 8 +++- cmd/selftest/tui/colors.go | 43 ++++++++++--------- cmd/selftest/tui/huh.go | 7 ++- cmd/selftest/tui/keys.go | 24 ++++++++--- cmd/selftest/tui/profile.go | 7 +-- cmd/selftest/tui/spinner.go | 7 +++ 25 files changed, 93 insertions(+), 56 deletions(-) diff --git a/acceptance/selftest/tui/colors/out.test.toml b/acceptance/selftest/tui/colors/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/colors/out.test.toml +++ b/acceptance/selftest/tui/colors/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/colors/output.txt b/acceptance/selftest/tui/colors/output.txt index 06e687db692..dfda143af8c 100644 --- a/acceptance/selftest/tui/colors/output.txt +++ b/acceptance/selftest/tui/colors/output.txt @@ -58,5 +58,3 @@ truecolor: >>> [CLI] selftest tui colors --stream file Error: --stream must be stdout or stderr, got "file" - -Exit code: 1 diff --git a/acceptance/selftest/tui/colors/script b/acceptance/selftest/tui/colors/script index c8f38dea3de..672e8833776 100644 --- a/acceptance/selftest/tui/colors/script +++ b/acceptance/selftest/tui/colors/script @@ -15,4 +15,4 @@ title "--lipgloss prints both swatch rows" trace $CLI selftest tui colors --lipgloss 2>&1 | contains.py 'truecolor:' '256-color:' title "Unknown stream is rejected" -errcode trace $CLI selftest tui colors --stream file +musterr trace $CLI selftest tui colors --stream file diff --git a/acceptance/selftest/tui/huh/out.test.toml b/acceptance/selftest/tui/huh/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/huh/out.test.toml +++ b/acceptance/selftest/tui/huh/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/huh/output.txt b/acceptance/selftest/tui/huh/output.txt index 61c4b271c9c..514b6405dc4 100644 --- a/acceptance/selftest/tui/huh/output.txt +++ b/acceptance/selftest/tui/huh/output.txt @@ -3,5 +3,3 @@ >>> [CLI] selftest tui huh --field bogus Error: --field must be one of input, select, confirm, multiselect, got "bogus" - -Exit code: 1 diff --git a/acceptance/selftest/tui/huh/script b/acceptance/selftest/tui/huh/script index 2b4373105b3..9515e131c05 100644 --- a/acceptance/selftest/tui/huh/script +++ b/acceptance/selftest/tui/huh/script @@ -1,2 +1,2 @@ title "Unknown field type is rejected" -errcode trace $CLI selftest tui huh --field bogus +musterr trace $CLI selftest tui huh --field bogus diff --git a/acceptance/selftest/tui/keys/out.test.toml b/acceptance/selftest/tui/keys/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/keys/out.test.toml +++ b/acceptance/selftest/tui/keys/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/keys/script b/acceptance/selftest/tui/keys/script index 7ea18ecce5c..e97ed88cfe3 100644 --- a/acceptance/selftest/tui/keys/script +++ b/acceptance/selftest/tui/keys/script @@ -5,4 +5,6 @@ title "--raw prints the bytes read from stdin" printf 'ab\x03\x03' | trace $CLI selftest tui keys --raw 2>&1 title "--timeout exits without any input" +# A pipe keeps stdin non-TTY and open: /dev/null makes bubbletea fail to create +# its cancel reader. The sleep is longer than the timeout so only --timeout can end the run. sleep 2 | trace $CLI selftest tui keys --timeout 1s 2>&1 | contains.py '!key=' diff --git a/acceptance/selftest/tui/pager/out.test.toml b/acceptance/selftest/tui/pager/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/pager/out.test.toml +++ b/acceptance/selftest/tui/pager/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/pager/output.txt b/acceptance/selftest/tui/pager/output.txt index ed71399b270..8ff7db5f5e5 100644 --- a/acceptance/selftest/tui/pager/output.txt +++ b/acceptance/selftest/tui/pager/output.txt @@ -7,23 +7,24 @@ item-0001 RUNNING item-0002 TERMINATED item-0003 PENDING +=== --header replaces the Name column header + +>>> [CLI] selftest tui pager --items 2 --header Workspace +Workspace State +item-0001 RUNNING +item-0002 TERMINATED + === An injected error after 2 rows is returned >>> [CLI] selftest tui pager --items 5 --fail-after 2 Error: injected iterator error after 2 rows -Exit code: 1 - === --fail-after 0 fails on the first Next >>> [CLI] selftest tui pager --items 5 --fail-after 0 Error: injected iterator error after 0 rows -Exit code: 1 - === Negative --items is rejected >>> [CLI] selftest tui pager --items -1 Error: --items must be non-negative, got -1 - -Exit code: 1 diff --git a/acceptance/selftest/tui/pager/script b/acceptance/selftest/tui/pager/script index 28b33b1722b..b515112dab1 100644 --- a/acceptance/selftest/tui/pager/script +++ b/acceptance/selftest/tui/pager/script @@ -1,11 +1,14 @@ title "Non-interactive output uses the non-paged renderer" trace $CLI selftest tui pager --items 3 +title "--header replaces the Name column header" +trace $CLI selftest tui pager --items 2 --header Workspace + title "An injected error after 2 rows is returned" -errcode trace $CLI selftest tui pager --items 5 --fail-after 2 +musterr trace $CLI selftest tui pager --items 5 --fail-after 2 title "--fail-after 0 fails on the first Next" -errcode trace $CLI selftest tui pager --items 5 --fail-after 0 +musterr trace $CLI selftest tui pager --items 5 --fail-after 0 title "Negative --items is rejected" -errcode trace $CLI selftest tui pager --items -1 +musterr trace $CLI selftest tui pager --items -1 diff --git a/acceptance/selftest/tui/profile/out.test.toml b/acceptance/selftest/tui/profile/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/profile/out.test.toml +++ b/acceptance/selftest/tui/profile/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/profile/output.txt b/acceptance/selftest/tui/profile/output.txt index df1a714def2..9ac67daa218 100644 --- a/acceptance/selftest/tui/profile/output.txt +++ b/acceptance/selftest/tui/profile/output.txt @@ -10,6 +10,7 @@ env: CLICOLOR=(unset or empty) CLICOLOR_FORCE=(unset or empty) NO_COLOR=(unset or empty) + COLORFGBG=(unset or empty) CI=(unset or empty) TERM_PROGRAM=(unset or empty) WT_SESSION=(unset or empty) @@ -25,6 +26,7 @@ env: CLICOLOR=(unset or empty) CLICOLOR_FORCE=(unset or empty) NO_COLOR=1 + COLORFGBG=(unset or empty) CI=(unset or empty) TERM_PROGRAM=(unset or empty) WT_SESSION=(unset or empty) diff --git a/acceptance/selftest/tui/spinner/out.test.toml b/acceptance/selftest/tui/spinner/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/spinner/out.test.toml +++ b/acceptance/selftest/tui/spinner/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/spinner/output.txt b/acceptance/selftest/tui/spinner/output.txt index 62f27b8e95d..a328e621e17 100644 --- a/acceptance/selftest/tui/spinner/output.txt +++ b/acceptance/selftest/tui/spinner/output.txt @@ -1,5 +1,5 @@ -=== --duration splits the total across the spinner messages +=== --duration is accepted and the spinner completes >>> [CLI] selftest tui spinner --duration 500ms Spinner test complete diff --git a/acceptance/selftest/tui/spinner/script b/acceptance/selftest/tui/spinner/script index c1a2f3b8f48..bc316cf0ac7 100644 --- a/acceptance/selftest/tui/spinner/script +++ b/acceptance/selftest/tui/spinner/script @@ -1,2 +1,2 @@ -title "--duration splits the total across the spinner messages" +title "--duration is accepted and the spinner completes" trace $CLI selftest tui spinner --duration 500ms diff --git a/acceptance/selftest/tui/table/out.test.toml b/acceptance/selftest/tui/table/out.test.toml index 18b1a88417e..2e1543aa802 100644 --- a/acceptance/selftest/tui/table/out.test.toml +++ b/acceptance/selftest/tui/table/out.test.toml @@ -1 +1,2 @@ Cloud = false +GOOS.windows = false diff --git a/acceptance/selftest/tui/table/output.txt b/acceptance/selftest/tui/table/output.txt index 02719820acf..727afc5320b 100644 --- a/acceptance/selftest/tui/table/output.txt +++ b/acceptance/selftest/tui/table/output.txt @@ -4,9 +4,5 @@ >>> [CLI] selftest tui table --cols 0 Error: --cols must be at least 1, got 0 -Exit code: 1 - >>> [CLI] selftest tui table --rows -1 Error: --rows must be non-negative, got -1 - -Exit code: 1 diff --git a/acceptance/selftest/tui/table/script b/acceptance/selftest/tui/table/script index 4b008e4c30a..7e339f26ca3 100644 --- a/acceptance/selftest/tui/table/script +++ b/acceptance/selftest/tui/table/script @@ -1,3 +1,3 @@ title "Flag validation runs before the TTY is needed" -errcode trace $CLI selftest tui table --cols 0 -errcode trace $CLI selftest tui table --rows -1 +musterr trace $CLI selftest tui table --cols 0 +musterr trace $CLI selftest tui table --rows -1 diff --git a/acceptance/selftest/tui/test.toml b/acceptance/selftest/tui/test.toml index 59b55cb46bb..53b4c801db0 100644 --- a/acceptance/selftest/tui/test.toml +++ b/acceptance/selftest/tui/test.toml @@ -8,5 +8,9 @@ Env.CI = "" Env.TERM_PROGRAM = "" Env.WT_SESSION = "" -# Only the non-interactive paths run here. The keys, table and huh commands need a -# TTY for their live behaviour, which is checked by hand under a PTY. +# Only the non-interactive paths run here. Live rendering and raw mode need a +# real TTY, which is checked by hand under a PTY. + +# The scripts use bash, printf and sleep, and the goldens embed bubbletea escape +# sequences. Not verified on Windows, so these tests run on Linux and macOS only. +GOOS.windows = false diff --git a/cmd/selftest/tui/colors.go b/cmd/selftest/tui/colors.go index e2d73d199cf..f3c4025e8c9 100644 --- a/cmd/selftest/tui/colors.go +++ b/cmd/selftest/tui/colors.go @@ -1,6 +1,7 @@ package tui import ( + "context" "fmt" "io" "strconv" @@ -39,29 +40,19 @@ are visible.`, }, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - // Default keeps the historical cmdio.LogString path, which writes to stderr. - var line func(string) - var w io.Writer - switch stream { - case "stdout": + w := cmd.ErrOrStderr() + if stream == "stdout" { w = cmd.OutOrStdout() - default: - w = cmd.ErrOrStderr() - } - if stream == "" { - line = func(s string) { cmdio.LogString(ctx, s) } - } else { - line = func(s string) { fmt.Fprintln(w, s) } } swatch := "the quick brown fox jumps over the lazy dog" - line("red: " + cmdio.Red(ctx, swatch)) - line("green: " + cmdio.Green(ctx, swatch)) - line("yellow: " + cmdio.Yellow(ctx, swatch)) - line("blue: " + cmdio.Blue(ctx, swatch)) - line("cyan: " + cmdio.Cyan(ctx, swatch)) - line("hiblack: " + cmdio.HiBlack(ctx, swatch)) - line("hiblue: " + cmdio.HiBlue(ctx, swatch)) + writeLine(ctx, stream, w, "red: "+cmdio.Red(ctx, swatch)) + writeLine(ctx, stream, w, "green: "+cmdio.Green(ctx, swatch)) + writeLine(ctx, stream, w, "yellow: "+cmdio.Yellow(ctx, swatch)) + writeLine(ctx, stream, w, "blue: "+cmdio.Blue(ctx, swatch)) + writeLine(ctx, stream, w, "cyan: "+cmdio.Cyan(ctx, swatch)) + writeLine(ctx, stream, w, "hiblack: "+cmdio.HiBlack(ctx, swatch)) + writeLine(ctx, stream, w, "hiblue: "+cmdio.HiBlue(ctx, swatch)) if !swatches { return nil @@ -76,8 +67,8 @@ are visible.`, index := 16 + i*7 ansi256 = append(ansi256, r.NewStyle().Background(lipgloss.Color(strconv.Itoa(index))).Render(" ")) } - line("truecolor: " + strings.Join(truecolor, "")) - line("256-color: " + strings.Join(ansi256, "")) + writeLine(ctx, stream, w, "truecolor: "+strings.Join(truecolor, "")) + writeLine(ctx, stream, w, "256-color: "+strings.Join(ansi256, "")) return nil }, } @@ -85,3 +76,13 @@ are visible.`, cmd.Flags().BoolVar(&swatches, "lipgloss", false, "also print truecolor and 256-color swatches") return cmd } + +// writeLine keeps the default stream on cmdio.LogString so the default output +// stays exactly as it was before --stream existed. +func writeLine(ctx context.Context, stream string, w io.Writer, s string) { + if stream == "" { + cmdio.LogString(ctx, s) + return + } + fmt.Fprintln(w, s) +} diff --git a/cmd/selftest/tui/huh.go b/cmd/selftest/tui/huh.go index 6cbfb5afafb..d1f96a32ce4 100644 --- a/cmd/selftest/tui/huh.go +++ b/cmd/selftest/tui/huh.go @@ -15,7 +15,12 @@ func newHuhCmd() *cobra.Command { cmd := &cobra.Command{ Use: "huh", Short: "Run one huh form field with the appkit theme", - Args: cobra.NoArgs, + Long: `Run one huh form field with the appkit theme and print the answer. + +The field is built here, not by a libs/apps/prompt helper: those helpers each +set their own title, options and validation. Only prompt.AppkitTheme is shared, +so this checks the theme, not the production prompt flows.`, + Args: cobra.NoArgs, PreRunE: func(cmd *cobra.Command, args []string) error { switch field { case "input", "select", "confirm", "multiselect": diff --git a/cmd/selftest/tui/keys.go b/cmd/selftest/tui/keys.go index c210754d069..d8ffef5c484 100644 --- a/cmd/selftest/tui/keys.go +++ b/cmd/selftest/tui/keys.go @@ -61,8 +61,8 @@ Uses the same tea.Program options as cmdio's prompts, so the output shows how this terminal encodes Esc, Alt+x, Shift+Enter, Ctrl+H vs Backspace, AltGr and bracketed paste. Exits on two Ctrl+C presses or after --timeout. -With --raw, bubbletea is bypassed: stdin is put in raw mode and each chunk of -bytes read is printed as hex and quoted text.`, +With --raw, bubbletea is bypassed: when stdin is a terminal it is put in raw +mode, and each chunk of bytes read is printed as hex and quoted text.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { in := cmd.InOrStdin() @@ -70,6 +70,8 @@ bytes read is printed as hex and quoted text.`, if raw { return runRawKeys(cmd.Context(), in, out, timeout) } + // Mirrors cmdio's runTUI options. cmdio does not expose a way to run a + // custom model, so keep these in sync with libs/cmdio/io.go. p := tea.NewProgram(&keysModel{out: out}, tea.WithInput(in), tea.WithOutput(out), @@ -85,19 +87,29 @@ bytes read is printed as hex and quoted text.`, } cmd.Flags().BoolVar(&raw, "raw", false, "bypass bubbletea and print the raw bytes read from stdin") cmd.Flags().DurationVar(&timeout, "timeout", 0, "exit after this duration (0 waits for Ctrl+C)") + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { + if timeout < 0 { + return fmt.Errorf("--timeout must not be negative, got %s", timeout) + } + return nil + } return cmd } -// runRawKeys reads stdin in raw mode without bubbletea. The reader goroutine -// may stay blocked on Read after return; the process exits right after the -// command finishes, so that is harmless. +// runRawKeys reads stdin in raw mode without bubbletea. After an early return +// the reader goroutine can be blocked on Read or on sending a chunk; the +// process exits once the command finishes, so it does not need to be joined. func runRawKeys(ctx context.Context, in io.Reader, out io.Writer, timeout time.Duration) error { + // Raw mode clears OPOST, so "\n" would not return the carriage. Print + // "\r\n" in that case so each line starts at column 0. + eol := "\n" if f, ok := in.(*os.File); ok && cmdio.IsInputTTY(ctx) { state, err := term.MakeRaw(int(f.Fd())) if err != nil { return err } defer func() { _ = term.Restore(int(f.Fd()), state) }() + eol = "\r\n" } chunks := make(chan []byte) @@ -129,7 +141,7 @@ func runRawKeys(ctx context.Context, in io.Reader, out io.Writer, timeout time.D for { select { case chunk := <-chunks: - fmt.Fprintf(out, "raw bytes=% x text=%q\n", chunk, string(chunk)) + fmt.Fprintf(out, "raw bytes=% x text=%q%s", chunk, string(chunk), eol) for _, b := range chunk { if b == 0x03 { ctrlC++ diff --git a/cmd/selftest/tui/profile.go b/cmd/selftest/tui/profile.go index 5b804453294..a1524c0debe 100644 --- a/cmd/selftest/tui/profile.go +++ b/cmd/selftest/tui/profile.go @@ -9,11 +9,12 @@ import ( "github.com/spf13/cobra" ) -// profileEnvVars are the variables cmdio and lipgloss consult when deciding -// on color and background, so a mismatch in a live terminal can be traced. +// profileEnvVars are the variables cmdio, lipgloss and termenv consult when +// deciding on color and background, so a mismatch in a live terminal can be +// traced. COLORFGBG is the termenv background fallback on terminals. var profileEnvVars = []string{ "TERM", "COLORTERM", "CLICOLOR", "CLICOLOR_FORCE", "NO_COLOR", - "CI", "TERM_PROGRAM", "WT_SESSION", + "COLORFGBG", "CI", "TERM_PROGRAM", "WT_SESSION", } func newProfileCmd() *cobra.Command { diff --git a/cmd/selftest/tui/spinner.go b/cmd/selftest/tui/spinner.go index 788c445fc57..e29cf269fd0 100644 --- a/cmd/selftest/tui/spinner.go +++ b/cmd/selftest/tui/spinner.go @@ -1,6 +1,7 @@ package tui import ( + "fmt" "time" "github.com/databricks/cli/libs/cmdio" @@ -40,6 +41,12 @@ func newSpinnerCmd() *cobra.Command { }, } cmd.Flags().BoolVar(&elapsed, "elapsed", false, "show an MM:SS elapsed-time prefix on the spinner") + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { + if duration < 0 { + return fmt.Errorf("--duration must not be negative, got %s", duration) + } + return nil + } cmd.Flags().DurationVar(&duration, "duration", 0, "total time the spinner runs, split across its messages (0 keeps the built-in per-message timing)") return cmd } From cd1fdd0f42782cd45318a58e5db44390cbd5d9f4 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 16 Nov 2026 15:26:01 +0000 Subject: [PATCH 3/7] selftest: second review round for TUI commands - Pin COLORFGBG in the tui acceptance env - pager rejects negative --delay and --delay-first in PreRunE - Tighten the writeLine comment and the --stream help text Co-authored-by: Isaac --- acceptance/selftest/tui/test.toml | 1 + cmd/selftest/tui/colors.go | 6 +++--- cmd/selftest/tui/pager.go | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/acceptance/selftest/tui/test.toml b/acceptance/selftest/tui/test.toml index 53b4c801db0..cff9358cb18 100644 --- a/acceptance/selftest/tui/test.toml +++ b/acceptance/selftest/tui/test.toml @@ -7,6 +7,7 @@ Env.NO_COLOR = "" Env.CI = "" Env.TERM_PROGRAM = "" Env.WT_SESSION = "" +Env.COLORFGBG = "" # Only the non-interactive paths run here. Live rendering and raw mode need a # real TTY, which is checked by hand under a PTY. diff --git a/cmd/selftest/tui/colors.go b/cmd/selftest/tui/colors.go index f3c4025e8c9..3415dbf0ce3 100644 --- a/cmd/selftest/tui/colors.go +++ b/cmd/selftest/tui/colors.go @@ -72,13 +72,13 @@ are visible.`, return nil }, } - cmd.Flags().StringVar(&stream, "stream", "", "write output to stdout or stderr (default: cmdio.LogString, stderr)") + cmd.Flags().StringVar(&stream, "stream", "", "write output to stdout or stderr (default: stderr)") cmd.Flags().BoolVar(&swatches, "lipgloss", false, "also print truecolor and 256-color swatches") return cmd } -// writeLine keeps the default stream on cmdio.LogString so the default output -// stays exactly as it was before --stream existed. +// writeLine sends lines without --stream through cmdio.LogString, so the default +// output and its stderr routing are unchanged. func writeLine(ctx context.Context, stream string, w io.Writer, s string) { if stream == "" { cmdio.LogString(ctx, s) diff --git a/cmd/selftest/tui/pager.go b/cmd/selftest/tui/pager.go index 6dd238dc00a..c0bc07df330 100644 --- a/cmd/selftest/tui/pager.go +++ b/cmd/selftest/tui/pager.go @@ -88,6 +88,9 @@ func newPagerCmd() *cobra.Command { if items < 0 { return fmt.Errorf("--items must be non-negative, got %d", items) } + if delayFirst < 0 || delay < 0 { + return fmt.Errorf("--delay and --delay-first must not be negative, got %s and %s", delay, delayFirst) + } if failAfter < -1 { return fmt.Errorf("--fail-after must be -1 or greater, got %d", failAfter) } From 335fee731025aff1f9205a31795e7d6faa4a9f9c Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 16 Nov 2026 15:50:10 +0000 Subject: [PATCH 4/7] selftest: drop acceptance tests for TUI commands The existing selftest tui commands have no acceptance tests, and acceptance/selftest holds the harness's own self-tests. Keep the unit tests for the raw key reader and the fake iterator. Co-authored-by: Isaac --- acceptance/selftest/tui/colors/out.test.toml | 2 - acceptance/selftest/tui/colors/output.txt | 60 ------------------- acceptance/selftest/tui/colors/script | 18 ------ acceptance/selftest/tui/huh/out.test.toml | 2 - acceptance/selftest/tui/huh/output.txt | 5 -- acceptance/selftest/tui/huh/script | 2 - acceptance/selftest/tui/keys/out.test.toml | 2 - acceptance/selftest/tui/keys/output.txt | 23 ------- acceptance/selftest/tui/keys/script | 10 ---- acceptance/selftest/tui/pager/out.test.toml | 2 - acceptance/selftest/tui/pager/output.txt | 30 ---------- acceptance/selftest/tui/pager/script | 14 ----- acceptance/selftest/tui/profile/out.test.toml | 2 - acceptance/selftest/tui/profile/output.txt | 32 ---------- acceptance/selftest/tui/profile/script | 5 -- acceptance/selftest/tui/spinner/out.test.toml | 2 - acceptance/selftest/tui/spinner/output.txt | 5 -- acceptance/selftest/tui/spinner/script | 2 - acceptance/selftest/tui/table/out.test.toml | 2 - acceptance/selftest/tui/table/output.txt | 8 --- acceptance/selftest/tui/table/script | 3 - acceptance/selftest/tui/test.toml | 17 ------ 22 files changed, 248 deletions(-) delete mode 100644 acceptance/selftest/tui/colors/out.test.toml delete mode 100644 acceptance/selftest/tui/colors/output.txt delete mode 100644 acceptance/selftest/tui/colors/script delete mode 100644 acceptance/selftest/tui/huh/out.test.toml delete mode 100644 acceptance/selftest/tui/huh/output.txt delete mode 100644 acceptance/selftest/tui/huh/script delete mode 100644 acceptance/selftest/tui/keys/out.test.toml delete mode 100644 acceptance/selftest/tui/keys/output.txt delete mode 100644 acceptance/selftest/tui/keys/script delete mode 100644 acceptance/selftest/tui/pager/out.test.toml delete mode 100644 acceptance/selftest/tui/pager/output.txt delete mode 100644 acceptance/selftest/tui/pager/script delete mode 100644 acceptance/selftest/tui/profile/out.test.toml delete mode 100644 acceptance/selftest/tui/profile/output.txt delete mode 100644 acceptance/selftest/tui/profile/script delete mode 100644 acceptance/selftest/tui/spinner/out.test.toml delete mode 100644 acceptance/selftest/tui/spinner/output.txt delete mode 100644 acceptance/selftest/tui/spinner/script delete mode 100644 acceptance/selftest/tui/table/out.test.toml delete mode 100644 acceptance/selftest/tui/table/output.txt delete mode 100644 acceptance/selftest/tui/table/script delete mode 100644 acceptance/selftest/tui/test.toml diff --git a/acceptance/selftest/tui/colors/out.test.toml b/acceptance/selftest/tui/colors/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/colors/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/colors/output.txt b/acceptance/selftest/tui/colors/output.txt deleted file mode 100644 index dfda143af8c..00000000000 --- a/acceptance/selftest/tui/colors/output.txt +++ /dev/null @@ -1,60 +0,0 @@ - -=== Default stream is stderr, so stdout is empty - ->>> [CLI] selftest tui colors -red: the quick brown fox jumps over the lazy dog -green: the quick brown fox jumps over the lazy dog -yellow: the quick brown fox jumps over the lazy dog -blue: the quick brown fox jumps over the lazy dog -cyan: the quick brown fox jumps over the lazy dog -hiblack: the quick brown fox jumps over the lazy dog -hiblue: the quick brown fox jumps over the lazy dog - -=== --stream stdout writes the swatches to stdout -red: the quick brown fox jumps over the lazy dog -green: the quick brown fox jumps over the lazy dog -yellow: the quick brown fox jumps over the lazy dog -blue: the quick brown fox jumps over the lazy dog -cyan: the quick brown fox jumps over the lazy dog -hiblack: the quick brown fox jumps over the lazy dog -hiblue: the quick brown fox jumps over the lazy dog - -=== --stream stderr writes to stderr -red: the quick brown fox jumps over the lazy dog -green: the quick brown fox jumps over the lazy dog -yellow: the quick brown fox jumps over the lazy dog -blue: the quick brown fox jumps over the lazy dog -cyan: the quick brown fox jumps over the lazy dog -hiblack: the quick brown fox jumps over the lazy dog -hiblue: the quick brown fox jumps over the lazy dog - -=== Piped output has no escape codes, even with --lipgloss - ->>> NO_COLOR=1 [CLI] selftest tui colors --lipgloss -red: the quick brown fox jumps over the lazy dog -green: the quick brown fox jumps over the lazy dog -yellow: the quick brown fox jumps over the lazy dog -blue: the quick brown fox jumps over the lazy dog -cyan: the quick brown fox jumps over the lazy dog -hiblack: the quick brown fox jumps over the lazy dog -hiblue: the quick brown fox jumps over the lazy dog -truecolor: -256-color: - -=== --lipgloss prints both swatch rows - ->>> [CLI] selftest tui colors --lipgloss -red: the quick brown fox jumps over the lazy dog -green: the quick brown fox jumps over the lazy dog -yellow: the quick brown fox jumps over the lazy dog -blue: the quick brown fox jumps over the lazy dog -cyan: the quick brown fox jumps over the lazy dog -hiblack: the quick brown fox jumps over the lazy dog -hiblue: the quick brown fox jumps over the lazy dog -truecolor: -256-color: - -=== Unknown stream is rejected - ->>> [CLI] selftest tui colors --stream file -Error: --stream must be stdout or stderr, got "file" diff --git a/acceptance/selftest/tui/colors/script b/acceptance/selftest/tui/colors/script deleted file mode 100644 index 672e8833776..00000000000 --- a/acceptance/selftest/tui/colors/script +++ /dev/null @@ -1,18 +0,0 @@ -title "Default stream is stderr, so stdout is empty" -$CLI selftest tui colors 2>/dev/null | contains.py '!red:' -trace $CLI selftest tui colors 2>&1 >/dev/null | contains.py 'red:' 'hiblue:' - -title "--stream stdout writes the swatches to stdout" -$CLI selftest tui colors --stream stdout 2>/dev/null - -title "--stream stderr writes to stderr" -$CLI selftest tui colors --stream stderr 2>&1 >/dev/null - -title "Piped output has no escape codes, even with --lipgloss" -trace NO_COLOR=1 $CLI selftest tui colors --lipgloss 2>&1 | contains.py '!'$'\x1b' - -title "--lipgloss prints both swatch rows" -trace $CLI selftest tui colors --lipgloss 2>&1 | contains.py 'truecolor:' '256-color:' - -title "Unknown stream is rejected" -musterr trace $CLI selftest tui colors --stream file diff --git a/acceptance/selftest/tui/huh/out.test.toml b/acceptance/selftest/tui/huh/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/huh/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/huh/output.txt b/acceptance/selftest/tui/huh/output.txt deleted file mode 100644 index 514b6405dc4..00000000000 --- a/acceptance/selftest/tui/huh/output.txt +++ /dev/null @@ -1,5 +0,0 @@ - -=== Unknown field type is rejected - ->>> [CLI] selftest tui huh --field bogus -Error: --field must be one of input, select, confirm, multiselect, got "bogus" diff --git a/acceptance/selftest/tui/huh/script b/acceptance/selftest/tui/huh/script deleted file mode 100644 index 9515e131c05..00000000000 --- a/acceptance/selftest/tui/huh/script +++ /dev/null @@ -1,2 +0,0 @@ -title "Unknown field type is rejected" -musterr trace $CLI selftest tui huh --field bogus diff --git a/acceptance/selftest/tui/keys/out.test.toml b/acceptance/selftest/tui/keys/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/keys/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/keys/output.txt b/acceptance/selftest/tui/keys/output.txt deleted file mode 100644 index d0e2c572ea4..00000000000 --- a/acceptance/selftest/tui/keys/output.txt +++ /dev/null @@ -1,23 +0,0 @@ - -=== Each decoded key is printed; two Ctrl+C exit - ->>> [CLI] selftest tui keys --timeout 5s -[?25l[?2004hkey="a" type=-1 runes="a" alt=false paste=false -key="ctrl+c" type=3 runes="" alt=false paste=false -key="ctrl+c" type=3 runes="" alt=false paste=false - - - -[?2004l[?25h[?1002l[?1003l[?1006l -=== --raw prints the bytes read from stdin - ->>> [CLI] selftest tui keys --raw -raw bytes=61 62 03 03 text="ab\x03\x03" - -=== --timeout exits without any input - ->>> [CLI] selftest tui keys --timeout 1s -[?25l[?2004h - - -[?2004l[?25h[?1002l[?1003l[?1006l \ No newline at end of file diff --git a/acceptance/selftest/tui/keys/script b/acceptance/selftest/tui/keys/script deleted file mode 100644 index e97ed88cfe3..00000000000 --- a/acceptance/selftest/tui/keys/script +++ /dev/null @@ -1,10 +0,0 @@ -title "Each decoded key is printed; two Ctrl+C exit" -printf 'a\x03\x03' | trace $CLI selftest tui keys --timeout 5s 2>&1 | contains.py 'key="a" type=-1 runes="a"' 'key="ctrl+c" type=3' - -title "--raw prints the bytes read from stdin" -printf 'ab\x03\x03' | trace $CLI selftest tui keys --raw 2>&1 - -title "--timeout exits without any input" -# A pipe keeps stdin non-TTY and open: /dev/null makes bubbletea fail to create -# its cancel reader. The sleep is longer than the timeout so only --timeout can end the run. -sleep 2 | trace $CLI selftest tui keys --timeout 1s 2>&1 | contains.py '!key=' diff --git a/acceptance/selftest/tui/pager/out.test.toml b/acceptance/selftest/tui/pager/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/pager/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/pager/output.txt b/acceptance/selftest/tui/pager/output.txt deleted file mode 100644 index 8ff7db5f5e5..00000000000 --- a/acceptance/selftest/tui/pager/output.txt +++ /dev/null @@ -1,30 +0,0 @@ - -=== Non-interactive output uses the non-paged renderer - ->>> [CLI] selftest tui pager --items 3 -Name State -item-0001 RUNNING -item-0002 TERMINATED -item-0003 PENDING - -=== --header replaces the Name column header - ->>> [CLI] selftest tui pager --items 2 --header Workspace -Workspace State -item-0001 RUNNING -item-0002 TERMINATED - -=== An injected error after 2 rows is returned - ->>> [CLI] selftest tui pager --items 5 --fail-after 2 -Error: injected iterator error after 2 rows - -=== --fail-after 0 fails on the first Next - ->>> [CLI] selftest tui pager --items 5 --fail-after 0 -Error: injected iterator error after 0 rows - -=== Negative --items is rejected - ->>> [CLI] selftest tui pager --items -1 -Error: --items must be non-negative, got -1 diff --git a/acceptance/selftest/tui/pager/script b/acceptance/selftest/tui/pager/script deleted file mode 100644 index b515112dab1..00000000000 --- a/acceptance/selftest/tui/pager/script +++ /dev/null @@ -1,14 +0,0 @@ -title "Non-interactive output uses the non-paged renderer" -trace $CLI selftest tui pager --items 3 - -title "--header replaces the Name column header" -trace $CLI selftest tui pager --items 2 --header Workspace - -title "An injected error after 2 rows is returned" -musterr trace $CLI selftest tui pager --items 5 --fail-after 2 - -title "--fail-after 0 fails on the first Next" -musterr trace $CLI selftest tui pager --items 5 --fail-after 0 - -title "Negative --items is rejected" -musterr trace $CLI selftest tui pager --items -1 diff --git a/acceptance/selftest/tui/profile/out.test.toml b/acceptance/selftest/tui/profile/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/profile/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/profile/output.txt b/acceptance/selftest/tui/profile/output.txt deleted file mode 100644 index 9ac67daa218..00000000000 --- a/acceptance/selftest/tui/profile/output.txt +++ /dev/null @@ -1,32 +0,0 @@ - -=== Piped stdout and stderr never get color - ->>> [CLI] selftest tui profile -stdout: supports_color=false profile=Ascii dark_background=true -stderr: supports_color=false profile=Ascii dark_background=true -env: - TERM=xterm-256color - COLORTERM=(unset or empty) - CLICOLOR=(unset or empty) - CLICOLOR_FORCE=(unset or empty) - NO_COLOR=(unset or empty) - COLORFGBG=(unset or empty) - CI=(unset or empty) - TERM_PROGRAM=(unset or empty) - WT_SESSION=(unset or empty) - -=== NO_COLOR is reported in the env section - ->>> NO_COLOR=1 [CLI] selftest tui profile -stdout: supports_color=false profile=Ascii dark_background=true -stderr: supports_color=false profile=Ascii dark_background=true -env: - TERM=xterm-256color - COLORTERM=(unset or empty) - CLICOLOR=(unset or empty) - CLICOLOR_FORCE=(unset or empty) - NO_COLOR=1 - COLORFGBG=(unset or empty) - CI=(unset or empty) - TERM_PROGRAM=(unset or empty) - WT_SESSION=(unset or empty) diff --git a/acceptance/selftest/tui/profile/script b/acceptance/selftest/tui/profile/script deleted file mode 100644 index 499165d23a0..00000000000 --- a/acceptance/selftest/tui/profile/script +++ /dev/null @@ -1,5 +0,0 @@ -title "Piped stdout and stderr never get color" -trace $CLI selftest tui profile - -title "NO_COLOR is reported in the env section" -trace NO_COLOR=1 $CLI selftest tui profile diff --git a/acceptance/selftest/tui/spinner/out.test.toml b/acceptance/selftest/tui/spinner/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/spinner/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/spinner/output.txt b/acceptance/selftest/tui/spinner/output.txt deleted file mode 100644 index a328e621e17..00000000000 --- a/acceptance/selftest/tui/spinner/output.txt +++ /dev/null @@ -1,5 +0,0 @@ - -=== --duration is accepted and the spinner completes - ->>> [CLI] selftest tui spinner --duration 500ms -Spinner test complete diff --git a/acceptance/selftest/tui/spinner/script b/acceptance/selftest/tui/spinner/script deleted file mode 100644 index bc316cf0ac7..00000000000 --- a/acceptance/selftest/tui/spinner/script +++ /dev/null @@ -1,2 +0,0 @@ -title "--duration is accepted and the spinner completes" -trace $CLI selftest tui spinner --duration 500ms diff --git a/acceptance/selftest/tui/table/out.test.toml b/acceptance/selftest/tui/table/out.test.toml deleted file mode 100644 index 2e1543aa802..00000000000 --- a/acceptance/selftest/tui/table/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -GOOS.windows = false diff --git a/acceptance/selftest/tui/table/output.txt b/acceptance/selftest/tui/table/output.txt deleted file mode 100644 index 727afc5320b..00000000000 --- a/acceptance/selftest/tui/table/output.txt +++ /dev/null @@ -1,8 +0,0 @@ - -=== Flag validation runs before the TTY is needed - ->>> [CLI] selftest tui table --cols 0 -Error: --cols must be at least 1, got 0 - ->>> [CLI] selftest tui table --rows -1 -Error: --rows must be non-negative, got -1 diff --git a/acceptance/selftest/tui/table/script b/acceptance/selftest/tui/table/script deleted file mode 100644 index 7e339f26ca3..00000000000 --- a/acceptance/selftest/tui/table/script +++ /dev/null @@ -1,3 +0,0 @@ -title "Flag validation runs before the TTY is needed" -musterr trace $CLI selftest tui table --cols 0 -musterr trace $CLI selftest tui table --rows -1 diff --git a/acceptance/selftest/tui/test.toml b/acceptance/selftest/tui/test.toml deleted file mode 100644 index cff9358cb18..00000000000 --- a/acceptance/selftest/tui/test.toml +++ /dev/null @@ -1,17 +0,0 @@ -# Pin the color and terminal env so the profile output does not depend on the host. -Env.TERM = "xterm-256color" -Env.COLORTERM = "" -Env.CLICOLOR = "" -Env.CLICOLOR_FORCE = "" -Env.NO_COLOR = "" -Env.CI = "" -Env.TERM_PROGRAM = "" -Env.WT_SESSION = "" -Env.COLORFGBG = "" - -# Only the non-interactive paths run here. Live rendering and raw mode need a -# real TTY, which is checked by hand under a PTY. - -# The scripts use bash, printf and sleep, and the goldens embed bubbletea escape -# sequences. Not verified on Windows, so these tests run on Linux and macOS only. -GOOS.windows = false From 7888c244d44eeace4d82087d525afc0acc355c59 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 19:06:21 +0000 Subject: [PATCH 5/7] selftest: return the carriage in keys output under raw mode bubbletea puts the terminal in raw mode, which clears OPOST, so the bare "\n" from the keys log staircased across the screen. Use "\r\n" there too, the same way the --raw path already does. The other new commands print outside any tea program: spinner's program has no input, and the table, pager and huh output comes after or outside tea's raw-mode window. Co-authored-by: Isaac --- cmd/selftest/tui/keys.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/selftest/tui/keys.go b/cmd/selftest/tui/keys.go index d8ffef5c484..4ff18a28519 100644 --- a/cmd/selftest/tui/keys.go +++ b/cmd/selftest/tui/keys.go @@ -23,6 +23,7 @@ const keysExitCtrlC = 2 // Update: tea.Println runs its commands concurrently, so the order is not kept. type keysModel struct { out io.Writer + eol string ctrlC int } @@ -35,7 +36,7 @@ func (m *keysModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } line := fmt.Sprintf("key=%q type=%d runes=%q alt=%t paste=%t", key.String(), key.Type, string(key.Runes), key.Alt, key.Paste) - fmt.Fprintln(m.out, line) + fmt.Fprint(m.out, line+m.eol) if key.Type == tea.KeyCtrlC { m.ctrlC++ if m.ctrlC >= keysExitCtrlC { @@ -65,14 +66,20 @@ With --raw, bubbletea is bypassed: when stdin is a terminal it is put in raw mode, and each chunk of bytes read is printed as hex and quoted text.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() in := cmd.InOrStdin() out := cmd.ErrOrStderr() if raw { - return runRawKeys(cmd.Context(), in, out, timeout) + return runRawKeys(ctx, in, out, timeout) } // Mirrors cmdio's runTUI options. cmdio does not expose a way to run a // custom model, so keep these in sync with libs/cmdio/io.go. - p := tea.NewProgram(&keysModel{out: out}, + // Raw mode clears OPOST, so "\n" would not return the carriage. + eol := "\n" + if _, ok := rawInputFile(ctx, in); ok { + eol = "\r\n" + } + p := tea.NewProgram(&keysModel{out: out, eol: eol}, tea.WithInput(in), tea.WithOutput(out), tea.WithoutSignalHandler(), @@ -96,6 +103,13 @@ mode, and each chunk of bytes read is printed as hex and quoted text.`, return cmd } +// rawInputFile returns stdin as a file when it is a terminal. bubbletea and the +// --raw path put that file in raw mode, which is why output needs "\r\n". +func rawInputFile(ctx context.Context, in io.Reader) (*os.File, bool) { + f, ok := in.(*os.File) + return f, ok && cmdio.IsInputTTY(ctx) +} + // runRawKeys reads stdin in raw mode without bubbletea. After an early return // the reader goroutine can be blocked on Read or on sending a chunk; the // process exits once the command finishes, so it does not need to be joined. @@ -103,7 +117,7 @@ func runRawKeys(ctx context.Context, in io.Reader, out io.Writer, timeout time.D // Raw mode clears OPOST, so "\n" would not return the carriage. Print // "\r\n" in that case so each line starts at column 0. eol := "\n" - if f, ok := in.(*os.File); ok && cmdio.IsInputTTY(ctx) { + if f, ok := rawInputFile(ctx, in); ok { state, err := term.MakeRaw(int(f.Fd())) if err != nil { return err From d5e908ac3437c3606ee873e6a68438f1959c7010 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 19:06:37 +0000 Subject: [PATCH 6/7] selftest: print the resolved huh button colour GetForeground returned the static AdaptiveColor, which printed the same Light/Dark pair whatever the terminal was. Print the side lipgloss picks with the default renderer, which is the renderer the theme's styles use. Co-authored-by: Isaac --- cmd/selftest/tui/huh.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/selftest/tui/huh.go b/cmd/selftest/tui/huh.go index d1f96a32ce4..cc42e92c209 100644 --- a/cmd/selftest/tui/huh.go +++ b/cmd/selftest/tui/huh.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/charmbracelet/huh" + "github.com/charmbracelet/lipgloss" "github.com/databricks/cli/libs/apps/prompt" "github.com/databricks/cli/libs/cmdio" "github.com/spf13/cobra" @@ -75,7 +76,21 @@ so this checks the theme, not the production prompt flows.`, r, _ := cmdio.NewRenderer(ctx, cmd.ErrOrStderr()) cmdio.LogString(ctx, fmt.Sprintf("dark_background=%t", r.HasDarkBackground())) - cmdio.LogString(ctx, fmt.Sprintf("blurred_button=%+v", theme.Focused.BlurredButton.GetForeground())) + + // The blurred button colour is an AdaptiveColor, which hides the choice + // behind one value. lipgloss resolves it with the default renderer, the + // one the theme's styles render with, so print the side it picks. + fg := theme.Focused.BlurredButton.GetForeground() + if adaptive, ok := fg.(lipgloss.AdaptiveColor); ok { + dark := lipgloss.HasDarkBackground() + resolved := adaptive.Light + if dark { + resolved = adaptive.Dark + } + cmdio.LogString(ctx, fmt.Sprintf("blurred_button=%s (resolved, default renderer dark_background=%t)", resolved, dark)) + } else { + cmdio.LogString(ctx, fmt.Sprintf("blurred_button=%+v (not an AdaptiveColor, not resolved)", fg)) + } return nil }, } From 3f86c74582ca584e687bf187684acfe4f10eea95 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 17 Nov 2026 19:06:59 +0000 Subject: [PATCH 7/7] selftest: show NO_COLOR and TERM=dumb separately in tui profile After #7700 NO_COLOR only turns color off, and TERM=dumb also turns off prompts and spinners. Print cmdio's interactive mode and prompt decision next to the raw env values, so a run under NO_COLOR shows prompts still on. This uses the existing cmdio helpers; libs/cmdio is unchanged. Co-authored-by: Isaac --- cmd/selftest/tui/profile.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/selftest/tui/profile.go b/cmd/selftest/tui/profile.go index a1524c0debe..d33f98a8265 100644 --- a/cmd/selftest/tui/profile.go +++ b/cmd/selftest/tui/profile.go @@ -20,11 +20,14 @@ var profileEnvVars = []string{ func newProfileCmd() *cobra.Command { return &cobra.Command{ Use: "profile", - Short: "Print the color decision cmdio makes for stdout and stderr", + Short: "Print the color and prompt decisions cmdio makes for stdout and stderr", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() w := cmd.OutOrStdout() + // NO_COLOR only removes color; TERM=dumb also rules out prompts and + // spinners. The env block below has the raw values, this line the decision. + fmt.Fprintf(w, "interactive=%s prompt=%t\n", cmdio.GetInteractiveMode(ctx), cmdio.IsPromptSupported(ctx)) for _, s := range []struct { name string out io.Writer