Skip to content

Commit

Permalink
simplify log handlers in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgross committed Jan 30, 2016
1 parent 7a45aeb commit 1075a76
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
bin
build
/develop-eggs/
/eggs
dist
lib
parts
Expand Down
22 changes: 5 additions & 17 deletions src/zLOG/tests/testzLog.py
Expand Up @@ -34,23 +34,13 @@ class EventLogTest(unittest.TestCase):
"""Test zLOG with the default implementation."""

def setUp(self):
self.path = tempfile.mkstemp()[1]
self.f, self.path = tempfile.mkstemp()
self._severity = 0
# Windows cannot remove a file that's open. The logging code
# keeps the log file open, and I can't find an advertised API
# to tell the logger to close a log file. So here we cheat:
# tearDown() will close and remove all the handlers that pop
# into existence after setUp() runs. This breaks into internals,
# but I couldn't find a sane way to do it.
self.handlers = list(logging._handlers.keys()) # capture current handlers
self.loghandler = self.setLog()

def tearDown(self):
# Close and remove all the handlers that came into existence
# since setUp ran.
for h in list(logging._handlers.keys()):
if h not in self.handlers:
h.close()
del logging._handlers[h]
self.loghandler.close()
os.close(self.f)
os.remove(self.path)

def setLog(self, severity=0):
Expand All @@ -64,6 +54,7 @@ def setLog(self, severity=0):
logger.addHandler(handler)

self._severity = severity
return handler

def verifyEntry(self, f, time=None, subsys=None, severity=None,
summary=None, detail=None, error=None):
Expand Down Expand Up @@ -108,19 +99,16 @@ def getLogFile(self):
return open(self.path, 'r')

def test_basics(self):
self.setLog()
zLOG.LOG("basic", zLOG.INFO, "summary")
with self.getLogFile() as f:
self.verifyEntry(f, subsys="basic", summary="summary")

def test_detail(self):
self.setLog()
zLOG.LOG("basic", zLOG.INFO, "xxx", "this is a detail")
with self.getLogFile() as f:
self.verifyEntry(f, subsys="basic", detail="detail")

def test_error(self):
self.setLog()
try:
1 / 0
except ZeroDivisionError:
Expand Down

0 comments on commit 1075a76

Please sign in to comment.