-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: fix bug in the
CliFormatter
log utility
The `format` method assumed that the `prefix` attribute would always exist, which is not always this case. This would result in an uncaught exception when a message would get logged. This went unnoticed since the utility was not individually tested. Additionally, messages with literal percentage signs would also except because it would always be interpolated with the `record.args` but if that was an empty tuple, a `TypeError` would be raised.
- Loading branch information
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Tests for the :mod:`aiida.cmdline.utils.log` module.""" | ||
import logging | ||
|
||
from aiida.cmdline.utils import log | ||
|
||
|
||
def test_cli_formatter(): | ||
"""Test the ``CliFormatter.format`` method for a plain message. | ||
Note that if it contains percentage signs but no arguments, it should not try to convert it. | ||
""" | ||
message = 'message' | ||
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), 'exc_info') | ||
assert log.CliFormatter.format(record) == message | ||
|
||
|
||
def test_cli_formatter_no_args(): | ||
"""Test the ``CliFormatter.format`` method for a message with percentage signs but no args.""" | ||
message = 'PID MEM % CPU % started' | ||
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, message, (), 'exc_info') | ||
assert log.CliFormatter.format(record) == message | ||
|
||
|
||
def test_cli_formatter_args(): | ||
"""Test the ``CliFormatter.format`` method for a message with a single argument.""" | ||
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), 'exc_info') | ||
assert log.CliFormatter.format(record) == 'Some value' | ||
|
||
|
||
def test_cli_formatter_prefix(): | ||
"""Test the ``CliFormatter.format`` method for a message with a single argument.""" | ||
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), 'exc_info') | ||
record.prefix = True | ||
assert log.CliFormatter.format(record) == '\x1b[34m\x1b[1mInfo\x1b[0m: Some value' |