feat(ansi): better logging and option to disab;e

This commit is contained in:
Barrett Ruth 2025-09-20 14:37:51 -04:00
parent bd81743274
commit f60f6dd5bb
8 changed files with 159 additions and 10 deletions

View file

@ -212,4 +212,32 @@ describe('ansi parser', function()
assert.is_nil(state.foreground)
end)
end)
describe('setup_highlight_groups', function()
it('creates highlight groups with fallback colors when terminal colors are nil', function()
local original_colors = {}
for i = 0, 15 do
original_colors[i] = vim.g['terminal_color_' .. i]
vim.g['terminal_color_' .. i] = nil
end
ansi.setup_highlight_groups()
local highlight = vim.api.nvim_get_hl(0, { name = 'CpAnsiRed' })
assert.equals('NONE', highlight.fg)
for i = 0, 15 do
vim.g['terminal_color_' .. i] = original_colors[i]
end
end)
it('creates highlight groups with proper colors when terminal colors are set', function()
vim.g.terminal_color_1 = '#ff0000'
ansi.setup_highlight_groups()
local highlight = vim.api.nvim_get_hl(0, { name = 'CpAnsiRed' })
assert.equals('#ff0000', highlight.fg)
end)
end)
end)

View file

@ -81,6 +81,16 @@ describe('cp.config', function()
end)
describe('run_panel config validation', function()
it('validates ansi is boolean', function()
local invalid_config = {
run_panel = { ansi = 'invalid' },
}
assert.has_error(function()
config.setup(invalid_config)
end, 'ansi color parsing must be enabled xor disabled')
end)
it('validates diff_mode values', function()
local invalid_config = {
run_panel = { diff_mode = 'invalid' },
@ -114,6 +124,7 @@ describe('cp.config', function()
it('accepts valid run_panel config', function()
local valid_config = {
run_panel = {
ansi = false,
diff_mode = 'git',
next_test_key = 'j',
prev_test_key = 'k',

65
spec/run_spec.lua Normal file
View file

@ -0,0 +1,65 @@
describe('run module ANSI processing', function()
local run = require('cp.run')
local config = require('cp.config')
describe('ANSI processing modes', function()
it('parses ANSI when ansi config is true', function()
local test_config = config.setup({ run_panel = { ansi = true } })
local result = {
stdout = 'Hello \027[31mworld\027[0m!',
stderr = '',
code = 0,
ok = true,
signal = nil,
timed_out = false,
}
local processed = run.process_test_result(result, 'expected_output', test_config)
assert.equals('Hello world!', processed.actual)
assert.equals(1, #processed.actual_highlights)
assert.equals('CpAnsiRed', processed.actual_highlights[1].highlight_group)
end)
it('strips ANSI when ansi config is false', function()
local test_config = config.setup({ run_panel = { ansi = false } })
local result = {
stdout = 'Hello \027[31mworld\027[0m!',
stderr = '',
code = 0,
ok = true,
signal = nil,
timed_out = false,
}
local processed = run.process_test_result(result, 'expected_output', test_config)
assert.equals('Hello world!', processed.actual)
assert.equals(0, #processed.actual_highlights)
end)
it('handles compilation failure with ansi disabled', function()
local test_config = config.setup({ run_panel = { ansi = false } })
local compilation_output = 'error.cpp:1:1: \027[1m\027[31merror:\027[0m undefined variable'
-- Create mock run panel state
run._test_set_panel_state({
test_cases = {
{ index = 1, input = 'test', expected = 'expected' },
},
})
run.handle_compilation_failure(compilation_output)
local panel_state = run.get_run_panel_state()
local test_case = panel_state.test_cases[1]
assert.equals('error.cpp:1:1: error: undefined variable', test_case.actual)
assert.equals(0, #test_case.actual_highlights)
assert.equals('Compilation failed', test_case.error)
end)
end)
end)