Commit 79b3c6495195dbd3ecb2e0eb06d9af3617a7cc1b
1 parent
a3a57750
tests: make log test modules look more like regular ones
Showing
2 changed files
with
21 additions
and
9 deletions
tests/common/log_helper/log_helper_test_imported.py
| @@ -4,7 +4,6 @@ by the main test file | @@ -4,7 +4,6 @@ by the main test file | ||
| 4 | """ | 4 | """ |
| 5 | 5 | ||
| 6 | from oletools.common.log_helper import log_helper | 6 | from oletools.common.log_helper import log_helper |
| 7 | -import logging | ||
| 8 | 7 | ||
| 9 | DEBUG_MESSAGE = 'imported: debug log' | 8 | DEBUG_MESSAGE = 'imported: debug log' |
| 10 | INFO_MESSAGE = 'imported: info log' | 9 | INFO_MESSAGE = 'imported: info log' |
| @@ -14,7 +13,11 @@ CRITICAL_MESSAGE = 'imported: critical log' | @@ -14,7 +13,11 @@ CRITICAL_MESSAGE = 'imported: critical log' | ||
| 14 | RESULT_MESSAGE = 'imported: result log' | 13 | RESULT_MESSAGE = 'imported: result log' |
| 15 | RESULT_TYPE = 'imported: result' | 14 | RESULT_TYPE = 'imported: result' |
| 16 | 15 | ||
| 17 | -logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR) | 16 | +logger = log_helper.get_or_create_silent_logger('test_imported') |
| 17 | + | ||
| 18 | +def enable_logging(): | ||
| 19 | + """Enable logging if imported by third party modules.""" | ||
| 20 | + logger.setLevel(log_helper.NOTSET) | ||
| 18 | 21 | ||
| 19 | 22 | ||
| 20 | def log(): | 23 | def log(): |
tests/common/log_helper/log_helper_test_main.py
| 1 | """ Test log_helpers """ | 1 | """ Test log_helpers """ |
| 2 | 2 | ||
| 3 | import sys | 3 | import sys |
| 4 | +import logging | ||
| 4 | from tests.common.log_helper import log_helper_test_imported | 5 | from tests.common.log_helper import log_helper_test_imported |
| 5 | from oletools.common.log_helper import log_helper | 6 | from oletools.common.log_helper import log_helper |
| 6 | 7 | ||
| @@ -15,7 +16,13 @@ RESULT_TYPE = 'main: result' | @@ -15,7 +16,13 @@ RESULT_TYPE = 'main: result' | ||
| 15 | logger = log_helper.get_or_create_silent_logger('test_main') | 16 | logger = log_helper.get_or_create_silent_logger('test_main') |
| 16 | 17 | ||
| 17 | 18 | ||
| 18 | -def init_logging_and_log(args): | 19 | +def enable_logging(): |
| 20 | + """Enable logging if imported by third party modules.""" | ||
| 21 | + logger.setLevel(log_helper.NOTSET) | ||
| 22 | + log_helper_test_imported.enable_logging() | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +def main(args): | ||
| 19 | """ | 26 | """ |
| 20 | Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: | 27 | Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome: |
| 21 | - Log without enabling: ['<level>'] | 28 | - Log without enabling: ['<level>'] |
| @@ -36,13 +43,12 @@ def init_logging_and_log(args): | @@ -36,13 +43,12 @@ def init_logging_and_log(args): | ||
| 36 | throw = 'throw' in args | 43 | throw = 'throw' in args |
| 37 | percent_autoformat = '%-autoformat' in args | 44 | percent_autoformat = '%-autoformat' in args |
| 38 | 45 | ||
| 46 | + log_helper_test_imported.logger.setLevel(logging.ERROR) | ||
| 47 | + | ||
| 39 | if 'enable' in args: | 48 | if 'enable' in args: |
| 40 | log_helper.enable_logging(use_json, level, stream=sys.stdout) | 49 | log_helper.enable_logging(use_json, level, stream=sys.stdout) |
| 41 | 50 | ||
| 42 | - _log() | ||
| 43 | - | ||
| 44 | - if percent_autoformat: | ||
| 45 | - logger.info('The %s is %d.', 'answer', 47) | 51 | + do_log(percent_autoformat) |
| 46 | 52 | ||
| 47 | if throw: | 53 | if throw: |
| 48 | raise Exception('An exception occurred before ending the logging') | 54 | raise Exception('An exception occurred before ending the logging') |
| @@ -50,7 +56,10 @@ def init_logging_and_log(args): | @@ -50,7 +56,10 @@ def init_logging_and_log(args): | ||
| 50 | log_helper.end_logging() | 56 | log_helper.end_logging() |
| 51 | 57 | ||
| 52 | 58 | ||
| 53 | -def _log(): | 59 | +def do_log(percent_autoformat=False): |
| 60 | + if percent_autoformat: | ||
| 61 | + logger.info('The %s is %d.', 'answer', 47) | ||
| 62 | + | ||
| 54 | logger.debug(DEBUG_MESSAGE) | 63 | logger.debug(DEBUG_MESSAGE) |
| 55 | logger.info(INFO_MESSAGE) | 64 | logger.info(INFO_MESSAGE) |
| 56 | logger.warning(WARNING_MESSAGE) | 65 | logger.warning(WARNING_MESSAGE) |
| @@ -61,4 +70,4 @@ def _log(): | @@ -61,4 +70,4 @@ def _log(): | ||
| 61 | 70 | ||
| 62 | 71 | ||
| 63 | if __name__ == '__main__': | 72 | if __name__ == '__main__': |
| 64 | - init_logging_and_log(sys.argv[1:]) | 73 | + main(sys.argv[1:]) |