2 Tests for tools.exec_utils module.
4 from pathlib import Path
9 import nominatim.tools.exec_utils as exec_utils
11 class TestRunLegacyScript:
13 @pytest.fixture(autouse=True)
14 def setup_nominatim_env(self, tmp_path, def_config):
15 tmp_phplib_dir = tmp_path / 'phplib'
16 tmp_phplib_dir.mkdir()
17 (tmp_phplib_dir / 'admin').mkdir()
21 phplib_dir = tmp_phplib_dir
22 data_dir = Path('data')
23 project_dir = Path('.')
24 sqllib_dir = Path('lib-sql')
25 config_dir = Path('settings')
27 osm2pgsql_path = 'osm2pgsql'
29 self.testenv = _NominatimEnv
32 def mk_script(self, code):
33 codefile = self.testenv.phplib_dir / 'admin' / 't.php'
34 codefile.write_text('<?php\n' + code + '\n')
39 @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
40 def test_run_legacy_return_exit_code(self, return_code):
41 fname = self.mk_script('exit({});'.format(return_code))
42 assert return_code == \
43 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
46 def test_run_legacy_return_throw_on_fail(self):
47 fname = self.mk_script('exit(11);')
48 with pytest.raises(subprocess.CalledProcessError):
49 exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
53 def test_run_legacy_return_dont_throw_on_success(self):
54 fname = self.mk_script('exit(0);')
55 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
58 def test_run_legacy_use_given_module_path(self):
59 fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
61 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
64 def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
65 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
66 fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
68 assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
71 class TestRunApiScript:
73 @pytest.fixture(autouse=True)
74 def setup_project_dir(self, tmp_path):
75 webdir = tmp_path / 'website'
77 (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
80 def test_run_api(self, tmp_path):
81 assert 0 == exec_utils.run_api_script('test', tmp_path)
83 def test_run_api_execution_error(self, tmp_path):
84 assert 0 != exec_utils.run_api_script('badname', tmp_path)
86 def test_run_api_with_extra_env(self, tmp_path):
87 extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
88 assert 0 == exec_utils.run_api_script('badname', tmp_path,
94 def test_run_osm2pgsql(osm2pgsql_options):
95 osm2pgsql_options['append'] = False
96 osm2pgsql_options['import_file'] = 'foo.bar'
97 osm2pgsql_options['tablespaces']['osm_data'] = 'extra'
98 exec_utils.run_osm2pgsql(osm2pgsql_options)
101 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
102 osm2pgsql_options['append'] = True
103 osm2pgsql_options['import_file'] = 'foo.bar'
104 osm2pgsql_options['disable_jit'] = True
105 exec_utils.run_osm2pgsql(osm2pgsql_options)