Commit 79b3c6495195dbd3ecb2e0eb06d9af3617a7cc1b

Authored by Christian Herdtweck
1 parent a3a57750

tests: make log test modules look more like regular ones

tests/common/log_helper/log_helper_test_imported.py
... ... @@ -4,7 +4,6 @@ by the main test file
4 4 """
5 5  
6 6 from oletools.common.log_helper import log_helper
7   -import logging
8 7  
9 8 DEBUG_MESSAGE = 'imported: debug log'
10 9 INFO_MESSAGE = 'imported: info log'
... ... @@ -14,7 +13,11 @@ CRITICAL_MESSAGE = 'imported: critical log'
14 13 RESULT_MESSAGE = 'imported: result log'
15 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 23 def log():
... ...
tests/common/log_helper/log_helper_test_main.py
1 1 """ Test log_helpers """
2 2  
3 3 import sys
  4 +import logging
4 5 from tests.common.log_helper import log_helper_test_imported
5 6 from oletools.common.log_helper import log_helper
6 7  
... ... @@ -15,7 +16,13 @@ RESULT_TYPE = 'main: result'
15 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 27 Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome:
21 28 - Log without enabling: ['<level>']
... ... @@ -36,13 +43,12 @@ def init_logging_and_log(args):
36 43 throw = 'throw' in args
37 44 percent_autoformat = '%-autoformat' in args
38 45  
  46 + log_helper_test_imported.logger.setLevel(logging.ERROR)
  47 +
39 48 if 'enable' in args:
40 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 53 if throw:
48 54 raise Exception('An exception occurred before ending the logging')
... ... @@ -50,7 +56,10 @@ def init_logging_and_log(args):
50 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 63 logger.debug(DEBUG_MESSAGE)
55 64 logger.info(INFO_MESSAGE)
56 65 logger.warning(WARNING_MESSAGE)
... ... @@ -61,4 +70,4 @@ def _log():
61 70  
62 71  
63 72 if __name__ == '__main__':
64   - init_logging_and_log(sys.argv[1:])
  73 + main(sys.argv[1:])
... ...