|
8 | 8 | from pytest_mock import MockFixture
|
9 | 9 |
|
10 | 10 | from commitizen import cli
|
| 11 | +from commitizen.defaults import DEFAULT_SETTINGS |
11 | 12 | from commitizen.exceptions import (
|
12 | 13 | ConfigFileNotFound,
|
13 | 14 | ExpectedExit,
|
@@ -239,3 +240,45 @@ def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback(
|
239 | 240 |
|
240 | 241 | # Verify original_excepthook was called with None as traceback
|
241 | 242 | mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)
|
| 243 | + |
| 244 | + |
| 245 | +def test_cli_no_raise_argument(mocker: MockFixture): |
| 246 | + """Test that the no_raise argument properly sets up the excepthook.""" |
| 247 | + # Mock sys.argv to include no_raise argument |
| 248 | + testargs = ["cz", "-nr", "1,2,3", "bump"] |
| 249 | + mocker.patch.object(sys, "argv", testargs) |
| 250 | + |
| 251 | + # Create a proper mock config with required settings |
| 252 | + mock_config = mocker.MagicMock() |
| 253 | + mock_config.settings = DEFAULT_SETTINGS |
| 254 | + mock_config.path = None |
| 255 | + mocker.patch("commitizen.config.read_cfg", return_value=mock_config) |
| 256 | + |
| 257 | + # Mock the command function to avoid actual execution |
| 258 | + mocker.patch("commitizen.commands.Bump") |
| 259 | + dummy_format = mocker.MagicMock() |
| 260 | + dummy_format.template = "dummy_template" |
| 261 | + mocker.patch( |
| 262 | + "commitizen.commands.bump.get_changelog_format", return_value=dummy_format |
| 263 | + ) |
| 264 | + dummy_provider = mocker.MagicMock() |
| 265 | + dummy_provider.get_version.return_value = "0.1.0" |
| 266 | + mocker.patch("commitizen.commands.bump.get_provider", return_value=dummy_provider) |
| 267 | + |
| 268 | + # Mock questionary.confirm to return a mock whose ask() returns True, to bypass interactive prompt in test |
| 269 | + mocker.patch("questionary.confirm", return_value=mocker.MagicMock(ask=lambda: True)) |
| 270 | + |
| 271 | + # Store original excepthook |
| 272 | + original_excepthook = sys.excepthook |
| 273 | + |
| 274 | + try: |
| 275 | + # Run the main function |
| 276 | + cli.main() |
| 277 | + |
| 278 | + # Verify that the excepthook was set to a partial function |
| 279 | + assert isinstance(sys.excepthook, partial) |
| 280 | + assert sys.excepthook.func == cli.commitizen_excepthook |
| 281 | + assert sys.excepthook.keywords.get("no_raise") == [1, 2, 3] |
| 282 | + finally: |
| 283 | + # Restore original excepthook |
| 284 | + sys.excepthook = original_excepthook |
0 commit comments