From f9719ba87eaa6edfa6b7888695d94e2d19508e87 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Tue, 13 Apr 2021 16:32:43 +0200 Subject: [PATCH] tests --- tests/test_log.py | 54 +++++++++++++++++++++++++++++++++++++++++++ tests/test_thinner.py | 4 ++-- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/test_log.py diff --git a/tests/test_log.py b/tests/test_log.py new file mode 100644 index 0000000..55571e5 --- /dev/null +++ b/tests/test_log.py @@ -0,0 +1,54 @@ +import zfs_autobackup.LogConsole +from basetest import * + + +class TestLog(unittest2.TestCase): + + def test_colored(self): + """test with color output""" + with OutputIO() as buf: + with redirect_stdout(buf): + l=LogConsole(show_verbose=False, show_debug=False) + l.verbose("verbose") + l.debug("debug") + + with redirect_stdout(buf): + l=LogConsole(show_verbose=True, show_debug=True) + l.verbose("verbose") + l.debug("debug") + + with redirect_stderr(buf): + l=LogConsole() + l.error("error") + + print(list(buf.getvalue())) + self.assertEqual(list(buf.getvalue()), ['\x1b', '[', '2', '2', 'm', ' ', ' ', 'v', 'e', 'r', 'b', 'o', 's', 'e', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '2', 'm', '#', ' ', 'd', 'e', 'b', 'u', 'g', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '1', 'm', '\x1b', '[', '1', 'm', '!', ' ', 'e', 'r', 'r', 'o', 'r', '\x1b', '[', '0', 'm', '\n']) + + def test_nocolor(self): + """test without color output""" + + zfs_autobackup.LogConsole.colorama=False + + with OutputIO() as buf: + with redirect_stdout(buf): + l=LogConsole(show_verbose=False, show_debug=False) + l.verbose("verbose") + l.debug("debug") + + with redirect_stdout(buf): + l=LogConsole(show_verbose=True, show_debug=True) + l.verbose("verbose") + l.debug("debug") + + with redirect_stderr(buf): + l=LogConsole() + l.error("error") + + print(list(buf.getvalue())) + self.assertEqual(list(buf.getvalue()), [' ', ' ', 'v', 'e', 'r', 'b', 'o', 's', 'e', '\n', '#', ' ', 'd', 'e', 'b', 'u', 'g', '\n', '!', ' ', 'e', 'r', 'r', 'o', 'r', '\n']) + + + zfs_autobackup.LogConsole.colorama=False + + + diff --git a/tests/test_thinner.py b/tests/test_thinner.py index 775707c..9945c9c 100644 --- a/tests/test_thinner.py +++ b/tests/test_thinner.py @@ -138,5 +138,5 @@ class TestThinner(unittest2.TestCase): self.assertEqual(result, ok) -if __name__ == '__main__': - unittest.main() +# if __name__ == '__main__': +# unittest.main()