Left: | ||
Right: |
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 test_type) | |
16 outputs = get_dir_contents(expected_out_path) | 17 outputs = get_dir_contents(expected_out_path) |
17 for filename in list(outputs): | 18 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output', |
Vasily Kuznetsov
2018/11/07 18:03:02
Reusing variables like this is not a very good pra
rhowell
2018/11/07 20:05:26
Done.
| |
18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 19 'common') |
19 # There are cases where we need to test outputs which differ depending | 20 outputs.update(get_dir_contents(expected_out_path)) |
Vasily Kuznetsov
2018/11/07 18:03:02
It seems that the correct approach should be to lo
rhowell
2018/11/07 20:05:26
Done.
| |
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() | 21 return outputs.items() |
28 | 22 |
29 | 23 |
30 static_expected_outputs = get_expected_outputs('static') | 24 static_expected_outputs = get_expected_outputs('static') |
31 dynamic_expected_outputs = get_expected_outputs('dynamic') | 25 dynamic_expected_outputs = get_expected_outputs('dynamic') |
26 relative_expected_outputs = get_expected_outputs('relative') | |
Vasily Kuznetsov
2018/11/07 18:03:02
My proposal was to also skip the common expected o
rhowell
2018/11/07 20:05:25
Done.
| |
27 | |
28 | |
29 def generate_static_pages(temp_site, tmpdir_factory, *extra_args): | |
30 dst_path = str(tmpdir_factory.mktemp('static')) | |
31 sys.argv = ['filler', temp_site, dst_path] + list(extra_args) | |
32 with mock.patch('cms.sources.FileSource.version', 1): | |
33 runpy.run_module('cms.bin.generate_static_pages', | |
34 run_name='__main__') | |
35 return get_dir_contents(dst_path) | |
32 | 36 |
33 | 37 |
34 @pytest.fixture(scope='session') | 38 @pytest.fixture(scope='session') |
35 def static_output(request, temp_site): | 39 def output_pages(temp_site, tmpdir_factory): |
36 static_out_path = os.path.join(temp_site, 'static_out') | 40 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 | 41 |
42 | 42 |
43 @pytest.fixture(scope='session') | 43 @pytest.fixture(scope='session') |
44 def output_pages(static_output): | 44 def output_pages_relative(temp_site, tmpdir_factory): |
45 return get_dir_contents(static_output) | 45 return generate_static_pages(temp_site, tmpdir_factory, '--relative') |
46 | 46 |
47 | 47 |
48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
49 def test_static(output_pages, filename, expected_output): | 49 def test_static(output_pages, filename, expected_output): |
50 if expected_output.startswith('## MISSING'): | 50 if expected_output.startswith('## MISSING'): |
51 assert filename not in output_pages | 51 assert filename not in output_pages |
52 else: | 52 else: |
53 assert expected_output == output_pages[filename] | 53 assert expected_output == output_pages[filename] |
54 | 54 |
55 | 55 |
56 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs) | |
57 def test_static_relative(output_pages_relative, filename, expected_output): | |
58 if expected_output.startswith('## MISSING'): | |
59 assert filename not in output_pages_relative | |
60 else: | |
61 assert expected_output == output_pages_relative[filename] | |
62 | |
63 | |
56 def test_cache(output_pages): | 64 def test_cache(output_pages): |
57 source = FileSource(os.path.join('test_site')) | 65 source = FileSource(os.path.join('test_site')) |
58 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 66 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
59 | 67 |
60 | 68 |
61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 69 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
62 def test_dynamic_server_handler(filename, expected_output, temp_site): | 70 def test_dynamic_server_handler(filename, expected_output, temp_site): |
71 | |
63 def cleanup(page): | 72 def cleanup(page): |
64 return page.replace(os.linesep, '').strip() | 73 return page.replace(os.linesep, '').strip() |
65 | 74 |
66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 75 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
67 environ = {'PATH_INFO': filename} | 76 environ = {'PATH_INFO': filename} |
68 | 77 |
69 generated_page = handler(environ, lambda x, y: None) | 78 generated_page = handler(environ, lambda x, y: None) |
70 | 79 |
71 assert cleanup(expected_output) == cleanup(generated_page[0]) | 80 assert cleanup(expected_output) == cleanup(generated_page[0]) |
72 | 81 |
73 | 82 |
74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 83 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
75 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): | 84 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
76 handler = DynamicServerHandler('localhost', 5000, | 85 handler = DynamicServerHandler('localhost', 5000, |
77 str(temp_site_with_conflicts)) | 86 str(temp_site_with_conflicts)) |
78 environ = {'PATH_INFO': page} | 87 environ = {'PATH_INFO': page} |
79 exp_msg = 'The requested page conflicts with another page.' | 88 exp_msg = 'The requested page conflicts with another page.' |
80 | 89 |
81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 90 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) |
OLD | NEW |