Skip to content

Commit f3a4c77

Browse files
committed
Strip ANSI codes in CLI help test assertions.
Typer still emits styled help on CI even with --no-color, so compare plain text when matching option names and placeholders.
1 parent cd1964d commit f3a4c77

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

tests/api/unit_tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
Fixtures:
66
mock_object: A pytest fixture that provides a mock object for the class instantiation."""
77

8+
import re
89
from unittest.mock import Mock
910
import pytest
11+
12+
_ANSI_ESCAPE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
13+
14+
15+
def strip_ansi(text: str) -> str:
16+
"""Remove ANSI escape sequences from CLI output."""
17+
return _ANSI_ESCAPE.sub("", text)
1018
from moldflow.constants import COLOR_BAND_RANGE
1119
from tests.api.unit_tests.mock_container import MockContainer
1220

tests/api/unit_tests/test_cli_basics.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import moldflow
1313

1414
from moldflow_cli.commands import build_cli_app
15+
from tests.api.unit_tests.conftest import strip_ansi
1516

1617
runner = CliRunner()
1718

@@ -32,17 +33,18 @@ def test_help_runs_without_synergy(self):
3233
# Help should not need to touch Synergy/COM at all.
3334
mock_ctx_synergy.assert_not_called()
3435
mock_fact_synergy.assert_not_called()
35-
assert "Moldflow command-line interface" in result.stdout
36-
assert "Start with 'list' to discover targets" in result.stdout
37-
assert "describe <target>" in result.stdout
38-
assert "invoke <target>" in result.stdout
36+
output = strip_ansi(result.stdout)
37+
assert "Moldflow command-line interface" in output
38+
assert "Start with 'list' to discover targets" in output
39+
assert "describe <target>" in output
40+
assert "invoke <target>" in output
3941

4042
def test_help_shows_global_no_color_option(self):
4143
"""Global help should advertise the output-styling toggle."""
4244
app = build_cli_app()
4345
result = runner.invoke(app, ["--no-color", "--help"])
4446
assert result.exit_code == 0
45-
assert "--no-color" in result.stdout
47+
assert "--no-color" in strip_ansi(result.stdout)
4648

4749
def test_subcommand_help_runs_without_synergy(self):
4850
"""Subcommand help should be available without creating Synergy/COM objects."""
@@ -59,17 +61,20 @@ def test_subcommand_help_runs_without_synergy(self):
5961
assert invoke_help.exit_code == 0
6062
mock_ctx_synergy.assert_not_called()
6163
mock_fact_synergy.assert_not_called()
64+
list_output = strip_ansi(list_help.stdout)
65+
describe_output = strip_ansi(describe_help.stdout)
66+
invoke_output = strip_ansi(invoke_help.stdout)
6267
assert (
6368
"Discover invokable targets and the next command to run for each one"
64-
in list_help.stdout
69+
in list_output
6570
)
66-
assert "--json" in list_help.stdout
67-
assert "--with-describe" in list_help.stdout
71+
assert "--json" in list_output
72+
assert "--with-describe" in list_output
6873
assert (
6974
"Inspect a target's signature, docs, examples, and structured invoke template"
70-
in describe_help.stdout
75+
in describe_output
7176
)
72-
assert "Run a Moldflow target with named parameters or JSON input" in invoke_help.stdout
77+
assert "Run a Moldflow target with named parameters or JSON input" in invoke_output
7378

7479
def test_global_no_color_option_configures_cli_output(self):
7580
"""Global --no-color should reconfigure shared console output before dispatch."""

tests/api/unit_tests/test_cli_invoke_parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from moldflow_cli.commands import build_cli_app
2121
from moldflow_cli.invoke_binding import _bucket_items_by_step, _coerce_final_value
22+
from tests.api.unit_tests.conftest import strip_ansi
2223

2324
runner = CliRunner()
2425

@@ -1857,7 +1858,7 @@ def test_invoke_help_shows_arg_syntax():
18571858
"""Invoke --help should demonstrate the step.param.attr=value routing syntax."""
18581859
app = build_cli_app()
18591860
result = runner.invoke(app, ["--no-color", "invoke", "--help"])
1860-
help_text = result.stdout.lower()
1861+
help_text = strip_ansi(result.stdout).lower()
18611862
assert result.exit_code == 0
18621863
assert "find_plot_by_name.plot_name" in help_text or "step.param.attr" in help_text
18631864
assert "duplicate/conflicting paths" in help_text

0 commit comments

Comments
 (0)