test(cli): cover --no-color flag handling and ui.DisableColors

Extract the --no-color handling into a testable applyNoColorFlag helper
and add unit tests verifying it clears colors only when the flag is set.
Also add a ui.DisableColors test asserting styles stop emitting ANSI.

Addresses codecov gap on GH#4222.
This commit is contained in:
kev 2026-05-28 23:32:57 -04:00
commit feb0f4c7bc
3 changed files with 77 additions and 3 deletions

View file

@ -197,6 +197,15 @@ func forcedMigratePreviewFlag(cmd *cobra.Command) string {
return ""
}
// applyNoColorFlag disables colorized output when --no-color is set.
// Complements the NO_COLOR / CLICOLOR=0 env detection in package ui,
// giving callers a per-invocation override.
func applyNoColorFlag() {
if noColorFlag {
ui.DisableColors()
}
}
// loadBeadsEnvFile loads .beads/.env into process environment for per-project
// Dolt credentials (GH#2520). Uses gotenv.Load which is non-overriding —
// existing shell env vars always take precedence.
@ -693,9 +702,7 @@ var rootCmd = &cobra.Command{
_ = cmd.Help() // Help() always returns nil for cobra commands
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if noColorFlag {
ui.DisableColors()
}
applyNoColorFlag()
// Initialize CommandContext to hold runtime state (replaces scattered globals)
initCommandContext()

43
cmd/bd/no_color_test.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"strings"
"testing"
lipgloss "charm.land/lipgloss/v2"
"github.com/steveyegge/beads/internal/ui"
)
func TestApplyNoColorFlag(t *testing.T) {
savedFlag := noColorFlag
savedStyle := ui.AccentStyle
savedColor := ui.ColorAccent
t.Cleanup(func() {
noColorFlag = savedFlag
ui.AccentStyle = savedStyle
ui.ColorAccent = savedColor
})
// Seed a colored style so we can observe it being cleared.
ui.ColorAccent = lipgloss.Color("#ff0000")
ui.AccentStyle = lipgloss.NewStyle().Foreground(ui.ColorAccent)
t.Run("flag unset leaves styles untouched", func(t *testing.T) {
noColorFlag = false
applyNoColorFlag()
if _, ok := ui.ColorAccent.(lipgloss.NoColor); ok {
t.Error("colors disabled even though --no-color was not set")
}
})
t.Run("flag set disables colors", func(t *testing.T) {
noColorFlag = true
applyNoColorFlag()
if _, ok := ui.ColorAccent.(lipgloss.NoColor); !ok {
t.Errorf("ColorAccent not reset to NoColor, got %T", ui.ColorAccent)
}
if out := ui.AccentStyle.Render("hi"); strings.ContainsRune(out, '\x1b') {
t.Errorf("AccentStyle still emits ANSI after --no-color: %q", out)
}
})
}

View file

@ -5,6 +5,7 @@ import (
"strings"
"testing"
lipgloss "charm.land/lipgloss/v2"
"github.com/steveyegge/beads/internal/types"
)
@ -151,6 +152,29 @@ func TestRenderClosedUtilities(t *testing.T) {
}
}
func TestDisableColors(t *testing.T) {
// Save and restore mutated globals so other tests are unaffected.
savedStyle := AccentStyle
savedColor := ColorAccent
t.Cleanup(func() {
AccentStyle = savedStyle
ColorAccent = savedColor
})
// Simulate a color-enabled init: a style carrying an explicit foreground.
ColorAccent = lipgloss.Color("#ff0000")
AccentStyle = lipgloss.NewStyle().Foreground(ColorAccent)
DisableColors()
if _, ok := ColorAccent.(lipgloss.NoColor); !ok {
t.Errorf("ColorAccent not reset to NoColor, got %T", ColorAccent)
}
if out := AccentStyle.Render("hello"); strings.ContainsRune(out, '\x1b') {
t.Errorf("AccentStyle still emits ANSI escape after DisableColors: %q", out)
}
}
func TestRenderCommandAndCategoryAreUppercaseSafe(t *testing.T) {
got := RenderCategory(" already upper ")
if !strings.Contains(got, " ALREADY UPPER ") {