1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for tools.exec_utils module.
10 from pathlib import Path
15 from nominatim.config import Configuration
16 import nominatim.tools.exec_utils as exec_utils
17 import nominatim.paths
19 class TestRunLegacyScript:
21 @pytest.fixture(autouse=True)
22 def setup_nominatim_env(self, tmp_path, monkeypatch):
23 tmp_phplib_dir = tmp_path / 'phplib'
24 tmp_phplib_dir.mkdir()
25 (tmp_phplib_dir / 'admin').mkdir()
27 monkeypatch.setattr(nominatim.paths, 'PHPLIB_DIR', tmp_phplib_dir)
29 self.phplib_dir = tmp_phplib_dir
30 self.config = Configuration(tmp_path)
31 self.config.set_libdirs(module='.', osm2pgsql='default_osm2pgsql',
35 def mk_script(self, code):
36 codefile = self.phplib_dir / 'admin' / 't.php'
37 codefile.write_text('<?php\n' + code + '\n')
42 @pytest.mark.parametrize("return_code", (0, 1, 15, 255))
43 def test_run_legacy_return_exit_code(self, return_code):
44 fname = self.mk_script('exit({});'.format(return_code))
45 assert return_code == \
46 exec_utils.run_legacy_script(fname, config=self.config)
49 def test_run_legacy_return_throw_on_fail(self):
50 fname = self.mk_script('exit(11);')
51 with pytest.raises(subprocess.CalledProcessError):
52 exec_utils.run_legacy_script(fname, config=self.config,
56 def test_run_legacy_return_dont_throw_on_success(self):
57 fname = self.mk_script('exit(0);')
58 assert exec_utils.run_legacy_script(fname, config=self.config,
59 throw_on_fail=True) == 0
61 def test_run_legacy_use_given_module_path(self):
62 fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
64 assert exec_utils.run_legacy_script(fname, config=self.config) == 0
67 def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
68 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
69 fname = self.mk_script(
70 "exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
72 assert exec_utils.run_legacy_script(fname, config=self.config) == 0
75 def test_run_legacy_default_osm2pgsql_binary(self, monkeypatch):
76 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'default_osm2pgsql' ? 0 : 23);")
78 assert exec_utils.run_legacy_script(fname, config=self.config) == 0
81 def test_run_legacy_override_osm2pgsql_binary(self, monkeypatch):
82 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', 'somethingelse')
84 fname = self.mk_script("exit($_SERVER['NOMINATIM_OSM2PGSQL_BINARY'] == 'somethingelse' ? 0 : 23);")
86 assert exec_utils.run_legacy_script(fname, config=self.config) == 0
89 class TestRunApiScript:
92 @pytest.fixture(autouse=True)
93 def setup_project_dir(tmp_path):
94 webdir = tmp_path / 'website'
96 (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
100 def test_run_api(tmp_path):
101 assert exec_utils.run_api_script('test', tmp_path) == 0
104 def test_run_api_execution_error(tmp_path):
105 assert exec_utils.run_api_script('badname', tmp_path) != 0
108 def test_run_api_with_extra_env(tmp_path):
109 extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
110 assert exec_utils.run_api_script('badname', tmp_path, extra_env=extra_env) == 0
113 def test_custom_phpcgi(tmp_path, capfd):
114 assert exec_utils.run_api_script('test', tmp_path, phpcgi_bin='env',
115 params={'q' : 'Berlin'}) == 0
116 captured = capfd.readouterr()
118 assert '?q=Berlin' in captured.out
121 def test_fail_on_error_output(tmp_path):
122 # Starting PHP 8 the PHP CLI no longer has STDERR defined as constant
125 if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb'));
126 fwrite(STDERR, 'WARNING'.PHP_EOL);
128 (tmp_path / 'website' / 'bad.php').write_text(php)
130 assert exec_utils.run_api_script('bad', tmp_path) == 1
134 def test_run_osm2pgsql(osm2pgsql_options):
135 osm2pgsql_options['append'] = False
136 osm2pgsql_options['import_file'] = 'foo.bar'
137 osm2pgsql_options['tablespaces']['slim_data'] = 'extra'
138 exec_utils.run_osm2pgsql(osm2pgsql_options)
141 def test_run_osm2pgsql_disable_jit(osm2pgsql_options):
142 osm2pgsql_options['append'] = True
143 osm2pgsql_options['import_file'] = 'foo.bar'
144 osm2pgsql_options['disable_jit'] = True
145 exec_utils.run_osm2pgsql(osm2pgsql_options)