Skip to content

Commit

Permalink
Add -l/--loglevel option
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Nov 28, 2017
1 parent 79a4df5 commit 2bf7967
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
9 changes: 7 additions & 2 deletions covimerage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
@click.version_option(__version__, '-V', '--version', prog_name='covimerage')
@click.option('-v', '--verbose', count=True, help='Increase verbosity.')
@click.option('-q', '--quiet', count=True, help='Decrease verbosity.')
def main(verbose, quiet):
if verbose - quiet:
@click.option('-l', '--loglevel',
help='Set logging level explicitly (overrides -v/-q).',
type=click.Choice(('error', 'warning', 'info', 'debug')))
def main(verbose, quiet, loglevel):
if loglevel:
LOGGER.setLevel(loglevel.upper())
elif verbose - quiet:
LOGGER.setLevel(max(10, LOGGER.level - (verbose - quiet) * 10))


Expand Down
39 changes: 39 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,42 @@ def test_logging_error_causes_exception(capfd):
"Message: 'Wrong:'",
"Arguments: ('no %s',)"]
assert 'TypeError: not all arguments converted during string formatting' in lines # noqa: E501


def test_loglevel(mocker, runner, devnull):
from covimerage import cli
import logging

logger = cli.LOGGER

m = mocker.spy(logger, 'setLevel')

for level in ['error', 'warning', 'info', 'debug']:
result = runner.invoke(cli.main, [
'--loglevel', level,
'report', '--nonexistingoption'])
assert result.output.splitlines() == [
'Error: no such option: --nonexistingoption']
assert result.exit_code == 2

level_name = level.upper()
assert m.call_args_list[-1] == mocker.call(level_name)
assert logging.getLevelName(logger.level) == level_name

# -v should not override -l.
result = runner.invoke(cli.main, [
'-l', 'warning', '-vvv',
'report', '--nonexistingoption'])
assert result.output.splitlines() == [
'Error: no such option: --nonexistingoption']
assert result.exit_code == 2
assert logging.getLevelName(logger.level) == 'WARNING'

# -q should not override -l.
result = runner.invoke(cli.main, [
'-l', 'warning', '-qqq',
'report', '--nonexistingoption'])
assert result.output.splitlines() == [
'Error: no such option: --nonexistingoption']
assert result.exit_code == 2
assert logging.getLevelName(logger.level) == 'WARNING'

0 comments on commit 2bf7967

Please sign in to comment.