Skip to content

Commit f2fbd19

Browse files
committed
Refactor
1 parent db6c098 commit f2fbd19

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

tests/test_cli.py

+25-43
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from time import time, sleep
88
from rich.console import Console # import Rich's Console to enable recording
99
import re
10+
import io
1011

1112
# Use the package logger consistently
1213
logger = get_logger(__name__)
@@ -24,59 +25,36 @@ def setup_config():
2425

2526
runner = CliRunner()
2627

27-
def wait_for_output_stabilize(get_output, timeout=2.0, poll_interval=0.1):
28-
"""
29-
Wait for output from a callable to stabilize.
30-
31-
Args:
32-
get_output: A callable returning the current output string
33-
timeout: Maximum time to wait
34-
poll_interval: Time between polls
35-
36-
Returns:
37-
The stabilized output string.
38-
"""
39-
start_time = time()
40-
last_output = get_output()
41-
while time() - start_time < timeout:
42-
sleep(poll_interval)
43-
current_output = get_output()
44-
if current_output == last_output:
45-
return current_output
46-
last_output = current_output
47-
logger.warning("Output did not stabilize within timeout period")
48-
return last_output
28+
class AccumulatingStream(io.StringIO):
29+
def __init__(self, *args, **kwargs):
30+
super().__init__(*args, **kwargs)
31+
self.accumulated = []
4932

50-
# Automatically patch the cli's Console for testing
51-
@pytest.fixture(autouse=True)
52-
def patch_console(monkeypatch):
53-
"""
54-
Monkeypatch the global console in audiokit.cli to use a recording console.
55-
"""
56-
import audiokit.cli as cli # import the module that created the console
57-
# Create a Console that forces terminal behavior and records all output:
58-
recording_console = Console(force_terminal=True, record=True)
59-
monkeypatch.setattr(cli, "console", recording_console)
60-
return recording_console
33+
def write(self, s):
34+
self.accumulated.append(s)
35+
return super().write(s)
36+
37+
def get_accumulated(self):
38+
return ''.join(self.accumulated)
6139

6240
def strip_ansi(text: str) -> str:
6341
# Regular expression for ANSI escape sequences
6442
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
6543
return ansi_escape.sub('', text)
6644

67-
def test_cli_analyze(sample_audio_path, patch_console):
45+
def test_cli_analyze(sample_audio_path):
6846
"""Test CLI analyze command"""
6947
logger.info("Testing CLI analyze command")
7048

49+
# Create an accumulating stream and override the runner's output stream method.
50+
accum_stream = AccumulatingStream()
51+
original_method = runner._get_output_stream
52+
runner._get_output_stream = lambda: accum_stream
7153
result = runner.invoke(
7254
app, ["analyze", str(sample_audio_path)], catch_exceptions=False
7355
)
74-
75-
# Wait for the recorded console output (from the patched Rich Console) to stabilize.
76-
final_output = wait_for_output_stabilize(lambda: patch_console.export_text(clear=False))
77-
# Fallback to using the captured output from runner.invoke if nothing is recorded.
78-
if not final_output:
79-
final_output = result.stdout
56+
runner._get_output_stream = original_method
57+
final_output = accum_stream.get_accumulated()
8058

8159
logger.debug("CLI analyze command rendered output: {}", final_output)
8260
logger.debug("CLI analyze command exit code: {}", result.exit_code)
@@ -97,9 +75,13 @@ def test_cli_analyze(sample_audio_path, patch_console):
9775
assert "0.85" in final_output_clean # Guitar
9876
assert "0.90" in final_output_clean # Drums
9977

100-
def test_cli_process(sample_audio_path, tmp_path, patch_console):
78+
def test_cli_process(sample_audio_path, tmp_path):
10179
"""Test CLI process command"""
10280
logger.info("Testing CLI process command")
81+
# Create an accumulating stream and override the runner's output stream method.
82+
accum_stream = AccumulatingStream()
83+
original_method = runner._get_output_stream
84+
runner._get_output_stream = lambda: accum_stream
10385
result = runner.invoke(
10486
app, [
10587
"process",
@@ -109,8 +91,8 @@ def test_cli_process(sample_audio_path, tmp_path, patch_console):
10991
],
11092
catch_exceptions=False
11193
)
112-
113-
final_output = wait_for_output_stabilize(lambda: patch_console.export_text(clear=False))
94+
runner._get_output_stream = original_method
95+
final_output = accum_stream.get_accumulated()
11496

11597
logger.debug("CLI process command rendered output: {}", final_output)
11698
logger.debug("CLI process command exit code: {}", result.exit_code)

0 commit comments

Comments
 (0)