Rename timer; add a test for OutputContext.
authorScott Gasch <[email protected]>
Sat, 17 Jul 2021 04:43:11 +0000 (21:43 -0700)
committerScott Gasch <[email protected]>
Sat, 17 Jul 2021 04:43:11 +0000 (21:43 -0700)
stopwatch.py [moved from timer.py with 100% similarity]
tests/logging_utils_test.py [new file with mode: 0755]

similarity index 100%
rename from timer.py
rename to stopwatch.py
diff --git a/tests/logging_utils_test.py b/tests/logging_utils_test.py
new file mode 100755 (executable)
index 0000000..48e5015
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+import contextlib
+import tempfile
+import unittest
+
+import bootstrap
+import logging_utils as lutils
+import string_utils as sutils
+
+
+class TestLoggingUtils(unittest.TestCase):
+
+    def test_output_context(self):
+        unique_suffix = sutils.generate_uuid(True)
+        filename = f'/tmp/logging_utils_test.{unique_suffix}'
+        secret_message = f'This is a test, {unique_suffix}.'
+
+        with tempfile.SpooledTemporaryFile(mode='r+') as tmpfile1:
+            with tempfile.SpooledTemporaryFile(mode='r+') as tmpfile2:
+                with contextlib.redirect_stdout(tmpfile1):
+                    with lutils.OutputContext(
+                            lutils.OutputMultiplexer.Destination.FILENAME |
+                            lutils.OutputMultiplexer.Destination.FILEHANDLE |
+                            lutils.OutputMultiplexer.Destination.STDOUT,
+                            filename = filename,
+                            handle = tmpfile2,
+                    ) as mplex:
+                        mplex.print(secret_message, end='')
+                    with open(filename, 'r') as rf:
+                        self.assertEqual(rf.readline(), secret_message)
+                tmpfile2.seek(0)
+                tmp = tmpfile2.readline()
+                self.assertEqual(tmp, secret_message)
+            tmpfile1.seek(0)
+            tmp = tmpfile1.readline()
+            self.assertEqual(tmp, secret_message)
+
+
+if __name__ == '__main__':
+    unittest.main = bootstrap.initialize(unittest.main)
+    unittest.main()