2023-07-27 09:35:24 +02:00
|
|
|
import contextlib
|
|
|
|
import io
|
2023-05-30 09:06:49 +02:00
|
|
|
import unittest
|
|
|
|
|
2023-07-27 09:35:24 +02:00
|
|
|
import osc.conf
|
|
|
|
from osc._private import print_msg
|
2023-05-30 09:06:49 +02:00
|
|
|
from osc.output import KeyValueTable
|
|
|
|
|
|
|
|
|
|
|
|
class TestKeyValueTable(unittest.TestCase):
|
|
|
|
def test_empty(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
self.assertEqual(str(t), "")
|
|
|
|
|
|
|
|
def test_simple(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
t.add("Key", "Value")
|
|
|
|
t.add("FooBar", "Text")
|
|
|
|
|
|
|
|
expected = """
|
|
|
|
Key : Value
|
|
|
|
FooBar : Text
|
|
|
|
""".strip()
|
|
|
|
self.assertEqual(str(t), expected)
|
|
|
|
|
|
|
|
def test_newline(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
t.add("Key", "Value")
|
|
|
|
t.newline()
|
|
|
|
t.add("FooBar", "Text")
|
|
|
|
|
|
|
|
expected = """
|
|
|
|
Key : Value
|
|
|
|
|
|
|
|
FooBar : Text
|
|
|
|
""".strip()
|
|
|
|
self.assertEqual(str(t), expected)
|
|
|
|
|
|
|
|
def test_continuation(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
t.add("Key", ["Value1", "Value2"])
|
|
|
|
|
|
|
|
expected = """
|
|
|
|
Key : Value1
|
|
|
|
Value2
|
|
|
|
""".strip()
|
|
|
|
self.assertEqual(str(t), expected)
|
|
|
|
|
|
|
|
def test_section(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
t.add("Section", None)
|
|
|
|
t.add("Key", "Value", indent=4)
|
|
|
|
t.add("FooBar", "Text", indent=4)
|
|
|
|
|
|
|
|
expected = """
|
|
|
|
Section
|
|
|
|
Key : Value
|
|
|
|
FooBar : Text
|
|
|
|
""".strip()
|
|
|
|
self.assertEqual(str(t), expected)
|
|
|
|
|
|
|
|
def test_wide_chars(self):
|
|
|
|
t = KeyValueTable()
|
|
|
|
t.add("Key", "Value")
|
|
|
|
t.add("🚀🚀🚀", "Value")
|
|
|
|
|
|
|
|
expected = """
|
|
|
|
Key : Value
|
|
|
|
🚀🚀🚀 : Value
|
|
|
|
""".strip()
|
|
|
|
self.assertEqual(str(t), expected)
|
|
|
|
|
|
|
|
|
2023-07-27 09:35:24 +02:00
|
|
|
class TestPrintMsg(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2024-01-03 21:21:40 +01:00
|
|
|
osc.conf.config = osc.conf.Options()
|
2023-07-27 09:35:24 +02:00
|
|
|
|
|
|
|
def test_debug(self):
|
2023-08-22 15:34:45 +02:00
|
|
|
osc.conf.config["debug"] = False
|
2023-07-27 09:35:24 +02:00
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="debug")
|
|
|
|
self.assertEqual("", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
2023-08-22 15:34:45 +02:00
|
|
|
osc.conf.config["debug"] = True
|
2023-07-27 09:35:24 +02:00
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="debug")
|
|
|
|
self.assertEqual("", stdout.getvalue())
|
|
|
|
self.assertEqual("DEBUG: foo bar\n", stderr.getvalue())
|
|
|
|
|
|
|
|
def test_verbose(self):
|
2023-08-22 15:34:45 +02:00
|
|
|
osc.conf.config["verbose"] = False
|
2023-07-27 09:35:24 +02:00
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="verbose")
|
|
|
|
self.assertEqual("", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
2023-08-22 15:34:45 +02:00
|
|
|
osc.conf.config["verbose"] = True
|
2023-07-27 09:35:24 +02:00
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="verbose")
|
|
|
|
self.assertEqual("foo bar\n", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
2023-08-22 15:34:45 +02:00
|
|
|
osc.conf.config["verbose"] = False
|
|
|
|
osc.conf.config["debug"] = True
|
2023-07-27 09:35:24 +02:00
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="verbose")
|
|
|
|
self.assertEqual("foo bar\n", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
|
|
|
def test_none(self):
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to=None)
|
|
|
|
self.assertEqual("", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
|
|
|
def test_stdout(self):
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
|
|
print_msg("foo", "bar", print_to="stdout")
|
|
|
|
self.assertEqual("foo bar\n", stdout.getvalue())
|
|
|
|
self.assertEqual("", stderr.getvalue())
|
|
|
|
|
|
|
|
|
2023-05-30 09:06:49 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|