|
1 | | -# import pytest |
2 | | -# import os |
3 | | -# from unittest.mock import patch, mock_open, MagicMock |
4 | | -# from typer.testing import CliRunner |
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from unittest.mock import patch, mock_open, MagicMock |
| 4 | +from typer.testing import CliRunner |
5 | 5 |
|
6 | | -# # Assuming cli.main is the entry point for typer app |
7 | | -# # We need to adjust imports based on actual structure if main.py is elsewhere |
8 | | -# # Let's assume main.py exists and imports version_command correctly |
9 | | -# # We will test the command function directly for simplicity here, |
10 | | -# # avoiding the need for a full typer app setup in this unit test. |
11 | | -# from cli.commands.version import version_command |
| 6 | +# Assuming cli.main is the entry point for typer app |
| 7 | +# We need to adjust imports based on actual structure if main.py is elsewhere |
| 8 | +# Let's assume main.py exists and imports version_command correctly |
| 9 | +# We will test the command function directly for simplicity here, |
| 10 | +# avoiding the need for a full typer app setup in this unit test. |
| 11 | +from cli.commands.version import version_command |
12 | 12 |
|
13 | | -# # Dummy translation messages |
14 | | -# DUMMY_MESSAGES = { |
15 | | -# "version_info": "SpiceCode Version:", |
16 | | -# "version_not_found": "Version information not found in setup.py", |
17 | | -# "setup_not_found": "Error: setup.py not found.", |
18 | | -# "error": "Error:", |
19 | | -# } |
| 13 | +# Dummy translation messages |
| 14 | +DUMMY_MESSAGES = { |
| 15 | + "version_info": "SpiceCode Version:", |
| 16 | + "version_not_found": "Version information not found in setup.py", |
| 17 | + "setup_not_found": "Error: setup.py not found.", |
| 18 | + "error": "Error:", |
| 19 | +} |
20 | 20 |
|
21 | | -# # Mock CURRENT_DIR (assuming it's the 'cli' directory for the command) |
22 | | -# TEST_CURRENT_DIR = "/home/ubuntu/spicecode/cli" |
23 | | -# EXPECTED_SETUP_PATH = "/home/ubuntu/spicecode/setup.py" |
| 21 | +# Mock CURRENT_DIR (assuming it's the 'cli' directory for the command) |
| 22 | +TEST_CURRENT_DIR = "/home/ubuntu/spicecode/cli" |
| 23 | +EXPECTED_SETUP_PATH = "/home/ubuntu/spicecode/setup.py" |
24 | 24 |
|
25 | | -# @patch("cli.commands.version.get_translation") |
26 | | -# @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): |
29 | | -# """Test version command when setup.py exists and contains version.""" |
30 | | -# mock_get_translation.return_value = DUMMY_MESSAGES |
31 | | -# mock_exists.return_value = True |
32 | | -# mock_file_open.read_data = "version=\"1.2.3\",\n" # Simulate setup.py content |
33 | | -# mock_file_open.return_value.read.return_value = mock_file_open.read_data |
34 | | -# mock_file_open.return_value.__iter__.return_value = mock_file_open.read_data.splitlines() |
| 25 | +@patch("cli.commands.version.get_translation") |
| 26 | +@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): |
| 29 | + """Test version command when setup.py exists and contains version.""" |
| 30 | + mock_get_translation.return_value = DUMMY_MESSAGES |
| 31 | + mock_exists.return_value = True |
| 32 | + mock_file_open.read_data = "version=\"1.2.3\",\n" # Simulate setup.py content |
| 33 | + mock_file_open.return_value.read.return_value = mock_file_open.read_data |
| 34 | + mock_file_open.return_value.__iter__.return_value = mock_file_open.read_data.splitlines() |
35 | 35 |
|
36 | | -# version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
| 36 | + version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
37 | 37 |
|
38 | | -# captured = capsys.readouterr() |
| 38 | + captured = capsys.readouterr() |
39 | 39 |
|
40 | | -# mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
41 | | -# mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
42 | | -# assert "SpiceCode Version: 1.2.3" in captured.out |
| 40 | + mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
| 41 | + mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
| 42 | + assert "SpiceCode Version: 1.2.3" in captured.out |
43 | 43 |
|
44 | | -# @patch("cli.commands.version.get_translation") |
45 | | -# @patch("os.path.exists") |
46 | | -# @patch("builtins.open", new_callable=mock_open) |
47 | | -# def test_version_command_version_not_in_setup(mock_file_open, mock_exists, mock_get_translation, capsys): |
48 | | -# """Test version command when setup.py exists but lacks version info.""" |
49 | | -# mock_get_translation.return_value = DUMMY_MESSAGES |
50 | | -# mock_exists.return_value = True |
51 | | -# mock_file_open.read_data = "name=\"spicecode\"\n" # Simulate setup.py without version |
52 | | -# mock_file_open.return_value.read.return_value = mock_file_open.read_data |
53 | | -# mock_file_open.return_value.__iter__.return_value = mock_file_open.read_data.splitlines() |
| 44 | +@patch("cli.commands.version.get_translation") |
| 45 | +@patch("os.path.exists") |
| 46 | +@patch("builtins.open", new_callable=mock_open) |
| 47 | +def test_version_command_version_not_in_setup(mock_file_open, mock_exists, mock_get_translation, capsys): |
| 48 | + """Test version command when setup.py exists but lacks version info.""" |
| 49 | + mock_get_translation.return_value = DUMMY_MESSAGES |
| 50 | + mock_exists.return_value = True |
| 51 | + mock_file_open.read_data = "name=\"spicecode\"\n" # Simulate setup.py without version |
| 52 | + mock_file_open.return_value.read.return_value = mock_file_open.read_data |
| 53 | + mock_file_open.return_value.__iter__.return_value = mock_file_open.read_data.splitlines() |
54 | 54 |
|
55 | | -# version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
| 55 | + version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
56 | 56 |
|
57 | | -# captured = capsys.readouterr() |
| 57 | + captured = capsys.readouterr() |
58 | 58 |
|
59 | | -# mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
60 | | -# mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
61 | | -# assert "Version information not found in setup.py" in captured.out |
| 59 | + mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
| 60 | + mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
| 61 | + assert "Version information not found in setup.py" in captured.out |
62 | 62 |
|
63 | | -# @patch("cli.commands.version.get_translation") |
64 | | -# @patch("os.path.exists") |
65 | | -# def test_version_command_setup_not_found(mock_exists, mock_get_translation, capsys): |
66 | | -# """Test version command when setup.py does not exist.""" |
67 | | -# mock_get_translation.return_value = DUMMY_MESSAGES |
68 | | -# mock_exists.return_value = False |
| 63 | +@patch("cli.commands.version.get_translation") |
| 64 | +@patch("os.path.exists") |
| 65 | +def test_version_command_setup_not_found(mock_exists, mock_get_translation, capsys): |
| 66 | + """Test version command when setup.py does not exist.""" |
| 67 | + mock_get_translation.return_value = DUMMY_MESSAGES |
| 68 | + mock_exists.return_value = False |
69 | 69 |
|
70 | | -# version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
| 70 | + version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
71 | 71 |
|
72 | | -# captured = capsys.readouterr() |
| 72 | + captured = capsys.readouterr() |
73 | 73 |
|
74 | | -# mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
75 | | -# assert "Error: setup.py not found." in captured.out |
| 74 | + mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
| 75 | + assert "Error: setup.py not found." in captured.out |
76 | 76 |
|
77 | | -# @patch("cli.commands.version.get_translation") |
78 | | -# @patch("os.path.exists") |
79 | | -# @patch("builtins.open", side_effect=OSError("Permission denied")) |
80 | | -# def test_version_command_read_error(mock_file_open, mock_exists, mock_get_translation, capsys): |
81 | | -# """Test version command handles exceptions during file reading.""" |
82 | | -# mock_get_translation.return_value = DUMMY_MESSAGES |
83 | | -# mock_exists.return_value = True |
| 77 | +@patch("cli.commands.version.get_translation") |
| 78 | +@patch("os.path.exists") |
| 79 | +@patch("builtins.open", side_effect=OSError("Permission denied")) |
| 80 | +def test_version_command_read_error(mock_file_open, mock_exists, mock_get_translation, capsys): |
| 81 | + """Test version command handles exceptions during file reading.""" |
| 82 | + mock_get_translation.return_value = DUMMY_MESSAGES |
| 83 | + mock_exists.return_value = True |
84 | 84 |
|
85 | | -# version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
| 85 | + version_command(LANG_FILE="dummy_lang.txt", CURRENT_DIR=TEST_CURRENT_DIR) |
86 | 86 |
|
87 | | -# captured = capsys.readouterr() |
| 87 | + captured = capsys.readouterr() |
88 | 88 |
|
89 | | -# mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
90 | | -# mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
91 | | -# assert "Error: Permission denied" in captured.out |
| 89 | + mock_exists.assert_called_once_with(EXPECTED_SETUP_PATH) |
| 90 | + mock_file_open.assert_called_once_with(EXPECTED_SETUP_PATH, "r") |
| 91 | + assert "Error: Permission denied" in captured.out |
92 | 92 |
|
93 | 93 |
|
0 commit comments