From 7ed10da4adbfa1a3dfbc9e967f351fe12aa985f7 Mon Sep 17 00:00:00 2001 From: Elliott Jin Date: Tue, 28 Feb 2017 19:19:56 -0800 Subject: [PATCH] test-backend: Raise zerver/views/report.py test coverage to 100%. --- tools/test-backend | 1 - zerver/tests/test_report.py | 11 +++++++++++ zerver/views/report.py | 14 ++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tools/test-backend b/tools/test-backend index 58d2ca14c4..b5486a6183 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -97,7 +97,6 @@ not_yet_fully_covered = { # Getting views file coverage to 100% is a major project goal 'zerver/views/auth.py', 'zerver/views/messages.py', - 'zerver/views/report.py', 'zerver/views/home.py', 'zerver/views/registration.py', 'zerver/views/events_register.py', diff --git a/zerver/tests/test_report.py b/zerver/tests/test_report.py index 4de315ff94..8b9472f03f 100644 --- a/zerver/tests/test_report.py +++ b/zerver/tests/test_report.py @@ -143,3 +143,14 @@ class TestReport(ZulipTestCase): with self.settings(BROWSER_ERROR_REPORTING=False): result = self.client_post("/json/report_error", params) self.assert_json_success(result) + + # If js_source_map is present, then the stack trace should be annotated. + # DEBUG=False and TEST_SUITE=False are necessary to ensure that + # js_source_map actually gets instantiated. + with \ + self.settings(DEBUG=False, TEST_SUITE=False), \ + mock.patch('zerver.lib.unminify.SourceMap.annotate_stacktrace') as annotate: + result = self.client_post("/json/report_error", params) + self.assert_json_success(result) + # fix_params (see above) adds quotes when JSON encoding. + annotate.assert_called_once_with('"trace"') diff --git a/zerver/views/report.py b/zerver/views/report.py index 4eb42e632f..e196b1d189 100644 --- a/zerver/views/report.py +++ b/zerver/views/report.py @@ -18,11 +18,16 @@ from typing import Optional, Text import subprocess import os -# Read the source map information for decoding JavaScript backtraces js_source_map = None -if not (settings.DEBUG or settings.TEST_SUITE): - js_source_map = SourceMap(os.path.join( - settings.DEPLOY_ROOT, 'prod-static/source-map')) + +# Read the source map information for decoding JavaScript backtraces. +def get_js_source_map(): + # type: () -> Optional[SourceMap] + global js_source_map + if not js_source_map and not (settings.DEBUG or settings.TEST_SUITE): + js_source_map = SourceMap(os.path.join( + settings.DEPLOY_ROOT, 'prod-static/source-map')) + return js_source_map @authenticated_json_post_view @has_request_variables @@ -85,6 +90,7 @@ def json_report_error(request, user_profile, message=REQ(), stacktrace=REQ(), if not settings.BROWSER_ERROR_REPORTING: return json_success() + js_source_map = get_js_source_map() if js_source_map: stacktrace = js_source_map.annotate_stacktrace(stacktrace)