|
| 1 | +import argparse |
1 | 2 | import os
|
2 | 3 | import subprocess
|
3 | 4 | import sys
|
| 5 | +import types |
4 | 6 | from functools import partial
|
5 | 7 |
|
6 | 8 | import pytest
|
@@ -182,3 +184,90 @@ def test_unknown_args_before_double_dash_raises(mocker: MockFixture):
|
182 | 184 | assert "Invalid commitizen arguments were found before -- separator" in str(
|
183 | 185 | excinfo.value
|
184 | 186 | )
|
| 187 | + |
| 188 | + |
| 189 | +def test_parse_kwargs_non_string_input(): |
| 190 | + """Test that ParseKwargs returns early when given non-string input.""" |
| 191 | + parser = argparse.ArgumentParser() |
| 192 | + namespace = argparse.Namespace() |
| 193 | + action = cli.ParseKwargs(option_strings=["-k"], dest="kwargs") |
| 194 | + |
| 195 | + # Test with None |
| 196 | + action(parser, namespace, None) |
| 197 | + assert not hasattr(namespace, "kwargs") |
| 198 | + |
| 199 | + # Test with list |
| 200 | + action(parser, namespace, ["key=value"]) |
| 201 | + assert not hasattr(namespace, "kwargs") |
| 202 | + |
| 203 | + # Test with dict |
| 204 | + action(parser, namespace, {"key": "value"}) |
| 205 | + assert not hasattr(namespace, "kwargs") |
| 206 | + |
| 207 | + |
| 208 | +def test_parse_kwargs_valid_string_input(): |
| 209 | + """Test that ParseKwargs correctly processes valid string input.""" |
| 210 | + parser = argparse.ArgumentParser() |
| 211 | + namespace = argparse.Namespace() |
| 212 | + action = cli.ParseKwargs(option_strings=["-k"], dest="kwargs") |
| 213 | + |
| 214 | + # Test with valid key=value string |
| 215 | + action(parser, namespace, "key=value") |
| 216 | + assert hasattr(namespace, "kwargs") |
| 217 | + assert namespace.kwargs == {"key": "value"} |
| 218 | + |
| 219 | + |
| 220 | +def test_commitizen_excepthook_non_commitizen_exception(mocker: MockFixture): |
| 221 | + """Test that commitizen_excepthook delegates to original_excepthook for non-CommitizenException.""" |
| 222 | + # Mock the original excepthook |
| 223 | + mock_original_excepthook = mocker.Mock() |
| 224 | + mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook) |
| 225 | + |
| 226 | + # Create a regular exception |
| 227 | + test_exception = ValueError("test error") |
| 228 | + |
| 229 | + # Call commitizen_excepthook with the regular exception |
| 230 | + cli.commitizen_excepthook(ValueError, test_exception, None) |
| 231 | + |
| 232 | + # Verify original_excepthook was called with correct arguments |
| 233 | + mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None) |
| 234 | + |
| 235 | + |
| 236 | +def test_commitizen_excepthook_non_commitizen_exception_with_traceback( |
| 237 | + mocker: MockFixture, |
| 238 | +): |
| 239 | + """Test that commitizen_excepthook handles traceback correctly for non-CommitizenException.""" |
| 240 | + # Mock the original excepthook |
| 241 | + mock_original_excepthook = mocker.Mock() |
| 242 | + mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook) |
| 243 | + |
| 244 | + # Create a regular exception with a traceback |
| 245 | + test_exception = ValueError("test error") |
| 246 | + test_traceback = mocker.Mock(spec=types.TracebackType) |
| 247 | + |
| 248 | + # Call commitizen_excepthook with the regular exception and traceback |
| 249 | + cli.commitizen_excepthook(ValueError, test_exception, test_traceback) |
| 250 | + |
| 251 | + # Verify original_excepthook was called with correct arguments including traceback |
| 252 | + mock_original_excepthook.assert_called_once_with( |
| 253 | + ValueError, test_exception, test_traceback |
| 254 | + ) |
| 255 | + |
| 256 | + |
| 257 | +def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback( |
| 258 | + mocker: MockFixture, |
| 259 | +): |
| 260 | + """Test that commitizen_excepthook handles invalid traceback correctly for non-CommitizenException.""" |
| 261 | + # Mock the original excepthook |
| 262 | + mock_original_excepthook = mocker.Mock() |
| 263 | + mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook) |
| 264 | + |
| 265 | + # Create a regular exception with an invalid traceback |
| 266 | + test_exception = ValueError("test error") |
| 267 | + test_traceback = mocker.Mock() # Not a TracebackType |
| 268 | + |
| 269 | + # Call commitizen_excepthook with the regular exception and invalid traceback |
| 270 | + cli.commitizen_excepthook(ValueError, test_exception, test_traceback) |
| 271 | + |
| 272 | + # Verify original_excepthook was called with None as traceback |
| 273 | + mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None) |
0 commit comments