Skip to content

Commit

Permalink
Dont print to sdout in unit tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
scottrice committed Jan 24, 2016
1 parent 2c7e04c commit 1cc2e64
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ice/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
Copyright (c) 2013 Scott Rice. All rights reserved.
"""

import sys
import inspect
import os
import sys
import time
import traceback

Expand All @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 1cc2e64

Please sign in to comment.