Mercurial > p > roundup > code
annotate test/test_demo.py @ 7586:859c57bc8d91
test: limit search for \r to first 100 bytes.
Don't scan entire file. just look at 100 bytes which should include
all of the first line.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 24 Jul 2023 21:24:07 -0400 |
| parents | 227aca44fea5 |
| children | 5b1163dca9e1 |
| rev | line source |
|---|---|
|
7583
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
1 import pytest |
|
6324
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 import unittest |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 import os, sys, shutil |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 from roundup.demo import install_demo, run_demo |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 import roundup.scripts.roundup_server |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 # lightly modified |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 from contextlib import contextmanager |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 _py3 = sys.version_info[0] > 2 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 if _py3: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 from io import StringIO # py3 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 else: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 from StringIO import StringIO # py2 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 @contextmanager |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 def captured_output(): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 new_out, new_err = StringIO(), StringIO() |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 old_out, old_err = sys.stdout, sys.stderr |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 try: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 sys.stdout, sys.stderr = new_out, new_err |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 yield sys.stdout, sys.stderr |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 finally: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 sys.stdout, sys.stderr = old_out, old_err |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 |
|
7583
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
27 try: |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
28 import jinja2 |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
29 skip_jinja2 = lambda func, *args, **kwargs: func |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
30 except ImportError: |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
31 from .pytest_patcher import mark_class |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
32 skip_jinja2 = mark_class(pytest.mark.skip( |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
33 reason='Skipping Jinja2 tests: jinja2 library not available')) |
|
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
34 |
|
6324
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
35 class TestDemo(unittest.TestCase): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
36 def setUp(self): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
37 self.home = os.path.abspath('_test_demo') |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
38 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
39 def tearDown(self): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
40 try: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
41 shutil.rmtree(self.home) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
42 except FileNotFoundError: |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
43 pass |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
44 |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
45 def run_install_demo(self, template, db="anydbm"): |
|
6324
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
46 with captured_output() as (out, err): |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
47 install_demo(self.home, db, template) |
|
6324
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
48 output = out.getvalue().strip() |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
49 print(output) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
50 |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
51 # verify that db was set properly by reading config |
|
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
52 with open(self.home + "/config.ini", "r") as f: |
|
7585
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
53 config_lines = f.read().replace("\r\n", "\n") |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
54 |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
55 try: |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
56 # handle text files with \r\n line endings |
|
7586
859c57bc8d91
test: limit search for \r to first 100 bytes.
John Rouillard <rouilj@ieee.org>
parents:
7585
diff
changeset
|
57 config_lines.index("\r", 0, 100) |
|
7585
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
58 config_lines = config_lines.replace("\r\n", "\n") |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
59 except ValueError: |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
60 pass |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
61 |
|
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
62 self.assertIn("backend = %s\n"%db, config_lines) |
|
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
63 |
|
6324
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
64 # dummy up the return of get_server so the serve_forever method |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
65 # raises keyboard interrupt exiting the server so the test exits. |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
66 gs = roundup.scripts.roundup_server.ServerConfig.get_server |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
67 def raise_KeyboardInterrupt(): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
68 raise KeyboardInterrupt |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
69 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
70 def test_get_server(self): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
71 httpd = gs(self) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
72 httpd.serve_forever = raise_KeyboardInterrupt |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
73 return httpd |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
74 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
75 roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
76 |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
77 # Run under context manager to capture output of startup text. |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
78 with captured_output() as (out, err): |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
79 run_demo(self.home) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
80 output = out.getvalue().strip() |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
81 print(output) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
82 # if the server installed and started this will be the |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
83 # last line in the output. |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
84 self.assertIn("Keyboard Interrupt: exiting", output.split('\n')) |
|
3e33b22a3158
BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
85 |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
86 def testDemoClassic(self): |
|
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
87 self.run_install_demo("classic") |
|
6545
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
88 |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
89 def testDemoMinimal(self): |
|
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
90 self.run_install_demo('../templates/minimal', db="sqlite") |
|
6545
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
91 |
|
7583
4cfaddc2d53e
test: do not run jinja2 demo test if jinja2 missing.
John Rouillard <rouilj@ieee.org>
parents:
6719
diff
changeset
|
92 @skip_jinja2 |
|
6545
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
93 def testDemoJinja(self): |
|
6719
77eb1a41fc06
test cleanup. Make test method and convert tests to use.
John Rouillard <rouilj@ieee.org>
parents:
6545
diff
changeset
|
94 self.run_install_demo('jinja2', db="anydbm") |
|
6545
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
95 |
|
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
96 # verify that template was set to jinja2 by reading config |
|
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
97 with open(self.home + "/config.ini", "r") as f: |
|
7585
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
98 config_lines = f.read() |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
99 |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
100 try: |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
101 # handle text files with \r\n line endings |
|
7586
859c57bc8d91
test: limit search for \r to first 100 bytes.
John Rouillard <rouilj@ieee.org>
parents:
7585
diff
changeset
|
102 config_lines.index("\r", 0, 100) |
|
7585
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
103 config_lines = config_lines.replace("\r\n", "\n") |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
104 except ValueError: |
|
227aca44fea5
test: fix failure under cygwin python caused by line endings
John Rouillard <rouilj@ieee.org>
parents:
7583
diff
changeset
|
105 pass |
|
6545
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
106 |
|
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
107 self.assertIn("template_engine = jinja2\n", config_lines) |
|
5a3a386aa8e7
issue2551179 Load config_ini.ini ... recognize minimal template demo.py
John Rouillard <rouilj@ieee.org>
parents:
6324
diff
changeset
|
108 |
