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('.')
27 osm2pgsql_path = 'osm2pgsql'
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:
42 @pytest.fixture(params=[0, 1, 15, 255])
43 def return_code(request):
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)
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,
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,
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);")
69 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
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);")
76 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
81 def tmp_project_dir():
82 with tempfile.TemporaryDirectory() as tempd:
83 project_dir = Path(tempd)
84 webdir = project_dir / 'website'
87 with (webdir / 'test.php').open(mode='w') as fd:
88 fd.write("<?php\necho 'OK\n';")
92 def test_run_api(tmp_project_dir):
93 assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
95 def test_run_api_execution_error(tmp_project_dir):
96 assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
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,
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'))