3
3
from audio_separator .utils .cli import main
4
4
import subprocess
5
5
from unittest import mock
6
- from unittest .mock import patch , MagicMock
6
+ from unittest .mock import patch , MagicMock , mock_open
7
7
8
8
9
9
# Common fixture for expected arguments
@@ -51,16 +51,33 @@ def test_cli_no_args(capsys):
51
51
52
52
53
53
# Test with multiple filename arguments
54
- def test_cli_multiple_filenames (capsys ):
54
+ def test_cli_multiple_filenames ():
55
55
test_args = ["cli.py" , "test1.mp3" , "test2.mp3" ]
56
- with patch ("sys.argv" , test_args ):
57
- # Expecting the application to raise a SystemExit due to unrecognized arguments
58
- with pytest .raises (SystemExit ):
59
- main ()
60
- captured = capsys .readouterr ()
61
56
62
- # Check if the correct error message is displayed
63
- assert "unrecognized arguments" in captured .err
57
+ # Mock the open function to prevent actual file operations
58
+ mock_file = mock_open ()
59
+
60
+ # Create a mock logger
61
+ mock_logger = MagicMock ()
62
+
63
+ # Patch multiple functions to prevent actual file operations and separations
64
+ with patch ("sys.argv" , test_args ), patch ("builtins.open" , mock_file ), patch ("audio_separator.separator.Separator.separate" ) as mock_separate , patch (
65
+ "audio_separator.separator.Separator.load_model"
66
+ ), patch ("logging.getLogger" , return_value = mock_logger ):
67
+
68
+ # Mock the separate method to return some dummy output
69
+ mock_separate .return_value = ["output_file1.mp3" , "output_file2.mp3" ]
70
+
71
+ # Call the main function
72
+ main ()
73
+
74
+ # Check if separate was called twice (once for each input file)
75
+ assert mock_separate .call_count == 2
76
+
77
+ # Check if the logger captured information about both files
78
+ log_messages = [call [0 ][0 ] for call in mock_logger .info .call_args_list ]
79
+ assert any ("test1.mp3" in msg and "test2.mp3" in msg for msg in log_messages )
80
+ assert any ("Separation complete" in msg for msg in log_messages )
64
81
65
82
66
83
# Test the CLI with a specific audio file
@@ -156,6 +173,7 @@ def test_cli_normalization_threshold_argument(common_expected_args):
156
173
# Assertions
157
174
mock_separator .assert_called_once_with (** expected_args )
158
175
176
+
159
177
# Test using normalization_threshold argument
160
178
def test_cli_amplification_threshold_argument (common_expected_args ):
161
179
test_args = ["cli.py" , "test_audio.mp3" , "--amplification=0.75" ]
@@ -172,6 +190,7 @@ def test_cli_amplification_threshold_argument(common_expected_args):
172
190
# Assertions
173
191
mock_separator .assert_called_once_with (** expected_args )
174
192
193
+
175
194
# Test using single stem argument
176
195
def test_cli_single_stem_argument (common_expected_args ):
177
196
test_args = ["cli.py" , "test_audio.mp3" , "--single_stem=instrumental" ]
0 commit comments