| Left: | ||
| Right: | 
| 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) | 16 outputs = {} | 
| 17 for filename in list(outputs): | 17 if with_common: | 
| 18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 18 outputs = get_dir_contents(os.path.join(root, 'common')) | 
| 19 # There are cases where we need to test outputs which differ depending | 19 outputs.update(get_dir_contents(os.path.join(root, test_type))) | 
| 20 # on how they are generated; either statically or dynamically | |
| 21 if filename.endswith('@' + test_type): | |
| 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() | 20 return outputs.items() | 
| 28 | 21 | 
| 29 | 22 | 
| 30 static_expected_outputs = get_expected_outputs('static') | 23 static_expected_outputs = get_expected_outputs('static') | 
| 31 dynamic_expected_outputs = get_expected_outputs('dynamic') | 24 dynamic_expected_outputs = get_expected_outputs('dynamic') | 
| 32 relative_expected_outputs = get_expected_outputs('relative') | 25 relative_expected_outputs = get_expected_outputs('relative', False) | 
| 26 | |
| 27 | |
| 28 def generate_static_pages(temp_site, tmpdir_factory, *extra_args): | |
| 29 dst_path = str(tmpdir_factory.mktemp('static')) | |
| 30 sys.argv = ['filler', temp_site, dst_path] + list(extra_args) | |
| 31 with mock.patch('cms.sources.FileSource.version', 1): | |
| 32 runpy.run_module('cms.bin.generate_static_pages', | |
| 33 run_name='__main__') | |
| 34 return get_dir_contents(dst_path) | |
| 33 | 35 | 
| 34 | 36 | 
| 35 @pytest.fixture(scope='session') | 37 @pytest.fixture(scope='session') | 
| 36 def static_output(request, temp_site): | 38 def output_pages(temp_site, tmpdir_factory): | 
| 37 static_out_path = os.path.join(temp_site, 'static_out') | 39 return generate_static_pages(temp_site, tmpdir_factory) | 
| 38 sys.argv = ['filler', temp_site, static_out_path] | |
| 39 with mock.patch('cms.sources.FileSource.version', 1): | |
| 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | |
| 41 return static_out_path | |
| 42 | 40 | 
| 43 | 41 | 
| 44 @pytest.fixture(scope='session') | 42 @pytest.fixture(scope='session') | 
| 45 def static_output_relative(request, temp_site): | 43 def output_pages_relative(temp_site, tmpdir_factory): | 
| 
 
Vasily Kuznetsov
2018/11/02 10:45:35
This code repetition is not that great. The differ
 
rhowell
2018/11/06 03:34:41
Yeah, good idea. This seems much cleaner (and more
 
 | |
| 46 static_out_path = os.path.join(temp_site, 'static_out_relative') | 44 return generate_static_pages(temp_site, tmpdir_factory, '--relative') | 
| 47 sys.argv = ['filler', temp_site, static_out_path, '--relative'] | |
| 48 with mock.patch('cms.sources.FileSource.version', 1): | |
| 49 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | |
| 50 return static_out_path | |
| 51 | |
| 52 | |
| 53 @pytest.fixture(scope='session') | |
| 54 def output_pages(static_output): | |
| 55 return get_dir_contents(static_output) | |
| 56 | |
| 57 | |
| 58 @pytest.fixture(scope='session') | |
| 59 def output_pages_relative(static_output_relative): | |
| 60 return get_dir_contents(static_output_relative) | |
| 61 | 45 | 
| 62 | 46 | 
| 63 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 47 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 
| 64 def test_static(output_pages, filename, expected_output): | 48 def test_static(output_pages, filename, expected_output): | 
| 65 if expected_output.startswith('## MISSING'): | 49 if expected_output.startswith('## MISSING'): | 
| 66 assert filename not in output_pages | 50 assert filename not in output_pages | 
| 67 else: | 51 else: | 
| 68 assert expected_output == output_pages[filename] | 52 assert expected_output == output_pages[filename] | 
| 69 | 53 | 
| 70 | 54 | 
| 71 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) | 55 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) | 
| 
 
Vasily Kuznetsov
2018/11/02 10:45:35
This function is also basically a clone of `test_s
 
rhowell
2018/11/06 03:34:42
Acknowledged.
 
 | |
| 72 def test_static_relative(output_pages_relative, filename, expected_output): | 56 def test_static_relative(output_pages_relative, filename, expected_output): | 
| 73 if expected_output.startswith('## MISSING'): | 57 if expected_output.startswith('## MISSING'): | 
| 74 assert filename not in output_pages_relative | 58 assert filename not in output_pages_relative | 
| 75 else: | 59 else: | 
| 76 assert expected_output == output_pages_relative[filename] | 60 assert expected_output == output_pages_relative[filename] | 
| 77 | 61 | 
| 78 | 62 | 
| 79 def test_cache(output_pages): | 63 def test_cache(output_pages): | 
| 80 source = FileSource(os.path.join('test_site')) | 64 source = FileSource(os.path.join('test_site')) | 
| 81 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 65 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 
| 82 | 66 | 
| 83 | 67 | 
| 84 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 68 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 
| 85 def test_dynamic_server_handler(filename, expected_output, temp_site): | 69 def test_dynamic_server_handler(filename, expected_output, temp_site): | 
| 70 | |
| 86 def cleanup(page): | 71 def cleanup(page): | 
| 87 return page.replace(os.linesep, '').strip() | 72 return page.replace(os.linesep, '').strip() | 
| 88 | 73 | 
| 89 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 74 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 
| 90 environ = {'PATH_INFO': filename} | 75 environ = {'PATH_INFO': filename} | 
| 91 | 76 | 
| 92 generated_page = handler(environ, lambda x, y: None) | 77 generated_page = handler(environ, lambda x, y: None) | 
| 93 | 78 | 
| 94 assert cleanup(expected_output) == cleanup(generated_page[0]) | 79 assert cleanup(expected_output) == cleanup(generated_page[0]) | 
| 95 | 80 | 
| 96 | 81 | 
| 97 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 82 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 
| 98 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): | 
| 99 handler = DynamicServerHandler('localhost', 5000, | 84 handler = DynamicServerHandler('localhost', 5000, | 
| 100 str(temp_site_with_conflicts)) | 85 str(temp_site_with_conflicts)) | 
| 101 environ = {'PATH_INFO': page} | 86 environ = {'PATH_INFO': page} | 
| 102 exp_msg = 'The requested page conflicts with another page.' | 87 exp_msg = 'The requested page conflicts with another page.' | 
| 103 | 88 | 
| 104 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 |