mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 11:25:37 +00:00
fix: TOML colorization now works when NO_COLOR env is set (#2584)
The colorizeToml function intended to force colors by setting color.NoColor = false, but SprintFunc() still respects the NO_COLOR environment variable. This caused TestTomlColourization to fail in CI environments where NO_COLOR=1 is set. Fixed by calling EnableColor() on each color object, which explicitly forces colors regardless of environment settings. Vibe-coded with Cursor (Claude Opus 4)
This commit is contained in:
parent
5f90039bdc
commit
c4f4e6d416
@ -546,12 +546,20 @@ func (te *tomlEncoder) colorizeToml(input []byte) []byte {
|
|||||||
color.NoColor = false
|
color.NoColor = false
|
||||||
|
|
||||||
// Create color functions for different token types
|
// Create color functions for different token types
|
||||||
commentColor := color.New(color.FgHiBlack).SprintFunc()
|
// Use EnableColor() to ensure colors work even when NO_COLOR env is set
|
||||||
stringColor := color.New(color.FgGreen).SprintFunc()
|
commentColorObj := color.New(color.FgHiBlack); commentColorObj.EnableColor()
|
||||||
numberColor := color.New(color.FgHiMagenta).SprintFunc()
|
stringColorObj := color.New(color.FgGreen); stringColorObj.EnableColor()
|
||||||
keyColor := color.New(color.FgCyan).SprintFunc()
|
numberColorObj := color.New(color.FgHiMagenta); numberColorObj.EnableColor()
|
||||||
boolColor := color.New(color.FgHiMagenta).SprintFunc()
|
keyColorObj := color.New(color.FgCyan); keyColorObj.EnableColor()
|
||||||
sectionColor := color.New(color.FgYellow, color.Bold).SprintFunc()
|
boolColorObj := color.New(color.FgHiMagenta); boolColorObj.EnableColor()
|
||||||
|
sectionColorObj := color.New(color.FgYellow, color.Bold); sectionColorObj.EnableColor()
|
||||||
|
|
||||||
|
commentColor := commentColorObj.SprintFunc()
|
||||||
|
stringColor := stringColorObj.SprintFunc()
|
||||||
|
numberColor := numberColorObj.SprintFunc()
|
||||||
|
keyColor := keyColorObj.SprintFunc()
|
||||||
|
boolColor := boolColorObj.SprintFunc()
|
||||||
|
sectionColor := sectionColorObj.SprintFunc()
|
||||||
|
|
||||||
// Simple tokenization for TOML colouring
|
// Simple tokenization for TOML colouring
|
||||||
i := 0
|
i := 0
|
||||||
|
|||||||
@ -632,6 +632,11 @@ func TestTomlScenarios(t *testing.T) {
|
|||||||
// TestTomlColourization tests that colourization correctly distinguishes
|
// TestTomlColourization tests that colourization correctly distinguishes
|
||||||
// between table section headers and inline arrays
|
// between table section headers and inline arrays
|
||||||
func TestTomlColourization(t *testing.T) {
|
func TestTomlColourization(t *testing.T) {
|
||||||
|
// Save and restore color state
|
||||||
|
oldNoColor := color.NoColor
|
||||||
|
color.NoColor = false
|
||||||
|
defer func() { color.NoColor = oldNoColor }()
|
||||||
|
|
||||||
// Test that inline arrays are not coloured as table sections
|
// Test that inline arrays are not coloured as table sections
|
||||||
encoder := &tomlEncoder{prefs: TomlPreferences{ColorsEnabled: true}}
|
encoder := &tomlEncoder{prefs: TomlPreferences{ColorsEnabled: true}}
|
||||||
|
|
||||||
@ -655,8 +660,9 @@ alpha = "test"
|
|||||||
// for actual table sections, not for inline arrays.
|
// for actual table sections, not for inline arrays.
|
||||||
|
|
||||||
// Get the ANSI codes for section colour (Yellow + Bold)
|
// Get the ANSI codes for section colour (Yellow + Bold)
|
||||||
sectionColour := color.New(color.FgYellow, color.Bold).SprintFunc()
|
sectionColourObj := color.New(color.FgYellow, color.Bold)
|
||||||
sampleSection := sectionColour("[database]")
|
sectionColourObj.EnableColor()
|
||||||
|
sampleSection := sectionColourObj.Sprint("[database]")
|
||||||
|
|
||||||
// Extract just the ANSI codes from the sample
|
// Extract just the ANSI codes from the sample
|
||||||
// ANSI codes start with \x1b[
|
// ANSI codes start with \x1b[
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user