|
10 | 10 | import pytest |
11 | 11 |
|
12 | 12 | import git_commits2slack |
| 13 | +from lib.exceptions import SlackTokenError |
13 | 14 |
|
14 | 15 |
|
15 | 16 | @pytest.fixture |
@@ -389,5 +390,115 @@ def test_main_ideal( |
389 | 390 | assert data == expected_slack_requests[0] |
390 | 391 |
|
391 | 392 |
|
392 | | -# FIXME: SlackTokenError |
393 | | -# FIXME: random exception |
| 393 | +@pytest.mark.parametrize( |
| 394 | + "extra_args,expected_retcode", |
| 395 | + [ |
| 396 | + ([], 0), |
| 397 | + (["--return-error"], 1), |
| 398 | + ], |
| 399 | +) |
| 400 | +@patch("git_commits2slack.rss2slack.get_slack_token") |
| 401 | +def test_main_slack_token_error( |
| 402 | + mock_get_slack_token, |
| 403 | + extra_args, |
| 404 | + expected_retcode, |
| 405 | + fixture_git_dir, |
| 406 | + caplog, |
| 407 | +): |
| 408 | + """Test that SlackTokenError is handled as expected.""" |
| 409 | + expected_log_records = [ |
| 410 | + ( |
| 411 | + "git-commits2slack", |
| 412 | + 40, |
| 413 | + "Environment variable SLACK_TOKEN must be set.", |
| 414 | + ), |
| 415 | + ] |
| 416 | + expected_slack_channel = "test" |
| 417 | + expected_slack_url = "https://slack.example.com" |
| 418 | + |
| 419 | + mock_get_slack_token.side_effect = SlackTokenError("pytest") |
| 420 | + |
| 421 | + exception = None |
| 422 | + args = [ |
| 423 | + "./git_commits2slack.py", |
| 424 | + "--git-clone-dir", |
| 425 | + fixture_git_dir, |
| 426 | + "--git-repository", |
| 427 | + "test", |
| 428 | + "--git-web", |
| 429 | + "http://example.com", |
| 430 | + "--slack-base-url", |
| 431 | + expected_slack_url, |
| 432 | + "--slack-channel", |
| 433 | + expected_slack_channel, |
| 434 | + "--slack-timeout", |
| 435 | + "10", |
| 436 | + "-v", |
| 437 | + ] + extra_args |
| 438 | + |
| 439 | + with patch.object(sys, "argv", args): |
| 440 | + try: |
| 441 | + git_commits2slack.main() |
| 442 | + except SystemExit as sys_exit: |
| 443 | + exception = sys_exit |
| 444 | + |
| 445 | + assert isinstance(exception, SystemExit) is True |
| 446 | + assert exception.code == expected_retcode |
| 447 | + assert caplog.record_tuples == expected_log_records |
| 448 | + |
| 449 | + |
| 450 | +@pytest.mark.parametrize( |
| 451 | + "extra_args,expected_retcode", |
| 452 | + [ |
| 453 | + ([], 0), |
| 454 | + (["--return-error"], 1), |
| 455 | + ], |
| 456 | +) |
| 457 | +@patch("git_commits2slack.rss2slack.get_slack_token") |
| 458 | +def test_main_random_exception( |
| 459 | + mock_get_slack_token, |
| 460 | + extra_args, |
| 461 | + expected_retcode, |
| 462 | + fixture_git_dir, |
| 463 | + caplog, |
| 464 | +): |
| 465 | + """Test that unexpected exception is handled as expected.""" |
| 466 | + expected_log_records = [ |
| 467 | + ( |
| 468 | + "git-commits2slack", |
| 469 | + 40, |
| 470 | + "Unexpected exception has occurred.", |
| 471 | + ), |
| 472 | + ] |
| 473 | + expected_slack_channel = "test" |
| 474 | + expected_slack_url = "https://slack.example.com" |
| 475 | + |
| 476 | + mock_get_slack_token.side_effect = ValueError("pytest") |
| 477 | + |
| 478 | + exception = None |
| 479 | + args = [ |
| 480 | + "./git_commits2slack.py", |
| 481 | + "--git-clone-dir", |
| 482 | + fixture_git_dir, |
| 483 | + "--git-repository", |
| 484 | + "test", |
| 485 | + "--git-web", |
| 486 | + "http://example.com", |
| 487 | + "--slack-base-url", |
| 488 | + expected_slack_url, |
| 489 | + "--slack-channel", |
| 490 | + expected_slack_channel, |
| 491 | + "--slack-timeout", |
| 492 | + "10", |
| 493 | + "-v", |
| 494 | + ] + extra_args |
| 495 | + |
| 496 | + with patch.object(sys, "argv", args): |
| 497 | + try: |
| 498 | + git_commits2slack.main() |
| 499 | + except SystemExit as sys_exit: |
| 500 | + exception = sys_exit |
| 501 | + |
| 502 | + assert isinstance(exception, SystemExit) is True |
| 503 | + assert exception.code == expected_retcode |
| 504 | + assert caplog.record_tuples == expected_log_records |
0 commit comments