Skip to content

Commit

Permalink
Increased code coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
scnerd committed Apr 5, 2018
1 parent 94bad9f commit c2b117c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 3 additions & 3 deletions miniutils/logs.py
Expand Up @@ -12,8 +12,8 @@ def enable_logging(log_level='NOTSET', *, logdir=None, use_colors=True, capture_
import sys
import logging.handlers

if logdir is not None and not os.path.exists(logdir):
os.makedirs(logdir)
if logdir is not None:
os.makedirs(logdir, exist_ok=True)
logs_base.logger = logging.getLogger()

for handler in logs_base.logger.handlers:
Expand Down Expand Up @@ -64,7 +64,7 @@ def format(self, record):
return super().format(record)

color_formatter = SmarterColorer(fmt=format_str)
except ImportError:
except ImportError: # pragma: nocover
color_formatter = plain_formatter
else:
color_formatter = plain_formatter
Expand Down
32 changes: 29 additions & 3 deletions tests/test_logging.py
@@ -1,4 +1,7 @@
import sys
import os
import shutil
import tempfile
from unittest import TestCase

from miniutils.capture_output import captured_output
Expand Down Expand Up @@ -29,9 +32,6 @@ def test_logging_imports(self):
#self.assertEqual(third.lower(), '__3__')

def test_log_dir(self):
import tempfile
import os

with tempfile.TemporaryDirectory() as d:
from miniutils.logs import enable_logging
log = enable_logging(logdir=d)
Expand All @@ -44,3 +44,29 @@ def test_log_dir(self):
print(">>> {} <<<".format(log_files), file=sys.__stderr__)
self.assertIn('TEST', log_files)

def test_log_dir_not_exists(self):
from miniutils.logs import enable_logging

dir_path = '__test_logs'
assert not os.path.exists(dir_path)

try:
log = enable_logging(logdir=dir_path)

assert os.path.exists(dir_path)
assert os.path.isdir(dir_path)

log.critical('TEST')

del log

log_files = [os.path.join(dir_path, f) for f in os.listdir(dir_path)]
log_files = [f for f in log_files if os.path.isfile(f)]
log_files = "\n".join(open(f).read() for f in log_files)
print(">>> {} <<<".format(log_files), file=sys.__stderr__)
self.assertIn('TEST', log_files)
except Exception:
raise
finally:
if os.path.exists(dir_path):
shutil.rmtree(dir_path, ignore_errors=True)

0 comments on commit c2b117c

Please sign in to comment.