Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_page_outputs.py

Issue 29933596: Issue 5333 - Allow cms to generate relative pages (Closed) Base URL: https://hg.adblockplus.org/cms/
Patch Set: Address comments on PS5 Created Nov. 8, 2018, 8:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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')
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)
32 35
33 36
34 @pytest.fixture(scope='session') 37 @pytest.fixture(scope='session')
35 def static_output(request, temp_site): 38 def output_pages(temp_site, tmpdir_factory):
36 static_out_path = os.path.join(temp_site, 'static_out') 39 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 40
42 41
43 @pytest.fixture(scope='session') 42 @pytest.fixture(scope='session')
44 def output_pages(static_output): 43 def output_pages_relative(temp_site, tmpdir_factory):
45 return get_dir_contents(static_output) 44 return generate_static_pages(temp_site, tmpdir_factory, '--relative')
46 45
47 46
48 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) 47 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs)
49 def test_static(output_pages, filename, expected_output): 48 def test_static(output_pages, filename, expected_output):
50 if expected_output.startswith('## MISSING'): 49 if expected_output.startswith('## MISSING'):
51 assert filename not in output_pages 50 assert filename not in output_pages
52 else: 51 else:
53 assert expected_output == output_pages[filename] 52 assert expected_output == output_pages[filename]
54 53
55 54
55 @pytest.mark.parametrize('filename,expected_output', relative_expected_outputs)
56 def test_static_relative(output_pages_relative, filename, expected_output):
57 if expected_output.startswith('## MISSING'):
58 assert filename not in output_pages_relative
59 else:
60 assert expected_output == output_pages_relative[filename]
61
62
56 def test_cache(output_pages): 63 def test_cache(output_pages):
57 source = FileSource(os.path.join('test_site')) 64 source = FileSource(os.path.join('test_site'))
58 assert source.get_cache_dir() == os.path.join('test_site', 'cache') 65 assert source.get_cache_dir() == os.path.join('test_site', 'cache')
59 66
60 67
61 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) 68 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs)
62 def test_dynamic_server_handler(filename, expected_output, temp_site): 69 def test_dynamic_server_handler(filename, expected_output, temp_site):
70
63 def cleanup(page): 71 def cleanup(page):
64 return page.replace(os.linesep, '').strip() 72 return page.replace(os.linesep, '').strip()
65 73
66 handler = DynamicServerHandler('localhost', 5000, str(temp_site)) 74 handler = DynamicServerHandler('localhost', 5000, str(temp_site))
67 environ = {'PATH_INFO': filename} 75 environ = {'PATH_INFO': filename}
68 76
69 generated_page = handler(environ, lambda x, y: None) 77 generated_page = handler(environ, lambda x, y: None)
70 78
71 assert cleanup(expected_output) == cleanup(generated_page[0]) 79 assert cleanup(expected_output) == cleanup(generated_page[0])
72 80
73 81
74 @pytest.mark.parametrize('page', ['en/translate', '/en/translate']) 82 @pytest.mark.parametrize('page', ['en/translate', '/en/translate'])
75 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):
76 handler = DynamicServerHandler('localhost', 5000, 84 handler = DynamicServerHandler('localhost', 5000,
77 str(temp_site_with_conflicts)) 85 str(temp_site_with_conflicts))
78 environ = {'PATH_INFO': page} 86 environ = {'PATH_INFO': page}
79 exp_msg = 'The requested page conflicts with another page.' 87 exp_msg = 'The requested page conflicts with another page.'
80 88
81 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None) 89 exception_test(handler, Exception, exp_msg, environ, lambda x, y: None)
OLDNEW

Powered by Google App Engine
This is Rietveld