]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_exec_utils.py
Merge pull request #2155 from lonvia/port-regresh-to-python
[nominatim.git] / test / python / test_tools_exec_utils.py
1 """
2 Tests for tools.exec_utils module.
3 """
4 from pathlib import Path
5 import subprocess
6 import tempfile
7
8 import pytest
9
10 import nominatim.tools.exec_utils as exec_utils
11
12 @pytest.fixture
13 def tmp_phplib_dir():
14     with tempfile.TemporaryDirectory() as phpdir:
15         (Path(phpdir) / 'admin').mkdir()
16
17         yield Path(phpdir)
18
19 @pytest.fixture
20 def nominatim_env(tmp_phplib_dir, def_config):
21     class _NominatimEnv:
22         config = def_config
23         phplib_dir = tmp_phplib_dir
24         data_dir = Path('data')
25         project_dir = Path('.')
26         module_dir = 'module'
27         osm2pgsql_path = 'osm2pgsql'
28
29     return _NominatimEnv
30
31 @pytest.fixture
32 def test_script(nominatim_env):
33     def _create_file(code):
34         with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd:
35             fd.write('<?php\n')
36             fd.write(code + '\n')
37
38         return 't.php'
39
40     return _create_file
41
42 @pytest.fixture(params=[0, 1, 15, 255])
43 def return_code(request):
44     return request.param
45
46 ### run_legacy_script
47
48 def test_run_legacy_return_exit_code(nominatim_env, test_script, return_code):
49     fname = test_script('exit({});'.format(return_code))
50     assert return_code == exec_utils.run_legacy_script(fname,
51                                                        nominatim_env=nominatim_env)
52
53
54 def test_run_legacy_return_throw_on_fail(nominatim_env, test_script):
55     fname = test_script('exit(11);')
56     with pytest.raises(subprocess.CalledProcessError):
57         exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
58                                      throw_on_fail=True)
59
60
61 def test_run_legacy_return_dont_throw_on_success(nominatim_env, test_script):
62     fname = test_script('exit(0);')
63     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
64                                              throw_on_fail=True)
65
66 def test_run_legacy_use_given__module_path(nominatim_env, test_script):
67     fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'module' ? 0 : 23);")
68
69     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
70
71
72 def test_run_legacy_do_not_overwrite_module_path(nominatim_env, test_script, monkeypatch):
73     monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
74     fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
75
76     assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
77
78 ### run_api_script
79
80 @pytest.fixture
81 def tmp_project_dir():
82     with tempfile.TemporaryDirectory() as tempd:
83         project_dir = Path(tempd)
84         webdir = project_dir / 'website'
85         webdir.mkdir()
86
87         with (webdir / 'test.php').open(mode='w') as fd:
88             fd.write("<?php\necho 'OK\n';")
89
90         yield project_dir
91
92 def test_run_api(tmp_project_dir):
93     assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
94
95 def test_run_api_execution_error(tmp_project_dir):
96     assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
97
98 def test_run_api_with_extra_env(tmp_project_dir):
99     extra_env = dict(SCRIPT_FILENAME=str(tmp_project_dir / 'website' / 'test.php'))
100     assert 0 == exec_utils.run_api_script('badname', tmp_project_dir,
101                                           extra_env=extra_env)
102
103
104 ### run_osm2pgsql
105
106 def test_run_osm2pgsql():
107     exec_utils.run_osm2pgsql(dict(osm2pgsql='echo', append=False, flatnode_file=None,
108                                   dsn='dbname=foobar', threads=1, osm2pgsql_cache=500,
109                                   osm2pgsql_style='./my.style',
110                                   import_file='foo.bar'))