From 1cc2e64d44e915a533a7501d9fcc212eb45555d0 Mon Sep 17 00:00:00 2001 From: Scott Rice Date: Sun, 24 Jan 2016 14:17:39 -0800 Subject: [PATCH] Dont print to sdout in unit tests. Since the logs module was imported early it was using the wrong `sys.stderr`, so you would still get logspew in stderr during the test, which throws off the test reporting. This commit uses the inspect module to figure out if we're running in a unit test and doesnt add the stream/file handler to the logger when that happens, meaning only the capture handler gets used. --- ice/logs.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ice/logs.py b/ice/logs.py index 4e1509f..04c6de8 100644 --- a/ice/logs.py +++ b/ice/logs.py @@ -6,8 +6,9 @@ Copyright (c) 2013 Scott Rice. All rights reserved. """ -import sys +import inspect import os +import sys import time import traceback @@ -16,6 +17,16 @@ import paths +def is_test_stack_frame(frame): + # The path of the executing file is in frame[1] + path = frame[1] + # See if the path is in the unittest module + return "unittest" in path + +def is_running_in_test(): + current_stack = inspect.stack() + return any(map(is_test_stack_frame, inspect.stack())) + class IceLevelTagFilter(logging.Formatter): def _tag_for_level(self, levelno): @@ -47,8 +58,9 @@ def create_logger(): # loggers logger = logging.getLogger('Ice') logger.setLevel(logging.DEBUG) - logger.addHandler(ch) - logger.addHandler(fh) + if not is_running_in_test(): + logger.addHandler(ch) + logger.addHandler(fh) return logger logger = create_logger()