Commit 28c0449c0f9f9431eaec782ad22239d5a19f67d0
1 parent
dc8c0a9d
tests: Add 2 tests for json formatting
Need to make sure that json output is formatted since e.g. warning messages
are created like this:
log.warning('%s', actual_message)
so without proper formatting the message is just "%s".
Also test that exception info is added as usual.
Showing
2 changed files
with
26 additions
and
3 deletions
tests/common/log_helper/log_helper_test_main.py
| ... | ... | @@ -49,6 +49,7 @@ def main(args): |
| 49 | 49 | throw = 'throw' in args |
| 50 | 50 | percent_autoformat = '%-autoformat' in args |
| 51 | 51 | warn = 'warn' in args |
| 52 | + exc_info = 'exc-info' in args | |
| 52 | 53 | |
| 53 | 54 | log_helper_test_imported.logger.setLevel(logging.ERROR) |
| 54 | 55 | |
| ... | ... | @@ -64,6 +65,12 @@ def main(args): |
| 64 | 65 | warnings.warn(ACTUAL_WARNING) |
| 65 | 66 | log_helper_test_imported.warn() |
| 66 | 67 | |
| 68 | + if exc_info: | |
| 69 | + try: | |
| 70 | + raise Exception('This is an exception') | |
| 71 | + except Exception: | |
| 72 | + logger.exception('Caught exception') # has exc_info=True | |
| 73 | + | |
| 67 | 74 | log_helper.end_logging() |
| 68 | 75 | |
| 69 | 76 | ... | ... |
tests/common/log_helper/test_log_helper.py
| ... | ... | @@ -17,8 +17,7 @@ from os.path import dirname, join, relpath, abspath |
| 17 | 17 | from tests.test_utils import PROJECT_ROOT |
| 18 | 18 | |
| 19 | 19 | # test file we use as "main" module |
| 20 | -TEST_FILE = relpath(join(dirname(abspath(__file__)), 'log_helper_test_main.py'), | |
| 21 | - PROJECT_ROOT) | |
| 20 | +TEST_FILE = join(dirname(abspath(__file__)), 'log_helper_test_main.py') | |
| 22 | 21 | |
| 23 | 22 | # test file simulating a third party main module that only imports oletools |
| 24 | 23 | TEST_FILE_3RD_PARTY = relpath(join(dirname(abspath(__file__)), |
| ... | ... | @@ -27,6 +26,8 @@ TEST_FILE_3RD_PARTY = relpath(join(dirname(abspath(__file__)), |
| 27 | 26 | |
| 28 | 27 | PYTHON_EXECUTABLE = sys.executable |
| 29 | 28 | |
| 29 | +PERCENT_FORMAT_OUTPUT = 'The answer is 47.' | |
| 30 | + | |
| 30 | 31 | |
| 31 | 32 | class TestLogHelper(unittest.TestCase): |
| 32 | 33 | def test_it_doesnt_log_when_not_enabled(self): |
| ... | ... | @@ -114,7 +115,7 @@ class TestLogHelper(unittest.TestCase): |
| 114 | 115 | def test_percent_autoformat(self): |
| 115 | 116 | """Test that auto-formatting of log strings with `%` works.""" |
| 116 | 117 | output = self._run_test(['enable', '%-autoformat', 'info']) |
| 117 | - self.assertIn('The answer is 47.', output) | |
| 118 | + self.assertIn(PERCENT_FORMAT_OUTPUT, output) | |
| 118 | 119 | |
| 119 | 120 | def test_json_correct_on_exceptions(self): |
| 120 | 121 | """ |
| ... | ... | @@ -208,6 +209,21 @@ class TestLogHelper(unittest.TestCase): |
| 208 | 209 | ]) |
| 209 | 210 | self.assertEqual(output.strip(), expect) |
| 210 | 211 | |
| 212 | + def test_json_percent_formatting(self): | |
| 213 | + """Test that json-output has formatting args included in output.""" | |
| 214 | + output = self._run_test(['enable', 'as-json', '%-autoformat', 'info']) | |
| 215 | + json.loads(output) # check that this does not raise, so json is valid | |
| 216 | + self.assertIn(PERCENT_FORMAT_OUTPUT, output) | |
| 217 | + | |
| 218 | + def test_json_exception_formatting(self): | |
| 219 | + """Test that json-output has formatted exception info in output""" | |
| 220 | + output = self._run_test(['enable', 'as-json', 'exc-info', 'info']) | |
| 221 | + json.loads(output) # check that this does not raise, so json is valid | |
| 222 | + self.assertIn('Caught exception', output) # actual log message | |
| 223 | + self.assertIn('This is an exception', output) # message of caught exception | |
| 224 | + self.assertIn('Traceback (most recent call last)', output) # start of trace | |
| 225 | + self.assertIn(TEST_FILE, output) # part of trace | |
| 226 | + | |
| 211 | 227 | def _assert_json_messages(self, output, messages): |
| 212 | 228 | try: |
| 213 | 229 | json_data = json.loads(output) | ... | ... |