|
| 1 | +import unittest |
| 2 | +import os, sys, shutil |
| 3 | + |
| 4 | +from roundup.demo import install_demo, run_demo |
| 5 | + |
| 6 | +import roundup.scripts.roundup_server |
| 7 | + |
| 8 | +# https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python |
| 9 | +# lightly modified |
| 10 | +from contextlib import contextmanager |
| 11 | +_py3 = sys.version_info[0] > 2 |
| 12 | +if _py3: |
| 13 | + from io import StringIO # py3 |
| 14 | +else: |
| 15 | + from StringIO import StringIO # py2 |
| 16 | +@contextmanager |
| 17 | +def captured_output(): |
| 18 | + new_out, new_err = StringIO(), StringIO() |
| 19 | + old_out, old_err = sys.stdout, sys.stderr |
| 20 | + try: |
| 21 | + sys.stdout, sys.stderr = new_out, new_err |
| 22 | + yield sys.stdout, sys.stderr |
| 23 | + finally: |
| 24 | + sys.stdout, sys.stderr = old_out, old_err |
| 25 | + |
| 26 | +class TestDemo(unittest.TestCase): |
| 27 | + def setUp(self): |
| 28 | + self.home = os.path.abspath('_test_demo') |
| 29 | + |
| 30 | + def tearDown(self): |
| 31 | + try: |
| 32 | + shutil.rmtree(self.home) |
| 33 | + except FileNotFoundError: |
| 34 | + pass |
| 35 | + |
| 36 | + def testDemo(self): |
| 37 | + with captured_output() as (out, err): |
| 38 | + install_demo(self.home, 'anydbm', 'classic') |
| 39 | + output = out.getvalue().strip() |
| 40 | + print(output) |
| 41 | + |
| 42 | + # dummy up the return of get_server so the serve_forever method |
| 43 | + # raises keyboard interrupt exiting the server so the test exits. |
| 44 | + gs = roundup.scripts.roundup_server.ServerConfig.get_server |
| 45 | + def raise_KeyboardInterrupt(): |
| 46 | + raise KeyboardInterrupt |
| 47 | + |
| 48 | + def test_get_server(self): |
| 49 | + httpd = gs(self) |
| 50 | + httpd.serve_forever = raise_KeyboardInterrupt |
| 51 | + return httpd |
| 52 | + |
| 53 | + roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server |
| 54 | + |
| 55 | + # Run under context manager to capture output of startup text. |
| 56 | + with captured_output() as (out, err): |
| 57 | + run_demo(self.home) |
| 58 | + output = out.getvalue().strip() |
| 59 | + print(output) |
| 60 | + # if the server installed and started this will be the |
| 61 | + # last line in the output. |
| 62 | + self.assertIn("Keyboard Interrupt: exiting", output.split('\n')) |
| 63 | + |
| 64 | + |
0 commit comments