2 Tests for tools.exec_utils module.
4 from pathlib import Path
10 import nominatim.tools.exec_utils as exec_utils
13 def nominatim_env(tmp_phplib_dir, def_config):
16 phplib_dir = tmp_phplib_dir
17 data_dir = Path('data')
18 project_dir = Path('.')
19 sqllib_dir = Path('lib-sql')
20 config_dir = Path('settings')
22 osm2pgsql_path = 'osm2pgsql'
27 def test_script(nominatim_env):
28 def _create_file(code):
29 with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd:
37 @pytest.fixture(params=[0, 1, 15, 255])
38 def return_code(request):
43 def test_run_legacy_return_exit_code(nominatim_env, test_script, return_code):
44 fname = test_script('exit({});'.format(return_code))
45 assert return_code == exec_utils.run_legacy_script(fname,
46 nominatim_env=nominatim_env)
49 def test_run_legacy_return_throw_on_fail(nominatim_env, test_script):
50 fname = test_script('exit(11);')
51 with pytest.raises(subprocess.CalledProcessError):
52 exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
56 def test_run_legacy_return_dont_throw_on_success(nominatim_env, test_script):
57 fname = test_script('exit(0);')
58 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
61 def test_run_legacy_use_given_module_path(nominatim_env, test_script):
62 fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
64 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
67 def test_run_legacy_do_not_overwrite_module_path(nominatim_env, test_script, monkeypatch):
68 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
69 fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
71 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
76 def tmp_project_dir():
77 with tempfile.TemporaryDirectory() as tempd:
78 project_dir = Path(tempd)
79 webdir = project_dir / 'website'
82 with (webdir / 'test.php').open(mode='w') as fd:
83 fd.write("<?php\necho 'OK\n';")
87 def test_run_api(tmp_project_dir):
88 assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
90 def test_run_api_execution_error(tmp_project_dir):
91 assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
93 def test_run_api_with_extra_env(tmp_project_dir):
94 extra_env = dict(SCRIPT_FILENAME=str(tmp_project_dir / 'website' / 'test.php'))
95 assert 0 == exec_utils.run_api_script('badname', tmp_project_dir,
101 def test_run_osm2pgsql(osm2pgsql_options):
102 osm2pgsql_options['append'] = False
103 osm2pgsql_options['import_file'] = 'foo.bar'
104 osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
105 exec_utils.run_osm2pgsql(osm2pgsql_options)
108 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
109 osm2pgsql_options['append'] = True
110 osm2pgsql_options['import_file'] = 'foo.bar'
111 osm2pgsql_options['disable_jit'] = True
112 exec_utils.run_osm2pgsql(osm2pgsql_options)