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