From 17ac17286c0d429e2b4860fcd6957ade0f16e94e Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 8 Oct 2020 18:32:00 -0700 Subject: [PATCH] python: Catch specific exceptions from subprocess. Signed-off-by: Anders Kaseorg --- zerver/lib/test_classes.py | 3 ++- zerver/logging_handlers.py | 2 +- zerver/tests/test_report.py | 3 ++- zerver/tests/test_zephyr.py | 3 ++- zerver/views/report.py | 2 +- zerver/views/zephyr.py | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 731cbc296b..dd1f54827e 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -2,6 +2,7 @@ import base64 import os import re import shutil +import subprocess import tempfile import urllib from contextlib import contextmanager @@ -970,7 +971,7 @@ Output: ''' with \ self.settings(ERROR_BOT=None), \ - mock.patch('zerver.lib.markdown.timeout', side_effect=KeyError('foo')), \ + mock.patch('zerver.lib.markdown.timeout', side_effect=subprocess.CalledProcessError(1, [])), \ mock.patch('zerver.lib.markdown.markdown_logger'): yield diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py index 6086ef9f86..c8b4c5263a 100644 --- a/zerver/logging_handlers.py +++ b/zerver/logging_handlers.py @@ -26,7 +26,7 @@ def try_git_describe() -> Optional[str]: stderr=subprocess.PIPE, cwd=os.path.join(os.path.dirname(__file__), '..'), ).strip().decode('utf-8') - except Exception: # nocoverage + except (FileNotFoundError, subprocess.CalledProcessError): # nocoverage return None def add_request_metadata(report: Dict[str, Any], request: HttpRequest) -> None: diff --git a/zerver/tests/test_report.py b/zerver/tests/test_report.py index e8706f314b..896d9f4228 100644 --- a/zerver/tests/test_report.py +++ b/zerver/tests/test_report.py @@ -1,3 +1,4 @@ +import subprocess from typing import Any, Callable, Dict, Iterable, List, Tuple from unittest import mock @@ -148,7 +149,7 @@ class TestReport(ZulipTestCase): subprocess_mock = mock.patch( 'zerver.views.report.subprocess.check_output', - side_effect=KeyError('foo'), + side_effect=subprocess.CalledProcessError(1, []), ) with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock: result = self.client_post("/json/report/error", params) diff --git a/zerver/tests/test_zephyr.py b/zerver/tests/test_zephyr.py index 8c6dffaf05..8d70a52030 100644 --- a/zerver/tests/test_zephyr.py +++ b/zerver/tests/test_zephyr.py @@ -1,3 +1,4 @@ +import subprocess from typing import Any from unittest.mock import patch @@ -51,7 +52,7 @@ class ZephyrTest(ZulipTestCase): with \ ccache_mock(return_value=b'1234'), \ - ssh_mock(side_effect=KeyError('foo')), \ + ssh_mock(side_effect=subprocess.CalledProcessError(1, [])), \ logging_mock() as log: result = post("zephyr", cred=cred) diff --git a/zerver/views/report.py b/zerver/views/report.py index 05c2edca15..5409e0d949 100644 --- a/zerver/views/report.py +++ b/zerver/views/report.py @@ -108,7 +108,7 @@ def report_error(request: HttpRequest, user_profile: UserProfile, message: str=R ["git", "show", "-s", "--oneline"], universal_newlines=True, ) - except Exception: + except (FileNotFoundError, subprocess.CalledProcessError): version = None # Get the IP address of the request diff --git a/zerver/views/zephyr.py b/zerver/views/zephyr.py index 7047bb23a2..61d50cda36 100644 --- a/zerver/views/zephyr.py +++ b/zerver/views/zephyr.py @@ -66,7 +66,7 @@ def webathena_kerberos_login(request: HttpRequest, user_profile: UserProfile, ] subprocess.check_call(["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", " ".join(map(shlex.quote, command))]) - except Exception: + except subprocess.CalledProcessError: logging.exception("Error updating the user's ccache", stack_info=True) return json_error(_("We were unable to setup mirroring for you"))