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 if filename.endswith('@' + test_type): |
| 26 realname = filename.split('@')[0] |
| 27 outputs[realname] = outputs[filename] |
| 28 if '@' in filename: |
| 29 del outputs[filename] |
| 30 return outputs.items() |
24 | 31 |
25 expected_outputs = get_expected_outputs() | 32 |
| 33 static_expected_outputs = get_expected_outputs('static') |
| 34 dynamic_expected_outputs = get_expected_outputs('dynamic') |
26 | 35 |
27 | 36 |
28 @pytest.fixture(scope='session', params=['master', None]) | 37 @pytest.fixture(scope='session', params=['master', None]) |
29 def revision(request): | 38 def revision(request): |
30 return request.param | 39 return request.param |
31 | 40 |
32 | 41 |
33 @pytest.fixture(scope='session') | 42 @pytest.fixture(scope='session') |
34 def static_output(revision, request, temp_site): | 43 def static_output(revision, request, temp_site): |
35 static_out_path = os.path.join(temp_site, 'static_out') | 44 static_out_path = os.path.join(temp_site, 'static_out') |
36 sys.argv = ['filler', temp_site, static_out_path] | 45 sys.argv = ['filler', temp_site, static_out_path] |
37 if revision is not None: | 46 if revision is not None: |
38 sys.argv += ['--rev', revision] | 47 sys.argv += ['--rev', revision] |
39 | 48 |
40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 49 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
41 return static_out_path | 50 return static_out_path |
42 | 51 |
43 | 52 |
44 @pytest.yield_fixture() | 53 @pytest.yield_fixture() |
45 def dynamic_server(temp_site): | 54 def dynamic_server(temp_site): |
46 args = ['python', 'runserver.py', temp_site] | 55 args = ['python', 'runserver.py', temp_site] |
47 # Werkzeug is a dependency of flask which we are using for the mock api | 56 # 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 | 57 # however there is an issue with Werkzeug that prevents it from properly |
49 # handling the SIGTERM sent by p.kill() or terminate() | 58 # handling the SIGTERM sent by p.kill() or terminate() |
50 # Issue: https://github.com/pallets/werkzeug/issues/58 | 59 # Issue: https://github.com/pallets/werkzeug/issues/58 |
51 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) | 60 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) |
52 time.sleep(0.5) | 61 time.sleep(0.5) |
53 yield 'http://localhost:5000/root/' | 62 yield 'http://localhost:5000/en/' |
54 os.killpg(os.getpgid(p.pid), signal.SIGTERM) | 63 os.killpg(os.getpgid(p.pid), signal.SIGTERM) |
55 | 64 |
56 | 65 |
57 @pytest.fixture(scope='session') | 66 @pytest.fixture(scope='session') |
58 def output_pages(static_output): | 67 def output_pages(static_output): |
59 return get_dir_contents(static_output) | 68 return get_dir_contents(static_output) |
60 | 69 |
61 | 70 |
62 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 71 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
63 def test_static(output_pages, filename, expected_output): | 72 def test_static(output_pages, filename, expected_output): |
64 assert output_pages[filename] == expected_output | 73 assert output_pages[filename] == expected_output |
65 | 74 |
66 | 75 |
67 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 76 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
68 def test_dynamic(dynamic_server, filename, expected_output): | 77 def test_dynamic(dynamic_server, filename, expected_output): |
69 response = urllib2.urlopen(dynamic_server + filename) | 78 response = urllib2.urlopen(dynamic_server + filename) |
70 assert response.read() == expected_output | 79 assert response.read().strip() == expected_output |
71 | 80 |
72 | 81 |
73 def test_revision_arg(revision, output_pages): | 82 def test_revision_arg(revision, output_pages): |
74 if revision is None: | 83 if revision is None: |
75 assert 'bar' in output_pages | 84 assert 'bar' in output_pages |
76 else: | 85 else: |
77 assert 'bar' not in output_pages | 86 assert 'bar' not in output_pages |
OLD | NEW |