2 Tests for tools.exec_utils module.
4 from pathlib import Path
10 import nominatim.tools.exec_utils as exec_utils
14 with tempfile.TemporaryDirectory() as phpdir:
15 (Path(phpdir) / 'admin').mkdir()
20 def nominatim_env(tmp_phplib_dir, 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')
29 osm2pgsql_path = 'osm2pgsql'
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:
44 @pytest.fixture(params=[0, 1, 15, 255])
45 def return_code(request):
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)
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,
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,
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);")
71 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
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);")
78 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
83 def tmp_project_dir():
84 with tempfile.TemporaryDirectory() as tempd:
85 project_dir = Path(tempd)
86 webdir = project_dir / 'website'
89 with (webdir / 'test.php').open(mode='w') as fd:
90 fd.write("<?php\necho 'OK\n';")
94 def test_run_api(tmp_project_dir):
95 assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
97 def test_run_api_execution_error(tmp_project_dir):
98 assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
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,
108 def test_run_osm2pgsql(osm2pgsql_options):
109 osm2pgsql_options['append'] = False
110 osm2pgsql_options['import_file'] = 'foo.bar'
111 osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
112 exec_utils.run_osm2pgsql(osm2pgsql_options)
115 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
116 osm2pgsql_options['append'] = True
117 osm2pgsql_options['import_file'] = 'foo.bar'
118 osm2pgsql_options['disable_jit'] = True
119 exec_utils.run_osm2pgsql(osm2pgsql_options)