OLD | NEW |
1 import os | 1 import os |
2 import sys | 2 import sys |
3 import runpy | 3 import runpy |
| 4 |
| 5 import mock |
4 import pytest | 6 import pytest |
5 import urllib2 | 7 import urllib2 |
6 | 8 |
7 from .conftest import ROOTPATH | 9 from .conftest import ROOTPATH |
8 from .utils import get_dir_contents, run_test_server | 10 from .utils import get_dir_contents, run_test_server |
| 11 from cms.sources import FileSource |
9 | 12 |
10 | 13 |
11 def get_expected_outputs(test_type): | 14 def get_expected_outputs(test_type): |
12 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 15 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
13 outputs = get_dir_contents(expected_out_path) | 16 outputs = get_dir_contents(expected_out_path) |
14 for filename in list(outputs): | 17 for filename in list(outputs): |
15 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") | 18 # Move test-type-specific expected outputs (e.g. "xyz@static" -> "xyz") |
16 # There are cases where we need to test outputs which differ depending | 19 # There are cases where we need to test outputs which differ depending |
17 # on how they are generated; either statically or dynamically | 20 # on how they are generated; either statically or dynamically |
18 if filename.endswith('@' + test_type): | 21 if filename.endswith('@' + test_type): |
19 realname = filename.split('@')[0] | 22 realname = filename.split('@')[0] |
20 outputs[realname] = outputs[filename] | 23 outputs[realname] = outputs[filename] |
21 # Move bookmark specific output (e.g. "xyx@static+master -> xyz+master) | |
22 # There are cases where we need to test outputs which differ depending | |
23 # on the bookmark which they are generated from: | |
24 # https://issues.adblockplus.org/ticket/6605 | |
25 if '+' in filename: | |
26 realname = ''.join(filename.split('@' + test_type)) | |
27 outputs[realname] = outputs[filename] | |
28 # 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. |
29 if '@' in filename: | 25 if '@' in filename: |
30 del outputs[filename] | 26 del outputs[filename] |
31 return outputs.items() | 27 return outputs.items() |
32 | 28 |
33 | 29 |
34 static_expected_outputs = get_expected_outputs('static') | 30 static_expected_outputs = get_expected_outputs('static') |
35 dynamic_expected_outputs = get_expected_outputs('dynamic') | 31 dynamic_expected_outputs = get_expected_outputs('dynamic') |
36 | 32 |
37 | 33 |
38 @pytest.fixture(scope='session', params=['master', None]) | 34 @mock.patch('cms.sources.FileSource.version', 1) |
39 def revision(request): | |
40 return request.param | |
41 | |
42 | |
43 @pytest.fixture(scope='session') | 35 @pytest.fixture(scope='session') |
44 def static_output(revision, request, temp_site): | 36 def static_output(request, temp_site): |
45 static_out_path = os.path.join(temp_site, 'static_out') | 37 static_out_path = os.path.join(temp_site, 'static_out') |
46 sys.argv = ['filler', temp_site, static_out_path] | 38 sys.argv = ['filler', temp_site, static_out_path] |
47 if revision is not None: | |
48 sys.argv += ['--rev', revision] | |
49 | 39 |
50 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 40 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
51 return static_out_path | 41 return static_out_path |
52 | 42 |
53 | 43 |
54 @pytest.fixture(scope='module') | 44 @pytest.fixture(scope='module') |
55 def dynamic_server(temp_site): | 45 def dynamic_server(temp_site): |
56 with run_test_server(temp_site) as ts: | 46 with run_test_server(temp_site) as ts: |
57 yield ts | 47 yield ts |
58 | 48 |
59 | 49 |
60 @pytest.fixture(scope='session') | 50 @pytest.fixture(scope='session') |
61 def output_pages(static_output): | 51 def output_pages(static_output): |
62 return get_dir_contents(static_output) | 52 return get_dir_contents(static_output) |
63 | 53 |
64 | 54 |
65 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) | 55 @pytest.mark.parametrize('filename,expected_output', static_expected_outputs) |
66 def test_static(revision, output_pages, filename, expected_output): | 56 def test_static(output_pages, filename, expected_output): |
67 if expected_output.startswith('## MISSING'): | 57 if expected_output.startswith('## MISSING'): |
68 assert filename not in output_pages | 58 assert filename not in output_pages |
69 elif revision and '+' + revision in filename: | 59 else: |
70 filename = filename.split('+')[0] | |
71 assert expected_output == output_pages[filename] | |
72 elif not revision and '+' not in filename: | |
73 assert expected_output == output_pages[filename] | 60 assert expected_output == output_pages[filename] |
74 | 61 |
75 | 62 |
76 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) | 63 @pytest.mark.parametrize('filename,expected_output', dynamic_expected_outputs) |
77 def test_dynamic(dynamic_server, filename, expected_output): | 64 def test_dynamic(dynamic_server, filename, expected_output): |
78 response = urllib2.urlopen(dynamic_server + filename) | 65 response = urllib2.urlopen(dynamic_server + filename) |
79 assert expected_output == response.read().strip() | 66 assert expected_output == response.read().strip() |
80 | 67 |
81 | 68 |
82 def test_revision_arg(revision, output_pages): | 69 def test_cache(output_pages): |
83 if revision is None: | 70 source = FileSource(os.path.join('test_site')) |
84 assert 'en/bar' in output_pages | 71 assert source.get_cache_dir() == os.path.join('test_site', 'cache') |
85 else: | |
86 assert 'en/bar' not in output_pages | |
OLD | NEW |