|
1 | 1 | import datetime |
| 2 | +import hashlib |
| 3 | +import optparse |
2 | 4 | import subprocess |
| 5 | +from typing import List |
3 | 6 | from unittest.mock import Mock |
4 | 7 |
|
5 | 8 | from django.core.exceptions import ImproperlyConfigured |
@@ -101,20 +104,75 @@ def test_serialize_exceptions(self) -> None: |
101 | 104 | with self.subTest(exc): |
102 | 105 | data = utils.exception_to_dict(exc) |
103 | 106 | self.assertEqual(utils.json_normalize(data), data) |
104 | | - self.assertEqual(set(data.keys()), {"exc_type", "exc_args"}) |
105 | | - reconstructed = utils.exception_from_dict(data) |
106 | | - self.assertIsInstance(reconstructed, type(exc)) |
107 | | - self.assertEqual(reconstructed.args, exc.args) |
| 107 | + self.assertEqual( |
| 108 | + set(data.keys()), {"exc_type", "exc_args", "exc_traceback"} |
| 109 | + ) |
| 110 | + exception = utils.exception_from_dict(data) |
| 111 | + self.assertIsInstance(exception, type(exc)) |
| 112 | + self.assertEqual(exception.args, exc.args) |
| 113 | + |
| 114 | + # Check that the exception traceback contains a minimal traceback |
| 115 | + msg = str(exc.args[0]) if exc.args else "" |
| 116 | + traceback = data["exc_traceback"] |
| 117 | + self.assertIn(exc.__class__.__name__, traceback) |
| 118 | + self.assertIn(msg, traceback) |
| 119 | + |
| 120 | + def test_serialize_full_traceback(self) -> None: |
| 121 | + try: |
| 122 | + # Using optparse to generate an error because: |
| 123 | + # - it's pure python |
| 124 | + # - it's easy to trip down |
| 125 | + # - it's unlikely to change ever |
| 126 | + optparse.OptionParser(option_list=[1]) # type: ignore |
| 127 | + except Exception as e: |
| 128 | + traceback = utils.exception_to_dict(e)["exc_traceback"] |
| 129 | + # The test is willingly fuzzy to ward against changes in the |
| 130 | + # traceback formatting |
| 131 | + self.assertIn("traceback", traceback.lower()) |
| 132 | + self.assertIn("line", traceback.lower()) |
| 133 | + self.assertIn(optparse.__file__, traceback) |
| 134 | + self.assertTrue( |
| 135 | + traceback.endswith("TypeError: not an Option instance: 1\n") |
| 136 | + ) |
| 137 | + |
| 138 | + def test_serialize_traceback_from_c_module(self) -> None: |
| 139 | + try: |
| 140 | + # Same as test_serialize_full_traceback, but uses hashlib |
| 141 | + # because it's in C, not in Python |
| 142 | + hashlib.md5(1) # type: ignore |
| 143 | + except Exception as e: |
| 144 | + traceback = utils.exception_to_dict(e)["exc_traceback"] |
| 145 | + self.assertIn("traceback", traceback.lower()) |
| 146 | + self.assertTrue( |
| 147 | + traceback.endswith( |
| 148 | + "TypeError: object supporting the buffer API required\n" |
| 149 | + ) |
| 150 | + ) |
| 151 | + self.assertIn("hashlib.md5(1)", traceback) |
108 | 152 |
|
109 | 153 | def test_cannot_deserialize_non_exception(self) -> None: |
110 | | - for data in [ |
111 | | - {"exc_type": "subprocess.check_output", "exc_args": ["exit", "1"]}, |
112 | | - {"exc_type": "True", "exc_args": []}, |
113 | | - {"exc_type": "math.pi", "exc_args": []}, |
114 | | - {"exc_type": __name__, "exc_args": []}, |
115 | | - {"exc_type": utils.get_module_path(type(self)), "exc_args": []}, |
116 | | - {"exc_type": utils.get_module_path(Mock), "exc_args": []}, |
117 | | - ]: |
| 154 | + serialized_exceptions: List[utils.SerializedExceptionDict] = [ |
| 155 | + { |
| 156 | + "exc_type": "subprocess.check_output", |
| 157 | + "exc_args": ["exit", "1"], |
| 158 | + "exc_traceback": "", |
| 159 | + }, |
| 160 | + {"exc_type": "True", "exc_args": [], "exc_traceback": ""}, |
| 161 | + {"exc_type": "math.pi", "exc_args": [], "exc_traceback": ""}, |
| 162 | + {"exc_type": __name__, "exc_args": [], "exc_traceback": ""}, |
| 163 | + { |
| 164 | + "exc_type": utils.get_module_path(type(self)), |
| 165 | + "exc_args": [], |
| 166 | + "exc_traceback": "", |
| 167 | + }, |
| 168 | + { |
| 169 | + "exc_type": utils.get_module_path(Mock), |
| 170 | + "exc_args": [], |
| 171 | + "exc_traceback": "", |
| 172 | + }, |
| 173 | + ] |
| 174 | + |
| 175 | + for data in serialized_exceptions: |
118 | 176 | with self.subTest(data): |
119 | 177 | with self.assertRaises((TypeError, ImportError)): |
120 | 178 | utils.exception_from_dict(data) |
0 commit comments