]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_exec_utils.py
test: avoid use of tempfile module
[nominatim.git] / test / python / test_tools_exec_utils.py
1 """
2 Tests for tools.exec_utils module.
3 """
4 from pathlib import Path
5 import subprocess
6
7 import pytest
8
9 import nominatim.tools.exec_utils as exec_utils
10
11 class TestRunLegacyScript:
12
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()
18
19         class _NominatimEnv:
20             config = def_config
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')
26             module_dir = 'module'
27             osm2pgsql_path = 'osm2pgsql'
28
29         self.testenv = _NominatimEnv
30
31
32     def mk_script(self, code):
33         codefile = self.testenv.phplib_dir / 'admin' / 't.php'
34         codefile.write_text('<?php\n' + code + '\n')
35
36         return 't.php'
37
38
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)
44
45
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,
50                                          throw_on_fail=True)
51
52
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,
56                                                  throw_on_fail=True)
57
58     def test_run_legacy_use_given_module_path(self):
59         fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
60
61         assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
62
63
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);")
67
68         assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
69
70
71 class TestRunApiScript:
72
73     @pytest.fixture(autouse=True)
74     def setup_project_dir(self, tmp_path):
75         webdir = tmp_path / 'website'
76         webdir.mkdir()
77         (webdir / 'test.php').write_text("<?php\necho 'OK\n';")
78
79
80     def test_run_api(self, tmp_path):
81         assert 0 == exec_utils.run_api_script('test', tmp_path)
82
83     def test_run_api_execution_error(self, tmp_path):
84         assert 0 != exec_utils.run_api_script('badname', tmp_path)
85
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,
89                                               extra_env=extra_env)
90
91
92 ### run_osm2pgsql
93
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)
99
100
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)