LEFT | RIGHT |
1 import os | 1 import os |
2 import sys | 2 import sys |
3 import runpy | 3 import runpy |
4 | 4 |
5 import mock | 5 import mock |
6 import pytest | 6 import pytest |
7 | 7 |
8 from .conftest import ROOTPATH | 8 from .conftest import ROOTPATH |
9 from .utils import get_dir_contents, exception_test | 9 from .utils import get_dir_contents, exception_test |
10 from cms.sources import FileSource | 10 from cms.sources import FileSource |
11 from cms.bin.test_server import DynamicServerHandler | 11 from cms.bin.test_server import DynamicServerHandler |
12 | 12 |
13 | 13 |
14 def get_expected_outputs(test_type): | 14 def get_expected_outputs(test_type, with_common=True): |
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 15 root = os.path.join(ROOTPATH, 'tests', 'expected_output') |
16 outputs = get_dir_contents(expected_out_path, True) | 16 outputs = {} |
17 result = {} | 17 if with_common: |
18 for filename in list(outputs): | 18 outputs = get_dir_contents(os.path.join(root, 'common')) |
19 ttype = os.path.split(os.path.split(filename)[0])[1] | 19 outputs.update(get_dir_contents(os.path.join(root, test_type))) |
20 if ttype == test_type or ttype == 'common': | 20 return outputs.items() |
21 result[filename] = outputs[filename] | |
22 return result.items() | |
23 | 21 |
24 | 22 |
25 static_expected_outputs = get_expected_outputs('static') | 23 static_expected_outputs = get_expected_outputs('static') |
26 dynamic_expected_outputs = get_expected_outputs('dynamic') | 24 dynamic_expected_outputs = get_expected_outputs('dynamic') |
27 relative_expected_outputs = get_expected_outputs('relative') | 25 relative_expected_outputs = get_expected_outputs('relative', False) |
28 | 26 |
29 | 27 |
30 def generate_static_pages(temp_site, tmpdir_factory, *extra_args): | 28 def generate_static_pages(temp_site, tmpdir_factory, *extra_args): |
31 dst_path = str(tmpdir_factory.mktemp('static')) | 29 dst_path = str(tmpdir_factory.mktemp('static')) |
32 sys.argv = ['filler', temp_site, dst_path] + list(extra_args) | 30 sys.argv = ['filler', temp_site, dst_path] + list(extra_args) |
33 with mock.patch('cms.sources.FileSource.version', 1): | 31 with mock.patch('cms.sources.FileSource.version', 1): |
34 runpy.run_module('cms.bin.generate_static_pages', | 32 runpy.run_module('cms.bin.generate_static_pages', |
35 run_name='__main__') | 33 run_name='__main__') |
36 return get_dir_contents(dst_path) | 34 return get_dir_contents(dst_path) |
37 | 35 |
38 | 36 |
39 @pytest.fixture(scope='session') | 37 @pytest.fixture(scope='session') |
40 def output_pages(temp_site, tmpdir_factory): | 38 def output_pages(temp_site, tmpdir_factory): |
41 return generate_static_pages(temp_site, tmpdir_factory) | 39 return generate_static_pages(temp_site, tmpdir_factory) |
42 | 40 |
43 | 41 |
44 @pytest.fixture(scope='session') | 42 @pytest.fixture(scope='session') |
45 def output_pages_relative(temp_site, tmpdir_factory): | 43 def output_pages_relative(temp_site, tmpdir_factory): |
46 return generate_static_pages(temp_site, tmpdir_factory, '--relative') | 44 return generate_static_pages(temp_site, tmpdir_factory, '--relative') |
47 | 45 |
48 | 46 |
49 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 47 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
50 def test_static(output_pages, filename, expected_output): | 48 def test_static(output_pages, filename, expected_output): |
51 filename = os.path.join(os.path.split(os.path.split(filename)[0])[0], | |
52 os.path.split(filename)[1]) | |
53 if expected_output.startswith('## MISSING'): | 49 if expected_output.startswith('## MISSING'): |
54 assert filename not in output_pages | 50 assert filename not in output_pages |
55 else: | 51 else: |
56 assert expected_output == output_pages[filename] | 52 assert expected_output == output_pages[filename] |
57 | 53 |
58 | 54 |
59 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) | 55 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) |
60 def test_static_relative(output_pages_relative, filename, expected_output): | 56 def test_static_relative(output_pages_relative, filename, expected_output): |
61 filename = os.path.join(os.path.split(os.path.split(filename)[0])[0], | |
62 os.path.split(filename)[1]) | |
63 if expected_output.startswith('## MISSING'): | 57 if expected_output.startswith('## MISSING'): |
64 assert filename not in output_pages_relative | 58 assert filename not in output_pages_relative |
65 else: | 59 else: |
66 assert expected_output == output_pages_relative[filename] | 60 assert expected_output == output_pages_relative[filename] |
67 | 61 |
68 | 62 |
69 def test_cache(output_pages): | 63 def test_cache(output_pages): |
70 source = FileSource(os.path.join('test_site')) | 64 source = FileSource(os.path.join('test_site')) |
71 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 65 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
72 | 66 |
73 | 67 |
74 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 68 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
75 def test_dynamic_server_handler(filename, expected_output, temp_site): | 69 def test_dynamic_server_handler(filename, expected_output, temp_site): |
76 filename = os.path.join(os.path.split(os.path.split(filename)[0])[0], | |
77 os.path.split(filename)[1]) | |
78 | 70 |
79 def cleanup(page): | 71 def cleanup(page): |
80 return page.replace(os.linesep, '').strip() | 72 return page.replace(os.linesep, '').strip() |
81 | 73 |
82 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 74 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
83 environ = {'PATH_INFO': filename} | 75 environ = {'PATH_INFO': filename} |
84 | 76 |
85 generated_page = handler(environ, lambda x, y: None) | 77 generated_page = handler(environ, lambda x, y: None) |
86 | 78 |
87 assert cleanup(expected_output) == cleanup(generated_page[0]) | 79 assert cleanup(expected_output) == cleanup(generated_page[0]) |
88 | 80 |
89 | 81 |
90 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 82 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
91 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): | 83 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
92 handler = DynamicServerHandler('localhost', 5000, | 84 handler = DynamicServerHandler('localhost', 5000, |
93 str(temp_site_with_conflicts)) | 85 str(temp_site_with_conflicts)) |
94 environ = {'PATH_INFO': page} | 86 environ = {'PATH_INFO': page} |
95 exp_msg = 'The requested page conflicts with another page.' | 87 exp_msg = 'The requested page conflicts with another page.' |
96 | 88 |
97 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 89 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) |
LEFT | RIGHT |