feat(pytest): strip ansi codes from errors

This commit is contained in:
Rónán Carrigan
2025-09-19 12:16:58 +01:00
parent ed9b4d794b
commit 1b1b1abf92
2 changed files with 5 additions and 3 deletions

View File

@@ -53,7 +53,7 @@ end
---@param positions neotest.Tree ---@param positions neotest.Tree
---@param root string ---@param root string
local function discover_params(python, script, path, positions, root) local function discover_params(python, script, path, positions, root)
local cmd = vim.tbl_flatten({ python, script, "--pytest-collect", path }) local cmd = vim.iter({ python, script, "--pytest-collect", path }):flatten():totable()
logger.debug("Running test instance discovery:", cmd) logger.debug("Running test instance discovery:", cmd)
local test_params = {} local test_params = {}
@@ -66,7 +66,7 @@ local function discover_params(python, script, path, positions, root)
return {} return {}
end end
for line in vim.gsplit(data.stdout, "\n", true) do for line in vim.gsplit(data.stdout, "\n", { plain = true }) do
local param_index = string.find(line, "[", nil, true) local param_index = string.find(line, "[", nil, true)
if param_index then if param_index then
local test_id = root .. lib.files.path.sep .. string.sub(line, 1, param_index - 1) local test_id = root .. lib.files.path.sep .. string.sub(line, 1, param_index - 1)

View File

@@ -1,6 +1,7 @@
from io import StringIO from io import StringIO
import json import json
from pathlib import Path from pathlib import Path
import re
from typing import Callable, Dict, List, Optional, Union from typing import Callable, Dict, List, Optional, Union
import pytest import pytest
@@ -10,6 +11,7 @@ from _pytest.fixtures import FixtureLookupErrorRepr
from .base import NeotestAdapter, NeotestError, NeotestResult, NeotestResultStatus from .base import NeotestAdapter, NeotestError, NeotestResult, NeotestResultStatus
ANSI_ESCAPE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
class PytestNeotestAdapter(NeotestAdapter): class PytestNeotestAdapter(NeotestAdapter):
def __init__(self, emit_parameterized_ids: bool): def __init__(self, emit_parameterized_ids: bool):
@@ -131,7 +133,7 @@ class NeotestResultCollector:
errors.append({"message": msg_prefix + exc_repr, "line": None}) errors.append({"message": msg_prefix + exc_repr, "line": None})
# Test failed internally # Test failed internally
elif isinstance(exc_repr, ExceptionRepr): elif isinstance(exc_repr, ExceptionRepr):
error_message = exc_repr.reprcrash.message # type: ignore error_message = ANSI_ESCAPE.sub('', exc_repr.reprcrash.message) # type: ignore
error_line = None error_line = None
for traceback_entry in reversed(call.excinfo.traceback): for traceback_entry in reversed(call.excinfo.traceback):
if str(traceback_entry.path) == abs_path: if str(traceback_entry.path) == abs_path: