OLD | NEW |
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): |
15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
16 outputs = get_dir_contents(expected_out_path) | 16 outputs = get_dir_contents(expected_out_path, True) |
| 17 result = {} |
17 for filename in list(outputs): | 18 for filename in list(outputs): |
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 19 ttype = os.path.split(os.path.split(filename)[0])[1] |
19 # There are cases where we need to test outputs which differ depending | 20 if ttype == test_type or ttype == 'common': |
20 # on how they are generated; either statically or dynamically | 21 result[filename] = outputs[filename] |
21 if filename.endswith('@' + test_type): | 22 return result.items() |
22 realname = filename.split('@')[0] | |
23 outputs[realname] = outputs[filename] | |
24 # Remove the expected outputs that don't apply for this test type. | |
25 if '@' in filename: | |
26 del outputs[filename] | |
27 return outputs.items() | |
28 | 23 |
29 | 24 |
30 static_expected_outputs = get_expected_outputs('static') | 25 static_expected_outputs = get_expected_outputs('static') |
31 dynamic_expected_outputs = get_expected_outputs('dynamic') | 26 dynamic_expected_outputs = get_expected_outputs('dynamic') |
| 27 relative_expected_outputs = get_expected_outputs('relative') |
| 28 |
| 29 |
| 30 def generate_static_pages(temp_site, tmpdir_factory, *extra_args): |
| 31 dst_path = str(tmpdir_factory.mktemp('static')) |
| 32 sys.argv = ['filler', temp_site, dst_path] + list(extra_args) |
| 33 with mock.patch('cms.sources.FileSource.version', 1): |
| 34 runpy.run_module('cms.bin.generate_static_pages', |
| 35 run_name='__main__') |
| 36 return get_dir_contents(dst_path) |
32 | 37 |
33 | 38 |
34 @pytest.fixture(scope='session') | 39 @pytest.fixture(scope='session') |
35 def static_output(request, temp_site): | 40 def output_pages(temp_site, tmpdir_factory): |
36 static_out_path = os.path.join(temp_site, 'static_out') | 41 return generate_static_pages(temp_site, tmpdir_factory) |
37 sys.argv = ['filler', temp_site, static_out_path] | |
38 with mock.patch('cms.sources.FileSource.version', 1): | |
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | |
40 return static_out_path | |
41 | 42 |
42 | 43 |
43 @pytest.fixture(scope='session') | 44 @pytest.fixture(scope='session') |
44 def output_pages(static_output): | 45 def output_pages_relative(temp_site, tmpdir_factory): |
45 return get_dir_contents(static_output) | 46 return generate_static_pages(temp_site, tmpdir_factory, '--relative') |
46 | 47 |
47 | 48 |
48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 49 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
49 def test_static(output_pages, filename, expected_output): | 50 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]) |
50 if expected_output.startswith('## MISSING'): | 53 if expected_output.startswith('## MISSING'): |
51 assert filename not in output_pages | 54 assert filename not in output_pages |
52 else: | 55 else: |
53 assert expected_output == output_pages[filename] | 56 assert expected_output == output_pages[filename] |
54 | 57 |
55 | 58 |
| 59 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) |
| 60 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'): |
| 64 assert filename not in output_pages_relative |
| 65 else: |
| 66 assert expected_output == output_pages_relative[filename] |
| 67 |
| 68 |
56 def test_cache(output_pages): | 69 def test_cache(output_pages): |
57 source = FileSource(os.path.join('test_site')) | 70 source = FileSource(os.path.join('test_site')) |
58 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 71 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
59 | 72 |
60 | 73 |
61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 74 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
62 def test_dynamic_server_handler(filename, expected_output, temp_site): | 75 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 |
63 def cleanup(page): | 79 def cleanup(page): |
64 return page.replace(os.linesep, '').strip() | 80 return page.replace(os.linesep, '').strip() |
65 | 81 |
66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 82 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
67 environ = {'PATH_INFO': filename} | 83 environ = {'PATH_INFO': filename} |
68 | 84 |
69 generated_page = handler(environ, lambda x, y: None) | 85 generated_page = handler(environ, lambda x, y: None) |
70 | 86 |
71 assert cleanup(expected_output) == cleanup(generated_page[0]) | 87 assert cleanup(expected_output) == cleanup(generated_page[0]) |
72 | 88 |
73 | 89 |
74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 90 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
75 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): | 91 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
76 handler = DynamicServerHandler('localhost', 5000, | 92 handler = DynamicServerHandler('localhost', 5000, |
77 str(temp_site_with_conflicts)) | 93 str(temp_site_with_conflicts)) |
78 environ = {'PATH_INFO': page} | 94 environ = {'PATH_INFO': page} |
79 exp_msg = 'The requested page conflicts with another page.' | 95 exp_msg = 'The requested page conflicts with another page.' |
80 | 96 |
81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 97 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) |
OLD | NEW |