mirror of
https://github.com/openSUSE/osc.git
synced 2025-03-01 05:32:13 +01:00
Extend output.print_msg() to accept 'error' and 'warning' values of 'to_print' argument
This commit is contained in:
parent
866d178345
commit
88f7dc517b
@ -1,7 +1,20 @@
|
|||||||
import sys
|
import sys
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from . import tty
|
||||||
|
|
||||||
|
|
||||||
def print_msg(*args, print_to="debug"):
|
def print_msg(*args, print_to: Optional[str] = "debug"):
|
||||||
|
"""
|
||||||
|
Print ``*args`` to the ``print_to`` target:
|
||||||
|
- None: print nothing
|
||||||
|
- debug: print() to stderr with "DEBUG:" prefix if config["debug"] is set
|
||||||
|
- verbose: print() to stdout if config["verbose"] or config["debug"] is set
|
||||||
|
- error: print() to stderr with red "ERROR:" prefix
|
||||||
|
- warning: print() to stderr with yellow "WARNING:" prefix
|
||||||
|
- stdout: print() to stdout
|
||||||
|
- stderr: print() to stderr
|
||||||
|
"""
|
||||||
from .. import conf
|
from .. import conf
|
||||||
|
|
||||||
if print_to is None:
|
if print_to is None:
|
||||||
@ -14,6 +27,10 @@ def print_msg(*args, print_to="debug"):
|
|||||||
# print a verbose message to stdout if config["verbose"] or config["debug"] is set
|
# print a verbose message to stdout if config["verbose"] or config["debug"] is set
|
||||||
if conf.config["verbose"] or conf.config["debug"]:
|
if conf.config["verbose"] or conf.config["debug"]:
|
||||||
print(*args)
|
print(*args)
|
||||||
|
elif print_to == "error":
|
||||||
|
print(tty.colorize("ERROR:", "red,bold"), *args, file=sys.stderr)
|
||||||
|
elif print_to == "warning":
|
||||||
|
print(tty.colorize("WARNING:", "yellow,bold"), *args, file=sys.stderr)
|
||||||
elif print_to == "stdout":
|
elif print_to == "stdout":
|
||||||
# print the message to stdout
|
# print the message to stdout
|
||||||
print(*args)
|
print(*args)
|
||||||
@ -22,5 +39,3 @@ def print_msg(*args, print_to="debug"):
|
|||||||
print(*args, file=sys.stderr)
|
print(*args, file=sys.stderr)
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid value of the 'print_to' option: {print_to}")
|
raise ValueError(f"Invalid value of the 'print_to' option: {print_to}")
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import unittest
|
|||||||
import osc.conf
|
import osc.conf
|
||||||
from osc.output import KeyValueTable
|
from osc.output import KeyValueTable
|
||||||
from osc.output import print_msg
|
from osc.output import print_msg
|
||||||
|
from osc.output import tty
|
||||||
|
|
||||||
|
|
||||||
class TestKeyValueTable(unittest.TestCase):
|
class TestKeyValueTable(unittest.TestCase):
|
||||||
@ -118,6 +119,22 @@ class TestPrintMsg(unittest.TestCase):
|
|||||||
self.assertEqual("foo bar\n", stdout.getvalue())
|
self.assertEqual("foo bar\n", stdout.getvalue())
|
||||||
self.assertEqual("", stderr.getvalue())
|
self.assertEqual("", stderr.getvalue())
|
||||||
|
|
||||||
|
def test_error(self):
|
||||||
|
stdout = io.StringIO()
|
||||||
|
stderr = io.StringIO()
|
||||||
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
||||||
|
print_msg("foo", "bar", print_to="error")
|
||||||
|
self.assertEqual("", stdout.getvalue())
|
||||||
|
self.assertEqual(f"{tty.colorize('ERROR:', 'red,bold')} foo bar\n", stderr.getvalue())
|
||||||
|
|
||||||
|
def test_warning(self):
|
||||||
|
stdout = io.StringIO()
|
||||||
|
stderr = io.StringIO()
|
||||||
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
||||||
|
print_msg("foo", "bar", print_to="warning")
|
||||||
|
self.assertEqual("", stdout.getvalue())
|
||||||
|
self.assertEqual(f"{tty.colorize('WARNING:', 'yellow,bold')} foo bar\n", stderr.getvalue())
|
||||||
|
|
||||||
def test_none(self):
|
def test_none(self):
|
||||||
stdout = io.StringIO()
|
stdout = io.StringIO()
|
||||||
stderr = io.StringIO()
|
stderr = io.StringIO()
|
||||||
@ -134,6 +151,14 @@ class TestPrintMsg(unittest.TestCase):
|
|||||||
self.assertEqual("foo bar\n", stdout.getvalue())
|
self.assertEqual("foo bar\n", stdout.getvalue())
|
||||||
self.assertEqual("", stderr.getvalue())
|
self.assertEqual("", stderr.getvalue())
|
||||||
|
|
||||||
|
def test_stderr(self):
|
||||||
|
stdout = io.StringIO()
|
||||||
|
stderr = io.StringIO()
|
||||||
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
||||||
|
print_msg("foo", "bar", print_to="stderr")
|
||||||
|
self.assertEqual("", stdout.getvalue())
|
||||||
|
self.assertEqual("foo bar\n", stderr.getvalue())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user