OLD | NEW |
1 import os | 1 import os |
2 import sys | 2 import sys |
3 import shutil | |
4 import time | 3 import time |
5 import runpy | 4 import runpy |
| 5 import signal |
6 import pytest | 6 import pytest |
7 import urllib2 | 7 import urllib2 |
8 import subprocess | 8 import subprocess |
9 | 9 from conftest import ROOTPATH |
10 ROOTPATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
11 | 10 |
12 | 11 |
13 def get_dir_contents(path): | 12 def get_dir_contents(path): |
14 return_data = {} | 13 return_data = {} |
15 for dirpath, dirnames, filenames in os.walk(path): | 14 for dirpath, dirnames, filenames in os.walk(path): |
16 for output_file in filenames: | 15 for output_file in filenames: |
17 with open(os.path.join(dirpath, output_file)) as f: | 16 with open(os.path.join(dirpath, output_file)) as f: |
18 return_data[output_file] = f.read() | 17 return_data[output_file] = f.read() |
19 return return_data | 18 return return_data |
20 | 19 |
21 | 20 |
22 def get_expected_outputs(): | 21 def get_expected_outputs(): |
23 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') | 22 expected_out_path = os.path.join(ROOTPATH, 'tests', 'expected_output') |
24 return get_dir_contents(expected_out_path).items() | 23 return get_dir_contents(expected_out_path).items() |
25 | 24 |
26 expected_outputs = get_expected_outputs() | 25 expected_outputs = get_expected_outputs() |
27 | 26 |
28 | 27 |
29 @pytest.fixture(scope='session') | 28 @pytest.fixture(scope='session') |
30 def temp_site(tmpdir_factory): | |
31 out_dir = tmpdir_factory.mktemp('temp_out') | |
32 site_dir = out_dir.join('test_site').strpath | |
33 | |
34 shutil.copytree(os.path.join(ROOTPATH, 'tests', 'test_site'), site_dir) | |
35 subprocess.check_call(['hg', 'init', site_dir]) | |
36 subprocess.check_call(['hg', '-R', site_dir, 'commit', '-A', '-m', 'foo']) | |
37 return site_dir | |
38 | |
39 | |
40 @pytest.fixture(scope='session') | |
41 def static_output(request, temp_site): | 29 def static_output(request, temp_site): |
42 static_out_path = os.path.join(temp_site, 'static_out') | 30 static_out_path = os.path.join(temp_site, 'static_out') |
43 sys.argv = ['filler', temp_site, static_out_path] | 31 sys.argv = ['filler', temp_site, static_out_path] |
44 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') | 32 runpy.run_module('cms.bin.generate_static_pages', run_name='__main__') |
45 return static_out_path | 33 return static_out_path |
46 | 34 |
47 | 35 |
48 @pytest.yield_fixture(scope='session') | 36 @pytest.yield_fixture() |
49 def dynamic_server(temp_site): | 37 def dynamic_server(temp_site): |
50 p = subprocess.Popen(['python', 'runserver.py', temp_site]) | 38 args = ['python', 'runserver.py', temp_site] |
| 39 # Werkzeug is a dependency of flask which we are using for the mock api |
| 40 # however there is an issue with Werkzeug that prevents it from properly |
| 41 # handling the SIGTERM sent by p.kill() or terminate() |
| 42 # Issue: https://github.com/pallets/werkzeug/issues/58 |
| 43 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) |
51 time.sleep(0.5) | 44 time.sleep(0.5) |
52 yield 'http://localhost:5000/root/' | 45 yield 'http://localhost:5000/root/' |
53 p.terminate() | 46 os.killpg(os.getpgid(p.pid), signal.SIGTERM) |
54 | 47 |
55 | 48 |
56 @pytest.fixture(scope='session') | 49 @pytest.fixture(scope='session') |
57 def output_pages(static_output): | 50 def output_pages(static_output): |
58 return get_dir_contents(static_output) | 51 return get_dir_contents(static_output) |
59 | 52 |
60 | 53 |
61 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 54 @pytest.mark.parametrize('filename,expected_output', expected_outputs) |
62 def test_static(output_pages, filename, expected_output): | 55 def test_static(output_pages, filename, expected_output): |
63 assert output_pages[filename] == expected_output | 56 assert output_pages[filename] == expected_output |
64 | 57 |
65 | 58 |
66 @pytest.mark.parametrize('filename,expected_output', expected_outputs) | 59 @pytest.mark.parametrize('filename,expected_output', expected_outputs) |
67 def test_dynamic(dynamic_server, filename, expected_output): | 60 def test_dynamic(dynamic_server, filename, expected_output): |
68 response = urllib2.urlopen(dynamic_server + filename) | 61 response = urllib2.urlopen(dynamic_server + filename) |
69 assert response.read() == expected_output | 62 assert response.read() == expected_output |
OLD | NEW |