|
1 | 1 | import os
|
2 |
| -from unittest.mock import patch |
| 2 | +from unittest.mock import patch, mock_open |
3 | 3 | from code_mage.loadConfig import load_config # Adjust the import path accordingly
|
4 | 4 |
|
5 | 5 |
|
6 | 6 | class TestLoadConfig:
|
7 | 7 | @patch("os.path.exists")
|
8 | 8 | @patch("toml.load")
|
9 |
| - def test_load_config_file_exists(self, mock_toml_load, mock_os_exists): |
10 |
| - # Simulate that the file exists |
| 9 | + @patch("builtins.open", new_callable=mock_open) |
| 10 | + def test_load_config_file_exists(self, mock_file, mock_toml_load, mock_os_exists): |
| 11 | + # Simulate that the config file exists |
11 | 12 | mock_os_exists.return_value = True
|
12 | 13 |
|
13 |
| - # Simulate the contents of the file |
14 |
| - mock_toml_load.return_value = { |
15 |
| - "language": "Python", |
16 |
| - "OPENROUTER_API_KEY": "mock_openrouter_api_key", |
17 |
| - } |
| 14 | + # Simulate the contents of the config file |
| 15 | + mock_toml_load.side_effect = [ |
| 16 | + { |
| 17 | + "language": "Python", |
| 18 | + "OPENROUTER_API_KEY": "mock_openrouter_api_key", |
| 19 | + }, # First call (config file) |
| 20 | + {"tool": {"poetry": {"version": "0.9.0"}}}, # Second call (pyproject.toml) |
| 21 | + ] |
18 | 22 |
|
19 | 23 | # Call load_config function
|
20 | 24 | config = load_config()
|
21 | 25 |
|
22 | 26 | # Assertions
|
23 | 27 | mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml"))
|
24 |
| - mock_toml_load.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml")) |
25 |
| - assert config == {"language": "Python", "OPENROUTER_API_KEY": "mock_openrouter_api_key"} |
| 28 | + mock_file.assert_called_once_with("pyproject.toml", "r") |
| 29 | + assert config == { |
| 30 | + "language": "Python", |
| 31 | + "OPENROUTER_API_KEY": "mock_openrouter_api_key", |
| 32 | + "version": "0.9.0", |
| 33 | + } |
26 | 34 |
|
27 | 35 | @patch("os.path.exists")
|
28 |
| - def test_load_config_file_not_exists(self, mock_os_exists): |
| 36 | + @patch("toml.load") |
| 37 | + @patch("builtins.open", new_callable=mock_open) |
| 38 | + def test_load_config_file_with_no_content(self, mock_file, mock_toml_load, mock_os_exists): |
29 | 39 | # Simulate that the file does not exist
|
| 40 | + mock_os_exists.return_value = True |
| 41 | + |
| 42 | + # Call the function under test |
| 43 | + |
| 44 | + mock_toml_load.side_effect = [ |
| 45 | + {}, |
| 46 | + {"tool": {"poetry": {"version": "0.9.0"}}}, # Second call (pyproject.toml) |
| 47 | + ] |
| 48 | + |
| 49 | + config = load_config() |
| 50 | + |
| 51 | + # Assertions |
| 52 | + mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml")) |
| 53 | + mock_file.assert_called_once_with("pyproject.toml", "r") |
| 54 | + |
| 55 | + # Assert the default config with version added |
| 56 | + assert config == {"version": "0.9.0"} |
| 57 | + |
| 58 | + @patch("os.path.exists") # Third argument |
| 59 | + @patch("builtins.open") # Second argument |
| 60 | + @patch("toml.load") # First argument |
| 61 | + def test_load_config_file_not_exists(self, mock_toml_load, mock_file, mock_os_exists): |
| 62 | + # Simulate that the config file does not exist |
30 | 63 | mock_os_exists.return_value = False
|
31 | 64 |
|
| 65 | + # Simulate the content of the pyproject.toml file |
| 66 | + mock_toml_load.return_value = {"tool": {"poetry": {"version": "0.9.0"}}} |
| 67 | + |
32 | 68 | # Call the function under test
|
33 | 69 | config = load_config()
|
34 | 70 |
|
35 | 71 | # Assertions
|
36 | 72 | mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml"))
|
37 |
| - assert config == {} |
| 73 | + mock_file.assert_called_once_with("pyproject.toml", "r") |
| 74 | + assert config == {"version": "0.9.0"} |
0 commit comments