diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-17 12:28:45 -0800 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-17 12:28:45 -0800 |
| commit | d2c6df9248d03a692bf51c5367ac6f6de6c46ad1 (patch) | |
| tree | 89998204a33d896cff4e128dfe6fdd252b7f4821 /tests | |
| parent | 0c93e4a19e0883d279e799c28625abbc262f3975 (diff) | |
| download | wikiget-d2c6df9248d03a692bf51c5367ac6f6de6c46ad1.tar.gz wikiget-d2c6df9248d03a692bf51c5367ac6f6de6c46ad1.zip | |
Add test for keyboard interrupt
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_wikiget_cli.py | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/tests/test_wikiget_cli.py b/tests/test_wikiget_cli.py index 28c4399..15e2a7b 100644 --- a/tests/test_wikiget_cli.py +++ b/tests/test_wikiget_cli.py @@ -27,7 +27,7 @@ from wikiget.wikiget import cli @patch("wikiget.wikiget.process_download") -class TestCli: +class TestWikigetCli: """Define tests related to wikiget.wikiget.cli.""" def test_cli_no_args(self, monkeypatch: pytest.MonkeyPatch) -> None: @@ -38,7 +38,7 @@ class TestCli: with pytest.raises(SystemExit) as e: cli() - assert e.value.code == 2 + assert e.value.code == 2 def test_cli_completed_successfully( self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch @@ -52,7 +52,7 @@ class TestCli: with pytest.raises(SystemExit) as e: cli() - assert e.value.code == 0 + assert e.value.code == 0 def test_cli_completed_with_problems( self, mock_process_download: MagicMock, monkeypatch: pytest.MonkeyPatch @@ -66,7 +66,7 @@ class TestCli: with pytest.raises(SystemExit) as e: cli() - assert e.value.code == 1 + assert e.value.code == 1 def test_cli_logs( self, @@ -87,15 +87,40 @@ class TestCli: with pytest.raises(SystemExit): cli() - assert caplog.record_tuples == [ - ( - "wikiget.wikiget", - logging.INFO, - f"Starting download session using wikiget {__version__}", - ), - ( - "wikiget.wikiget", - logging.DEBUG, - f"User agent: {USER_AGENT}", - ), - ] + assert caplog.record_tuples == [ + ( + "wikiget.wikiget", + logging.INFO, + f"Starting download session using wikiget {__version__}", + ), + ( + "wikiget.wikiget", + logging.DEBUG, + f"User agent: {USER_AGENT}", + ), + ] + + def test_cli_interrupt( + self, + mock_process_download: MagicMock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, + ) -> None: + """Test what happens when KeyboardInterrupt is raised during processing. + + A critical log message should be printed and the exit code should be 130. + """ + with monkeypatch.context() as m: + mock_process_download.side_effect = KeyboardInterrupt + m.setattr("sys.argv", ["wikiget", "File:Example.jpg"]) + + with pytest.raises(SystemExit) as e: + cli() + + assert e.value.code == 130 + # ignore the first two messages, since they're tested elsewhere + assert caplog.record_tuples[2] == ( + "wikiget.wikiget", + logging.CRITICAL, + "Interrupted by user", + ) |
