OLD | NEW |
1 import os | 1 import os |
2 import sys | 2 import sys |
3 import time | 3 import time |
4 import runpy | 4 import runpy |
5 import signal | 5 import signal |
6 import pytest | 6 import pytest |
7 import urllib2 | 7 import urllib2 |
8 import subprocess | 8 import subprocess |
9 from conftest import ROOTPATH | 9 from conftest import ROOTPATH |
10 | 10 |
11 | 11 |
12 def get_dir_contents(path): | 12 def get_dir_contents(path): |
13 return_data = {} | 13 return_data = {} |
14 for dirpath, dirnames, filenames in os.walk(path): | 14 for dirpath, dirnames, filenames in os.walk(path): |
15 for output_file in filenames: | 15 for output_file in filenames: |
16 with open(os.path.join(dirpath, output_file)) as f: | 16 with open(os.path.join(dirpath, output_file)) as f: |
17 return_data[output_file] = f.read() | 17 return_data[output_file] = f.read().strip() |
18 return return_data | 18 return return_data |
19 | 19 |
20 | 20 |
21 def get_expected_outputs(): | 21 def get_expected_outputs(test_type): |
22 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 22 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
23 return get_dir_contents(expected_out_path).items() | 23 outputs = get_dir_contents(expected_out_path) |
| 24 for filename in list(outputs): |
| 25 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") |
| 26 # and remove the expected outputs that don't apply for this test type. |
| 27 if filename.endswith('@' + test_type): |
| 28 realname = filename.split('@')[0] |
| 29 outputs[realname] = outputs[filename] |
| 30 if '@' in filename: |
| 31 del outputs[filename] |
| 32 return outputs.items() |
24 | 33 |
25 expected_outputs = get_expected_outputs() | 34 |
| 35 static_expected_outputs = get_expected_outputs('static') |
| 36 dynamic_expected_outputs = get_expected_outputs('dynamic') |
26 | 37 |
27 | 38 |
28 @pytest.fixture(scope='session', params=['master', None]) | 39 @pytest.fixture(scope='session', params=['master', None]) |
29 def revision(request): | 40 def revision(request): |
30 return request.param | 41 return request.param |
31 | 42 |
32 | 43 |
33 @pytest.fixture(scope='session') | 44 @pytest.fixture(scope='session') |
34 def static_output(revision, request, temp_site): | 45 def static_output(revision, request, temp_site): |
35 static_out_path = os.path.join(temp_site, 'static_out') | 46 static_out_path = os.path.join(temp_site, 'static_out') |
36 sys.argv = ['filler', temp_site, static_out_path] | 47 sys.argv = ['filler', temp_site, static_out_path] |
37 if revision is not None: | 48 if revision is not None: |
38 sys.argv += ['--rev', revision] | 49 sys.argv += ['--rev', revision] |
39 | 50 |
40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 51 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
41 return static_out_path | 52 return static_out_path |
42 | 53 |
43 | 54 |
44 @pytest.yield_fixture() | 55 @pytest.yield_fixture() |
45 def dynamic_server(temp_site): | 56 def dynamic_server(temp_site): |
46 args = ['python', 'runserver.py', temp_site] | 57 args = ['python', 'runserver.py', temp_site] |
47 # Werkzeug is a dependency of flask which we are using for the mock api | 58 # Werkzeug is a dependency of flask which we are using for the mock api |
48 # however there is an issue with Werkzeug that prevents it from properly | 59 # however there is an issue with Werkzeug that prevents it from properly |
49 # handling the SIGTERM sent by p.kill() or terminate() | 60 # handling the SIGTERM sent by p.kill() or terminate() |
50 # Issue: https://github.com/pallets/werkzeug/issues/58 | 61 # Issue: https://github.com/pallets/werkzeug/issues/58 |
51 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) | 62 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) |
52 time.sleep(0.5) | 63 time.sleep(0.5) |
53 yield 'http://localhost:5000/root/' | 64 yield 'http://localhost:5000/en/' |
54 os.killpg(os.getpgid(p.pid), signal.SIGTERM) | 65 os.killpg(os.getpgid(p.pid), signal.SIGTERM) |
55 | 66 |
56 | 67 |
57 @pytest.fixture(scope='session') | 68 @pytest.fixture(scope='session') |
58 def output_pages(static_output): | 69 def output_pages(static_output): |
59 return get_dir_contents(static_output) | 70 return get_dir_contents(static_output) |
60 | 71 |
61 | 72 |
62 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 73 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
63 def test_static(output_pages, filename, expected_output): | 74 def test_static(output_pages, filename, expected_output): |
64 assert output_pages[filename] == expected_output | 75 assert output_pages[filename] == expected_output |
65 | 76 |
66 | 77 |
67 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 78 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
68 def test_dynamic(dynamic_server, filename, expected_output): | 79 def test_dynamic(dynamic_server, filename, expected_output): |
69 response = urllib2.urlopen(dynamic_server + filename) | 80 response = urllib2.urlopen(dynamic_server + filename) |
70 assert response.read() == expected_output | 81 assert response.read().strip() == expected_output |
71 | 82 |
72 | 83 |
73 def test_revision_arg(revision, output_pages): | 84 def test_revision_arg(revision, output_pages): |
74 if revision is None: | 85 if revision is None: |
75 assert 'bar' in output_pages | 86 assert 'bar' in output_pages |
76 else: | 87 else: |
77 assert 'bar' not in output_pages | 88 assert 'bar' not in output_pages |
OLD | NEW |