From d32d7a9b4d98a83d049088069087e1ef694196c5 Mon Sep 17 00:00:00 2001 From: Aayush Agrawal Date: Sun, 11 Mar 2018 21:59:38 +0530 Subject: [PATCH] test_logging_handlers: Mock out `git describe` because can be slow. This cuts out about 11 calls to `git describe`. In a nice fast LXC container following our instructions for development on a Linux host, this might save "only" about 1.5s; in a dev environment on a Windows host, the savings have been clocked at 49s, presumably due to an extremely slow filesystem in the VM. The tests weren't doing much with this codepath as they were, and there isn't a lot of value to be gained by testing it anyway; it's totally non-critical and rarely changes. [Commit message rewritten by greg.] --- zerver/logging_handlers.py | 2 +- zerver/tests/test_logging_handlers.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py index a02bb3a7d7..b299743ad7 100644 --- a/zerver/logging_handlers.py +++ b/zerver/logging_handlers.py @@ -18,7 +18,7 @@ from zerver.lib.queue import queue_json_publish from version import ZULIP_VERSION def try_git_describe() -> Optional[str]: - try: + try: # nocoverage return subprocess.check_output( ['git', '--git-dir', os.path.join(os.path.dirname(__file__), '../.git'), diff --git a/zerver/tests/test_logging_handlers.py b/zerver/tests/test_logging_handlers.py index 21f31b56b2..238368f9e3 100644 --- a/zerver/tests/test_logging_handlers.py +++ b/zerver/tests/test_logging_handlers.py @@ -9,7 +9,7 @@ from django.http import HttpRequest, HttpResponse from django.test import RequestFactory, TestCase from django.utils.log import AdminEmailHandler from functools import wraps -from mock import patch +from mock import MagicMock, patch from mypy_extensions import NoReturn from typing import Any, Callable, Dict, Mapping, Optional, Text, Iterator @@ -61,7 +61,9 @@ class AdminNotifyHandlerTest(ZulipTestCase): if isinstance(h, AdminNotifyHandler) ][0] - def test_basic(self) -> None: + @patch('zerver.logging_handlers.try_git_describe') + def test_basic(self, mock_function: MagicMock) -> None: + mock_function.return_value = None """A random exception passes happily through AdminNotifyHandler""" handler = self.get_admin_zulip_handler() try: @@ -92,7 +94,9 @@ class AdminNotifyHandlerTest(ZulipTestCase): patched_notify.assert_called_once() return patched_notify.call_args[0][0] - def test_long_exception_request(self) -> None: + @patch('zerver.logging_handlers.try_git_describe') + def test_long_exception_request(self, mock_function: MagicMock) -> None: + mock_function.return_value = None """A request with no stack and multi-line report.getMessage() is handled properly""" record = self.simulate_error() record.exc_info = None @@ -105,7 +109,9 @@ class AdminNotifyHandlerTest(ZulipTestCase): self.assertEqual(report['stack_trace'], 'message\nmoremesssage\nmore') self.assertEqual(report['message'], 'message') - def test_request(self) -> None: + @patch('zerver.logging_handlers.try_git_describe') + def test_request(self, mock_function: MagicMock) -> None: + mock_function.return_value = None """A normal request is handled properly""" record = self.simulate_error()