Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derra branch Mine functions I improved the coverage for #3780

Closed
wants to merge 10 commits into from
20 changes: 19 additions & 1 deletion nikola/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,33 @@ def wrap_in_color(self, record: logging.LogRecord) -> str:
# Initial configuration
class LoggingMode(enum.Enum):
"""Logging mode options."""

NORMAL = 0
STRICT = 1
QUIET = 2

branch_coverage_configure = {
"branch_1": False,
"branch_2": False,
"branch_3": False,
"branch_4": False
}


def configure_logging(logging_mode: LoggingMode = LoggingMode.NORMAL) -> None:
"""Configure logging for Nikola.

This method can be called multiple times, previous configuration will be overridden.
"""
if DEBUG:
branch_coverage_configure["branch_1"] = True
logging.root.level = logging.DEBUG
else:
branch_coverage_configure["branch_2"] = True
logging.root.level = logging.INFO

if logging_mode == LoggingMode.QUIET:
branch_coverage_configure["branch_3"] = True
logging.root.handlers = []
return

Expand All @@ -110,14 +120,22 @@ def configure_logging(logging_mode: LoggingMode = LoggingMode.NORMAL) -> None:

handlers = [handler]
if logging_mode == LoggingMode.STRICT:
branch_coverage_configure["branch_4"] = True
handlers.append(StrictModeExceptionHandler())

logging.root.handlers = handlers

def print_coverage_configure():
for branch, hit in branch_coverage_configure.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
def reset_coverage_configure():
for key in branch_coverage_configure:
branch_coverage_configure[key] = False

configure_logging()



# For compatibility with old code written with Logbook in mind
# TODO remove in v9
def patch_notice_level(logger: logging.Logger) -> logging.Logger:
Expand Down
23 changes: 22 additions & 1 deletion nikola/plugins/command/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@

class CommandStatus(Command):
"""Display site status."""

branch_coverage = {
"branch_1": False,
"branch_2": False,
"branch_3": False,
"branch_4": False
}


name = "status"

name = "status"
doc_purpose = "display site status"
doc_description = "Show information about the posts and site deployment."
doc_usage = '[-d|--list-drafts] [-m|--list-modified] [-p|--list-private] [-P|--list-published] [-s|--list-scheduled]'
Expand Down Expand Up @@ -163,9 +171,22 @@ def human_time(self, dt):
hours = dt.seconds / 60 // 60
minutes = dt.seconds / 60 - (hours * 60)
if days > 0:
self.branch_coverage["branch_1"] = True
return "{0:.0f} days and {1:.0f} hours".format(days, hours)
elif hours > 0:
self.branch_coverage["branch_2"] = True
return "{0:.0f} hours and {1:.0f} minutes".format(hours, minutes)
elif minutes:
self.branch_coverage["branch_3"] = True
return "{0:.0f} minutes".format(minutes)
self.branch_coverage["branch_4"] = True
return False


def report_coverage(self):
for branch, hit in self.branch_coverage.items():
print(f"{branch}: {'covered' if hit else 'not covered'}")

def reset_coverage(self):
for key in self.branch_coverage:
self.branch_coverage[key] = False
37 changes: 37 additions & 0 deletions tests/test_configure_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from nikola.log import configure_logging, print_coverage_configure, reset_coverage_configure, LoggingMode
from unittest.mock import patch

def test_normal_mode_debug():
reset_coverage_configure()
with patch('nikola.log.DEBUG', True):
configure_logging(logging_mode=LoggingMode.NORMAL)
print_coverage_configure()


def test_normal_mode_no_debug():
reset_coverage_configure()
configure_logging(logging_mode=LoggingMode.NORMAL)
print_coverage_configure()


def test_quiet_mode_debug():
reset_coverage_configure()
with patch('nikola.log.DEBUG', True):
configure_logging(logging_mode=LoggingMode.QUIET)
print_coverage_configure()

def test_quiet_mode_no_debug():
reset_coverage_configure()
configure_logging(logging_mode=LoggingMode.QUIET)
print_coverage_configure()

def test_strict_mode_debug():
reset_coverage_configure()
with patch('nikola.log.DEBUG', True):
configure_logging(logging_mode=LoggingMode.STRICT)
print_coverage_configure()

def test_strict_mode_no_debug():
reset_coverage_configure()
configure_logging(logging_mode=LoggingMode.STRICT)
print_coverage_configure()
34 changes: 34 additions & 0 deletions tests/test_human.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from datetime import timedelta
from nikola.plugins.command.status import CommandStatus

@pytest.fixture(scope="module")
def command_status():
return CommandStatus()


def test_days_branch(command_status):
command_status.reset_coverage()
result = command_status.human_time(timedelta(days=2, hours=3))
print(result)
command_status.report_coverage()

def test_hours_branch(command_status):
command_status.reset_coverage()
result = command_status.human_time(timedelta(hours=4, minutes=20))
print(result)
command_status.report_coverage()

def test_minutes_branch(command_status):
command_status.reset_coverage()
result = command_status.human_time(timedelta(minutes=15))
print(result)
command_status.report_coverage()

def test_seconds_branch(command_status):
command_status.reset_coverage()
result = command_status.human_time(timedelta(seconds=0))
print(result)
command_status.report_coverage()