7
7
from time import time , sleep
8
8
from rich .console import Console # import Rich's Console to enable recording
9
9
import re
10
+ import io
10
11
11
12
# Use the package logger consistently
12
13
logger = get_logger (__name__ )
@@ -24,59 +25,36 @@ def setup_config():
24
25
25
26
runner = CliRunner ()
26
27
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 = []
49
32
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 )
61
39
62
40
def strip_ansi (text : str ) -> str :
63
41
# Regular expression for ANSI escape sequences
64
42
ansi_escape = re .compile (r'\x1B\[[0-?]*[ -/]*[@-~]' )
65
43
return ansi_escape .sub ('' , text )
66
44
67
- def test_cli_analyze (sample_audio_path , patch_console ):
45
+ def test_cli_analyze (sample_audio_path ):
68
46
"""Test CLI analyze command"""
69
47
logger .info ("Testing CLI analyze command" )
70
48
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
71
53
result = runner .invoke (
72
54
app , ["analyze" , str (sample_audio_path )], catch_exceptions = False
73
55
)
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 ()
80
58
81
59
logger .debug ("CLI analyze command rendered output: {}" , final_output )
82
60
logger .debug ("CLI analyze command exit code: {}" , result .exit_code )
@@ -97,9 +75,13 @@ def test_cli_analyze(sample_audio_path, patch_console):
97
75
assert "0.85" in final_output_clean # Guitar
98
76
assert "0.90" in final_output_clean # Drums
99
77
100
- def test_cli_process (sample_audio_path , tmp_path , patch_console ):
78
+ def test_cli_process (sample_audio_path , tmp_path ):
101
79
"""Test CLI process command"""
102
80
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
103
85
result = runner .invoke (
104
86
app , [
105
87
"process" ,
@@ -109,8 +91,8 @@ def test_cli_process(sample_audio_path, tmp_path, patch_console):
109
91
],
110
92
catch_exceptions = False
111
93
)
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 ( )
114
96
115
97
logger .debug ("CLI process command rendered output: {}" , final_output )
116
98
logger .debug ("CLI process command exit code: {}" , result .exit_code )
0 commit comments