test(unit): fix failing tests related to RichTerm usage #110

Merged
brent.edwards merged 2 commits from test/fix-unit-tests2 into master 2026-02-19 00:34:33 +00:00
7 changed files with 108 additions and 55 deletions
@@ -106,6 +106,20 @@ def step_spec_service(context: Context) -> None:
context._cleanup_handlers = []
context._cleanup_handlers.append(context.service_patcher.stop)
# Patch the module-level Rich Console so it never injects ANSI escape
# codes. Without this, Rich detects a colour terminal and embeds
# escape sequences in the CLI output, causing plain-text assertions
# (e.g. ``"Action Created" in output``) to fail on real terminals
# while passing inside headless CI containers.
from rich.console import Console as _Console
_plain_console = _Console(no_color=True, highlight=False, width=300)
context.console_patcher = patch(
"cleveragents.cli.commands.action.console", _plain_console
)
context.console_patcher.start()
context._cleanup_handlers.append(context.console_patcher.stop)
# ---------------------------------------------------------------------------
# Given steps
+6 -1
View File
@@ -119,7 +119,12 @@ def _run_with_container(context: Any, func: Any, *args: Any, **kwargs: Any) -> N
mock_cont = _mock_container(context)
buf = StringIO()
test_console = RichConsole(file=buf, no_color=True)
# force_terminal=False and highlight=False prevent Rich from emitting
# ANSI escape codes when FORCE_COLOR is set or a real colour terminal
# is detected.
test_console = RichConsole(
file=buf, no_color=True, highlight=False, force_terminal=False
)
with (
patch(
@@ -30,6 +30,14 @@ from cleveragents.core.exceptions import ValidationError
_PATCH_TARGET = "cleveragents.cli.commands.resource._get_registry_service"
_PATCH_CONTAINER = "cleveragents.cli.commands.resource.get_container"
_PATCH_CONSOLE = "cleveragents.cli.commands.resource.console"
def _no_color_console():
"""Return a Rich Console that never emits ANSI escape codes."""
from rich.console import Console as _Console
return _Console(no_color=True, highlight=False, force_terminal=False)
def _make_mock_type_spec(*, built_in: bool = False) -> MagicMock:
@@ -136,7 +144,10 @@ def step_invoke_type_add_update(context: Context) -> None:
tmp.flush()
config_path = tmp.name
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["type", "add", "--config", config_path, "--update"],
@@ -165,7 +176,10 @@ def step_invoke_type_add_fnf(context: Context) -> None:
tmp.flush()
config_path = tmp.name
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["type", "add", "--config", config_path],
@@ -189,7 +203,10 @@ def step_invoke_type_remove_no(context: Context) -> None:
from cleveragents.cli.commands.resource import app
runner = CliRunner()
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["type", "remove", "test/mock-type"],
@@ -234,7 +251,10 @@ def step_invoke_type_remove_row_none(context: Context) -> None:
from cleveragents.cli.commands.resource import app
runner = CliRunner()
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["type", "remove", "test/mock-type", "--yes"],
@@ -282,7 +302,10 @@ def step_invoke_type_remove_exception(context: Context) -> None:
from cleveragents.cli.commands.resource import app
runner = CliRunner()
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["type", "remove", "test/mock-type", "--yes"],
@@ -326,7 +349,10 @@ def step_invoke_resource_remove_edges(context: Context) -> None:
from cleveragents.cli.commands.resource import app
runner = CliRunner()
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["remove", "local/mock-res", "--yes"],
@@ -377,7 +403,10 @@ def step_invoke_resource_remove_exception(context: Context) -> None:
from cleveragents.cli.commands.resource import app
runner = CliRunner()
with patch(_PATCH_TARGET, return_value=context.rcb_mock_service):
with (
patch(_PATCH_TARGET, return_value=context.rcb_mock_service),
patch(_PATCH_CONSOLE, _no_color_console()),
):
context.rcb_result = runner.invoke(
app,
["remove", "local/mock-res", "--yes"],
+6 -1
View File
@@ -35,7 +35,12 @@ def _make_service(context: Context) -> ResourceRegistryService:
def _capture_output(func: Any, *args: Any, **kwargs: Any) -> tuple[str, bool]:
"""Run a CLI function capturing its console output and success status."""
buf = StringIO()
console = Console(file=buf, width=200, no_color=True)
# force_terminal=False and highlight=False prevent Rich from emitting
# ANSI escape codes (bold, syntax highlighting) when FORCE_COLOR is set
# or a real colour terminal is detected.
console = Console(
file=buf, width=200, no_color=True, highlight=False, force_terminal=False
)
import cleveragents.cli.commands.resource as resource_mod
+7 -7
View File
@@ -19,7 +19,7 @@ Initialize New Project
[Setup] Setup Test Directory
${result}= Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME}
... cwd=${TEST_DIR} env:CLEVERAGENTS_AUTO_APPLY_MIGRATIONS=true stderr=STDOUT timeout=30s
... cwd=${TEST_DIR} env:CLEVERAGENTS_AUTO_APPLY_MIGRATIONS=true stderr=STDOUT timeout=60s
Should Be Equal As Integers ${result.rc} 0 Init failed: ${result.stdout}
Should Exist ${TEST_DIR}${/}.cleveragents
@@ -55,7 +55,7 @@ Create Plan With Tell
[Setup] Initialize Test Project
${result}= Run Process ${PYTHON} -m cleveragents tell Add error handling
... cwd=${TEST_DIR} timeout=30s
... cwd=${TEST_DIR} timeout=60s
Should Be Equal As Integers ${result.rc} 0
@@ -72,7 +72,7 @@ Build Plan
[Setup] Initialize Test Project With Plan
${result}= Run Process ${PYTHON} -m cleveragents build
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
Should Be Equal As Integers ${result.rc} 0
@@ -83,7 +83,7 @@ Apply Plan Changes
[Setup] Initialize Test Project With Built Plan
${result}= Run Process ${PYTHON} -m cleveragents apply --yes
... cwd=${TEST_DIR} timeout=30s
... cwd=${TEST_DIR} timeout=60s
Should Be Equal As Integers ${result.rc} 0
@@ -219,7 +219,7 @@ Clear All Context
[Setup] Initialize Test Project With Context
${result}= Run Process ${PYTHON} -m cleveragents context clear --yes
... cwd=${TEST_DIR} env:CLEVERAGENTS_AUTO_APPLY_MIGRATIONS=true stderr=STDOUT timeout=30s
... cwd=${TEST_DIR} env:CLEVERAGENTS_AUTO_APPLY_MIGRATIONS=true stderr=STDOUT timeout=60s
Should Be Equal As Integers ${result.rc} 0 Context clear failed: ${result.stdout}
@@ -266,14 +266,14 @@ Initialize Test Project With Plan
[Documentation] Initialize project and create a plan
Initialize Test Project
${result}= Run Process ${PYTHON} -m cleveragents tell Test instruction
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
Should Be Equal As Integers ${result.rc} 0
Initialize Test Project With Built Plan
[Documentation] Initialize project with a built plan
Initialize Test Project With Plan
${result}= Run Process ${PYTHON} -m cleveragents build
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
... cwd=${TEST_DIR} env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
Should Be Equal As Integers ${result.rc} 0
Initialize Test Project With Multiple Plans
+38 -38
View File
@@ -16,7 +16,7 @@ ${PROJECT_NAME} test-project
*** Test Cases ***
Test CLI Help Shows All Commands
[Documentation] Verify that CLI help displays all expected command groups
${result} = Run Process ${PYTHON} -m cleveragents --help timeout=30s
${result} = Run Process ${PYTHON} -m cleveragents --help timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} AI-powered development assistant
Should Contain ${result.stdout} project
@@ -31,7 +31,7 @@ Test Project Initialization
[Documentation] Test initializing a new CleverAgents project
Create Directory ${TEST_DIR}/project1
${result} = Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME}
... cwd=${TEST_DIR}/project1 timeout=30s
... cwd=${TEST_DIR}/project1 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Project '${PROJECT_NAME}' initialized successfully
Directory Should Exist ${TEST_DIR}/project1/.cleveragents
@@ -44,9 +44,9 @@ Test Project Initialization
Test Project Cannot Initialize Twice
[Documentation] Verify project cannot be initialized twice without force
Create Directory ${TEST_DIR}/project2
Run Process ${PYTHON} -m cleveragents init first-project cwd=${TEST_DIR}/project2 timeout=30s
Run Process ${PYTHON} -m cleveragents init first-project cwd=${TEST_DIR}/project2 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents init second-project
... cwd=${TEST_DIR}/project2 timeout=30s
... cwd=${TEST_DIR}/project2 timeout=60s
Should Not Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stderr} Project already initialized
Should Contain ${result.stderr} --force
@@ -54,19 +54,19 @@ Test Project Cannot Initialize Twice
Test Force Reinitialize Project
[Documentation] Test force reinitialization of a project
Create Directory ${TEST_DIR}/project3
Run Process ${PYTHON} -m cleveragents init first-project cwd=${TEST_DIR}/project3 timeout=30s
Run Process ${PYTHON} -m cleveragents init first-project cwd=${TEST_DIR}/project3 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents init second-project --force
... cwd=${TEST_DIR}/project3 timeout=30s
... cwd=${TEST_DIR}/project3 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Project 'second-project' initialized successfully
Test Context Add Files
[Documentation] Test adding files to context
Create Directory ${TEST_DIR}/project4
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project4 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project4 timeout=60s
Create File ${TEST_DIR}/project4/test.py print('hello world')
${result} = Run Process ${PYTHON} -m cleveragents context add test.py
... cwd=${TEST_DIR}/project4 timeout=30s
... cwd=${TEST_DIR}/project4 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Added 1 file(s) to context
Should Contain ${result.stdout} test.py
@@ -74,13 +74,13 @@ Test Context Add Files
Test Context List Files
[Documentation] Test listing context files
Create Directory ${TEST_DIR}/project5
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project5 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project5 timeout=60s
Create File ${TEST_DIR}/project5/file1.py # File 1
Create File ${TEST_DIR}/project5/file2.py # File 2
Run Process ${PYTHON} -m cleveragents context add file1.py file2.py
... cwd=${TEST_DIR}/project5 timeout=30s
... cwd=${TEST_DIR}/project5 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents context list
... cwd=${TEST_DIR}/project5 timeout=30s
... cwd=${TEST_DIR}/project5 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} file1.py
Should Contain ${result.stdout} file2.py
@@ -88,20 +88,20 @@ Test Context List Files
Test Context Clear
[Documentation] Test clearing context files
Create Directory ${TEST_DIR}/project6
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project6 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project6 timeout=60s
Create File ${TEST_DIR}/project6/test.py # Test file
Run Process ${PYTHON} -m cleveragents context add test.py cwd=${TEST_DIR}/project6 timeout=30s
Run Process ${PYTHON} -m cleveragents context add test.py cwd=${TEST_DIR}/project6 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents context clear --yes
... cwd=${TEST_DIR}/project6 timeout=30s
... cwd=${TEST_DIR}/project6 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Cleared all files from context
Test Plan Creation With Tell
[Documentation] Test creating a plan using tell command
Create Directory ${TEST_DIR}/project7
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project7 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project7 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents tell Add error handling to main function
... cwd=${TEST_DIR}/project7 timeout=30s
... cwd=${TEST_DIR}/project7 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Plan created
Should Contain ${result.stdout} error handling
@@ -109,10 +109,10 @@ Test Plan Creation With Tell
Test Plan Build
[Documentation] Test building a plan
Create Directory ${TEST_DIR}/project8
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project8 timeout=30s
Run Process ${PYTHON} -m cleveragents tell Create example code cwd=${TEST_DIR}/project8 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project8 timeout=60s
Run Process ${PYTHON} -m cleveragents tell Create example code cwd=${TEST_DIR}/project8 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents build cwd=${TEST_DIR}/project8
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Plan built successfully
Should Contain ${result.stdout} Generated
@@ -121,11 +121,11 @@ Test Plan Build
Test Plan Apply
[Documentation] Test applying plan changes
Create Directory ${TEST_DIR}/project9
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project9 timeout=30s
Run Process ${PYTHON} -m cleveragents tell Create example file cwd=${TEST_DIR}/project9 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project9 timeout=60s
Run Process ${PYTHON} -m cleveragents tell Create example file cwd=${TEST_DIR}/project9 timeout=60s
Run Process ${PYTHON} -m cleveragents build cwd=${TEST_DIR}/project9
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
${result} = Run Process ${PYTHON} -m cleveragents apply --yes cwd=${TEST_DIR}/project9 timeout=30s
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents apply --yes cwd=${TEST_DIR}/project9 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Successfully applied
File Should Exist ${TEST_DIR}/project9/example.py
@@ -133,21 +133,21 @@ Test Plan Apply
Test Plan List
[Documentation] Test listing plans
Create Directory ${TEST_DIR}/project10
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project10 timeout=30s
Run Process ${PYTHON} -m cleveragents tell First plan cwd=${TEST_DIR}/project10 timeout=30s
Run Process ${PYTHON} -m cleveragents plan new second-plan cwd=${TEST_DIR}/project10 timeout=30s
${result} = Run Process ${PYTHON} -m cleveragents plan list cwd=${TEST_DIR}/project10 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project10 timeout=60s
Run Process ${PYTHON} -m cleveragents tell First plan cwd=${TEST_DIR}/project10 timeout=60s
Run Process ${PYTHON} -m cleveragents plan new second-plan cwd=${TEST_DIR}/project10 timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents plan list cwd=${TEST_DIR}/project10 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Plans (3 total)
Test Shortcut Commands Work
[Documentation] Test that shortcut commands work properly
Create Directory ${TEST_DIR}/project11
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project11 timeout=30s
Run Process ${PYTHON} -m cleveragents init ${PROJECT_NAME} cwd=${TEST_DIR}/project11 timeout=60s
Create File ${TEST_DIR}/project11/test.txt Test content
# Test context-load shortcut
${result} = Run Process ${PYTHON} -m cleveragents context-load test.txt
... cwd=${TEST_DIR}/project11 timeout=30s
... cwd=${TEST_DIR}/project11 timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Added 1 file(s) to context
@@ -156,30 +156,30 @@ Test End To End Workflow
Create Directory ${TEST_DIR}/project12
# Initialize project
${init} = Run Process ${PYTHON} -m cleveragents init workflow-project
... cwd=${TEST_DIR}/project12 timeout=30s
... cwd=${TEST_DIR}/project12 timeout=60s
Should Be Equal As Numbers ${init.rc} 0
# Add context
Create File ${TEST_DIR}/project12/input.py def main(): pass
${add} = Run Process ${PYTHON} -m cleveragents context add input.py
... cwd=${TEST_DIR}/project12 timeout=30s
... cwd=${TEST_DIR}/project12 timeout=60s
Should Be Equal As Numbers ${add.rc} 0
# Create plan
${tell} = Run Process ${PYTHON} -m cleveragents tell Add logging to main function
... cwd=${TEST_DIR}/project12 timeout=30s
... cwd=${TEST_DIR}/project12 timeout=60s
Should Be Equal As Numbers ${tell.rc} 0
# Build plan
${build} = Run Process ${PYTHON} -m cleveragents build cwd=${TEST_DIR}/project12
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=30s
... env:CLEVERAGENTS_TESTING_USE_MOCK_AI=true timeout=60s
Should Be Equal As Numbers ${build.rc} 0
# Apply changes
${apply} = Run Process ${PYTHON} -m cleveragents apply --yes cwd=${TEST_DIR}/project12 timeout=30s
${apply} = Run Process ${PYTHON} -m cleveragents apply --yes cwd=${TEST_DIR}/project12 timeout=60s
Should Be Equal As Numbers ${apply.rc} 0
# Verify file created
File Should Exist ${TEST_DIR}/project12/example.py
Test Command Error Handling
[Documentation] Test error handling for invalid commands
${result} = Run Process ${PYTHON} -m cleveragents invalid-command timeout=30s
${result} = Run Process ${PYTHON} -m cleveragents invalid-command timeout=60s
Should Not Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stderr} Invalid command
@@ -187,16 +187,16 @@ Test Project Status Without Project
[Documentation] Test project status command without initialized project
Create Directory ${TEST_DIR}/no_project
${result} = Run Process ${PYTHON} -m cleveragents project status
... cwd=${TEST_DIR}/no_project timeout=30s
... cwd=${TEST_DIR}/no_project timeout=60s
Should Not Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stderr} No project found
Test Project Status With Project
[Documentation] Test project status command with initialized project
Create Directory ${TEST_DIR}/with_project
Run Process ${PYTHON} -m cleveragents init status-test cwd=${TEST_DIR}/with_project timeout=30s
Run Process ${PYTHON} -m cleveragents init status-test cwd=${TEST_DIR}/with_project timeout=60s
${result} = Run Process ${PYTHON} -m cleveragents project status
... cwd=${TEST_DIR}/with_project timeout=30s
... cwd=${TEST_DIR}/with_project timeout=60s
Should Be Equal As Numbers ${result.rc} 0
Should Contain ${result.stdout} Project: status-test
Should Contain ${result.stdout} Plans: 1
+1 -1
View File
@@ -755,7 +755,7 @@ Run Python Script
# Write to a temporary file
${temp_file}= Evaluate __import__('tempfile').NamedTemporaryFile(mode='w', suffix='.py', delete=False, dir='/tmp').name
Create File ${temp_file} ${full_code}
${result}= Run Process ${PYTHON} ${temp_file} timeout=10s stderr=STDOUT env:PYTHONWARNINGS=ignore env:PYTHONDONTWRITEBYTECODE=1
${result}= Run Process ${PYTHON} ${temp_file} timeout=60s stderr=STDOUT env:PYTHONWARNINGS=ignore env:PYTHONDONTWRITEBYTECODE=1
Remove File ${temp_file}
# Check if process failed and log stderr if present
IF ${result.rc} != 0