Skip to content

Commit

Permalink
Fix test_logging issue with windows.
Browse files Browse the repository at this point in the history
Change test_logging.py to use the LogCapture package instead of trying
to work with log files.
  • Loading branch information
KSchopmeyer committed Jul 18, 2017
1 parent df8c6bd commit 2f41525
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def run(self):
"coverage>=4.3",
"pytest>=2.4",
"pytest-cov",
'testfixtures>=4.13.3',
"Sphinx>=1.3" if sys.version_info[0:2] != (2, 6) else None,
# Pinning GitPython to 2.0.8 max, due to its use of unittest.case
# which is not available on Python 2.6.
Expand Down
40 changes: 13 additions & 27 deletions testsuite/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# pylint: disable=invalid-name,missing-docstring,too-many-statements
# pylint: disable=too-many-lines,no-self-use
import unittest
import logging
from testfixtures import LogCapture, log_capture

from pywbem import PywbemLoggers, LOG_OPS_CALLS_NAME
from pywbem._logging import get_logger
Expand All @@ -44,31 +44,12 @@

class BaseLoggingTests(unittest.TestCase):
"""Base class for logging unit tests"""

def setUp(self):
PywbemLoggers.reset()
if os.path.isfile(TEST_OUTPUT_LOG):
os.remove(TEST_OUTPUT_LOG)

def tearDown(self):

# Close any open logging files
# Windows log files be closed to be removed.
if os.path.isfile(TEST_OUTPUT_LOG):
logger = logging.getLogger(LOG_OPS_CALLS_NAME)
if logger.handlers:
handlers = logger.handlers[:]
for handler in handlers:
handler.close()
logger.removeHandler(handler)

os.remove(TEST_OUTPUT_LOG)

def loadLogfile(self):
if os.path.isfile(TEST_OUTPUT_LOG):
with open(TEST_OUTPUT_LOG) as f:
lines = f.read().splitlines()
return lines
return None
LogCapture.uninstall_all()


class TestLogParse(BaseLoggingTests):
Expand Down Expand Up @@ -182,7 +163,9 @@ def test_create_single_logger2(self):
expected_result = \
{'pywbem.http': ('min', 'debug', 'file', TEST_OUTPUT_LOG)}

self.assertEqual(PywbemLoggers.loggers, expected_result)
self.assertEqual(PywbemLoggers.loggers, expected_result,
'Actual %s, Expected %s' % (PywbemLoggers.loggers,
expected_result))

def test_create_single_logger3(self):
"""
Expand All @@ -198,7 +181,9 @@ def test_create_single_logger3(self):
expected_result = \
{'pywbem.http': ('min', 'debug', 'stderr', None)}

self.assertEqual(PywbemLoggers.loggers, expected_result)
self.assertEqual(PywbemLoggers.loggers, expected_result,
'Actual %s, Expected %s' % (PywbemLoggers.loggers,
expected_result))

def test_create_single_logger4(self):
"""
Expand Down Expand Up @@ -346,7 +331,8 @@ def test_create_loggers2(self):
class TestLoggerOutput(BaseLoggingTests):
"""Test output from logging"""

def test_log_output(self):
@log_capture()
def test_log_output(self, l):
test_input = 'all=file:all:debug'

print('log filename %s' % TEST_OUTPUT_LOG)
Expand All @@ -370,8 +356,8 @@ def test_log_output(self):

my_logger.debug('%s: %s: %s', return_name, 'FakeMethodName', result)

logged_data = self.loadLogfile()
print('Log file contents:\n%s' % logged_data)
l.check(('pywbem.ops', 'DEBUG',
"Return: FakeMethodName: 'This is fake return data'"))


if __name__ == '__main__':
Expand Down
15 changes: 8 additions & 7 deletions testsuite/test_wbemcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,20 @@ def test(self): # pylint: disable=missing-docstring
# pywarn = 'export PYTHONWARNINGS= "" &&'

# command = 'PYTHONWARNINGS="" && %s' % command
bash_cmd = 'bash -c "set -o pipefail; PYTHONWARNINGS= %s"' % cmd
bash_cmd = 'bash -c "set -o pipefail; PYTHONWARNINGS= %s"' % \
cmd

proc = Popen(bash_cmd, shell=True, stdout=PIPE, stderr=PIPE)
std_out, std_err = proc.communicate()
exitcode = proc.returncode
print('bash_cmd: %s\nexitcode: %s std_out:\n%s\nstd_err:\n%s' %
(bash_cmd, exitcode, std_out, std_err))
print('cwd %s' % os.getcwd())
dir1 = '/cygdrive/c/projects/pywbem/.tox/pywin/Scripts'
dir2 = '/cygdrive/c/projects/pywbem/.tox/pywin'
dir3 = '/cygdrive/c/projects/pywbem/.tox'

if os.name == 'nt':
print('bash_cmd: %s\nexitcode: %s std_out:\n%s\nstd_err:'
'\n%s' % (bash_cmd, exitcode, std_out, std_err))
print('cwd %s' % os.getcwd())
dir1 = '/cygdrive/c/projects/pywbem/.tox/pywin/Scripts'
dir2 = '/cygdrive/c/projects/pywbem/.tox/pywin'
dir3 = '/cygdrive/c/projects/pywbem/.tox'
print('dir %' % os.listdir(dir1))
print('dir %' % os.listdir(dir2))
print('dir %' % os.listdir(dir3))
Expand Down

0 comments on commit 2f41525

Please sign in to comment.