2424
2525@patch ("cli.commands.version.get_translation" )
2626@patch ("os.path.exists" )
27- @patch ("builtins.open" , new_callable = mock_open )
28- def test_version_command_success (mock_file_open , mock_exists , mock_get_translation , capsys ):
27+ def test_version_command_success (mock_exists , mock_get_translation , capsys ):
2928 """Test version command when setup.py exists and contains version."""
3029 mock_get_translation .return_value = DUMMY_MESSAGES
3130 mock_exists .return_value = True
3231
33- # Setup mock file content - each line should end with \n for proper line iteration
34- file_lines = [
35- 'name="spicecode",\n ' ,
36- 'version="1.2.3",\n ' ,
37- 'author="test"\n '
38- ]
39- mock_file_open .return_value .__iter__ .return_value = iter (file_lines )
40-
41- version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
42-
43- captured = capsys .readouterr ()
32+ # Create file content with version line
33+ file_content = 'name="spicecode",\n version="1.2.3",\n author="test"\n '
4434
45- mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
46- mock_file_open .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
47- assert "SpiceCode Version: 1.2.3" in captured .out
35+ # Use mock_open with read_data parameter
36+ with patch ("builtins.open" , mock_open (read_data = file_content )) as mock_file :
37+ version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
38+
39+ captured = capsys .readouterr ()
40+
41+ mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
42+ mock_file .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
43+ assert "SpiceCode Version: 1.2.3" in captured .out
4844
4945@patch ("cli.commands.version.get_translation" )
5046@patch ("os.path.exists" )
51- @patch ("builtins.open" , new_callable = mock_open )
52- def test_version_command_version_not_in_setup (mock_file_open , mock_exists , mock_get_translation , capsys ):
47+ def test_version_command_version_not_in_setup (mock_exists , mock_get_translation , capsys ):
5348 """Test version command when setup.py exists but lacks version info."""
5449 mock_get_translation .return_value = DUMMY_MESSAGES
5550 mock_exists .return_value = True
5651
57- # Setup mock file content without version line
58- file_lines = [
59- 'name="spicecode",\n ' ,
60- 'author="test",\n ' ,
61- 'description="A CLI tool"\n '
62- ]
63- mock_file_open .return_value .__iter__ .return_value = iter (file_lines )
64-
65- version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
52+ # Create file content without version line
53+ file_content = 'name="spicecode",\n author="test",\n description="A CLI tool"\n '
6654
67- captured = capsys .readouterr ()
68-
69- mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
70- mock_file_open .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
71- assert "Version information not found in setup.py" in captured .out
55+ with patch ("builtins.open" , mock_open (read_data = file_content )) as mock_file :
56+ version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
57+
58+ captured = capsys .readouterr ()
59+
60+ mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
61+ mock_file .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
62+ assert "Version information not found in setup.py" in captured .out
7263
7364@patch ("cli.commands.version.get_translation" )
7465@patch ("os.path.exists" )
@@ -86,16 +77,16 @@ def test_version_command_setup_not_found(mock_exists, mock_get_translation, caps
8677
8778@patch ("cli.commands.version.get_translation" )
8879@patch ("os.path.exists" )
89- @patch ("builtins.open" , side_effect = OSError ("Permission denied" ))
90- def test_version_command_read_error (mock_file_open , mock_exists , mock_get_translation , capsys ):
80+ def test_version_command_read_error (mock_exists , mock_get_translation , capsys ):
9181 """Test version command handles exceptions during file reading."""
9282 mock_get_translation .return_value = DUMMY_MESSAGES
9383 mock_exists .return_value = True
9484
95- version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
96-
97- captured = capsys .readouterr ()
98-
99- mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
100- mock_file_open .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
101- assert "Error: Permission denied" in captured .out
85+ with patch ("builtins.open" , side_effect = OSError ("Permission denied" )) as mock_file :
86+ version_command (LANG_FILE = "dummy_lang.txt" , CURRENT_DIR = TEST_CURRENT_DIR )
87+
88+ captured = capsys .readouterr ()
89+
90+ mock_exists .assert_called_once_with (EXPECTED_SETUP_PATH )
91+ mock_file .assert_called_once_with (EXPECTED_SETUP_PATH , "r" )
92+ assert "Error: Permission denied" in captured .out
0 commit comments