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 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 realname = filename.split('@')[0] | 22 realname = filename.split('@')[0] |
23 outputs[realname] = outputs[filename] | 23 outputs[realname] = outputs[filename] |
24 # Remove the expected outputs that don't apply for this test type. | 24 # Remove the expected outputs that don't apply for this test type. |
25 if '@' in filename: | 25 if '@' in filename: |
26 del outputs[filename] | 26 del outputs[filename] |
27 return outputs.items() | 27 return outputs.items() |
28 | 28 |
29 | 29 |
30 static_expected_outputs = get_expected_outputs('static') | 30 static_expected_outputs = get_expected_outputs('static') |
31 dynamic_expected_outputs = get_expected_outputs('dynamic') | 31 dynamic_expected_outputs = get_expected_outputs('dynamic') |
32 relative_expected_outputs = get_expected_outputs('relative') | |
32 | 33 |
33 | 34 |
34 @pytest.fixture(scope='session') | 35 @pytest.fixture(scope='session') |
35 def static_output(request, temp_site): | 36 def static_output(request, temp_site): |
36 static_out_path = os.path.join(temp_site, 'static_out') | 37 static_out_path = os.path.join(temp_site, 'static_out') |
37 sys.argv = ['filler', temp_site, static_out_path] | 38 sys.argv = ['filler', temp_site, static_out_path] |
38 with mock.patch('cms.sources.FileSource.version', 1): | 39 with mock.patch('cms.sources.FileSource.version', 1): |
39 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
40 return static_out_path | 41 return static_out_path |
41 | 42 |
42 | 43 |
43 @pytest.fixture(scope='session') | 44 @pytest.fixture(scope='session') |
45 def static_output_relative(request, temp_site): | |
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') | |
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') | |
44 def output_pages(static_output): | 54 def output_pages(static_output): |
45 return get_dir_contents(static_output) | 55 return get_dir_contents(static_output) |
46 | 56 |
47 | 57 |
58 @pytest.fixture(scope='session') | |
59 def output_pages_relative(static_output_relative): | |
60 return get_dir_contents(static_output_relative) | |
61 | |
62 | |
48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 63 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
49 def test_static(output_pages, filename, expected_output): | 64 def test_static(output_pages, filename, expected_output): |
50 if expected_output.startswith('## MISSING'): | 65 if expected_output.startswith('## MISSING'): |
51 assert filename not in output_pages | 66 assert filename not in output_pages |
52 else: | 67 else: |
53 assert expected_output == output_pages[filename] | 68 assert expected_output == output_pages[filename] |
54 | 69 |
55 | 70 |
71 @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): | |
73 if expected_output.startswith('## MISSING'): | |
74 assert filename not in output_pages_relative | |
75 else: | |
76 assert expected_output == output_pages_relative[filename] | |
77 | |
78 | |
56 def test_cache(output_pages): | 79 def test_cache(output_pages): |
57 source = FileSource(os.path.join('test_site')) | 80 source = FileSource(os.path.join('test_site')) |
58 assert source.get_cache_dir() == os.path.join('test_site', 'cache') | 81 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
59 | 82 |
60 | 83 |
61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 84 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
62 def test_dynamic_server_handler(filename, expected_output, temp_site): | 85 def test_dynamic_server_handler(filename, expected_output, temp_site): |
63 def cleanup(page): | 86 def cleanup(page): |
64 return page.replace(os.linesep, '').strip() | 87 return page.replace(os.linesep, '').strip() |
65 | 88 |
66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) | 89 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) |
67 environ = {'PATH_INFO': filename} | 90 environ = {'PATH_INFO': filename} |
68 | 91 |
69 generated_page = handler(environ, lambda x, y: None) | 92 generated_page = handler(environ, lambda x, y: None) |
70 | 93 |
71 assert cleanup(expected_output) == cleanup(generated_page[0]) | 94 assert cleanup(expected_output) == cleanup(generated_page[0]) |
72 | 95 |
73 | 96 |
74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) | 97 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) |
75 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): | 98 def test_dynamic_server_handler_with_conflicts(page, temp_site_with_conflicts): |
76 handler = DynamicServerHandler('localhost', 5000, | 99 handler = DynamicServerHandler('localhost', 5000, |
77 str(temp_site_with_conflicts)) | 100 str(temp_site_with_conflicts)) |
78 environ = {'PATH_INFO': page} | 101 environ = {'PATH_INFO': page} |
79 exp_msg = 'The requested page conflicts with another page.' | 102 exp_msg = 'The requested page conflicts with another page.' |
80 | 103 |
81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) | 104 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) |
OLD | NEW |