1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Test for loading dotenv configuration.
10 from pathlib import Path
13 from nominatim_db.config import Configuration, flatten_config_list
14 from nominatim_db.errors import UsageError
19 """ Create a configuration object from the given project directory.
21 def _mk_config(project_dir=None):
22 return Configuration(project_dir)
28 def make_config_path(tmp_path):
29 """ Create a configuration object with project and config directories
30 in a temporary directory.
33 (tmp_path / 'project').mkdir()
34 (tmp_path / 'config').mkdir()
35 conf = Configuration(tmp_path / 'project')
36 conf.config_dir = tmp_path / 'config'
42 def test_no_project_dir(make_config):
43 config = make_config()
45 assert config.DATABASE_WEBUSER == 'www-data'
48 @pytest.mark.parametrize("val", ('apache', '"apache"'))
49 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
50 envfile = tmp_path / '.env'
51 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val), encoding='utf-8')
53 config = make_config(tmp_path)
55 assert config.DATABASE_WEBUSER == 'apache'
58 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
59 envfile = tmp_path / '.env'
60 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n', encoding='utf-8')
62 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
64 config = make_config(tmp_path)
66 assert config.DATABASE_WEBUSER == 'nobody'
69 def test_prefer_os_environ_can_unset_project_setting(make_config, monkeypatch, tmp_path):
70 envfile = tmp_path / '.env'
71 envfile.write_text('NOMINATIM_OSM2PGSQL_BINARY=osm2pgsql\n', encoding='utf-8')
73 monkeypatch.setenv('NOMINATIM_OSM2PGSQL_BINARY', '')
75 config = make_config(tmp_path)
77 assert config.OSM2PGSQL_BINARY == ''
80 def test_get_os_env_add_defaults(make_config, monkeypatch):
81 config = make_config()
83 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
85 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
88 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
89 config = make_config()
91 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
93 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
96 def test_get_libpq_dsn_convert_default(make_config):
97 config = make_config()
99 assert config.get_libpq_dsn() == 'dbname=nominatim'
102 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
103 config = make_config()
105 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
106 'pgsql:dbname=gis;password=foo;host=localhost')
108 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
111 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
114 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
115 config = make_config()
117 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
118 'pgsql:dbname=gis;password={}'.format(val))
120 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
123 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
124 config = make_config()
126 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
127 'host=localhost dbname=gis password=foo')
129 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
132 @pytest.mark.parametrize("value,result",
133 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
134 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
135 def test_get_bool(make_config, monkeypatch, value, result):
136 config = make_config()
138 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
140 assert config.get_bool('FOOBAR') == result
143 def test_get_bool_empty(make_config):
144 config = make_config()
146 assert config.TOKENIZER_CONFIG == ''
147 assert not config.get_bool('TOKENIZER_CONFIG')
150 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
151 ('85762513444', 85762513444)])
152 def test_get_int_success(make_config, monkeypatch, value, result):
153 config = make_config()
155 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
157 assert config.get_int('FOOBAR') == result
160 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
161 def test_get_int_bad_values(make_config, monkeypatch, value):
162 config = make_config()
164 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
166 with pytest.raises(UsageError):
167 config.get_int('FOOBAR')
170 def test_get_int_empty(make_config):
171 config = make_config()
173 assert config.TOKENIZER_CONFIG == ''
175 with pytest.raises(UsageError):
176 config.get_int('TOKENIZER_CONFIG')
179 @pytest.mark.parametrize("value,outlist", [('sd', ['sd']),
180 ('dd,rr', ['dd', 'rr']),
181 (' a , b ', ['a', 'b'])])
182 def test_get_str_list_success(make_config, monkeypatch, value, outlist):
183 config = make_config()
185 monkeypatch.setenv('NOMINATIM_MYLIST', value)
187 assert config.get_str_list('MYLIST') == outlist
190 def test_get_str_list_empty(make_config):
191 config = make_config()
193 assert config.get_str_list('LANGUAGES') is None
196 def test_get_path_empty(make_config):
197 config = make_config()
199 assert config.TOKENIZER_CONFIG == ''
200 assert not config.get_path('TOKENIZER_CONFIG')
203 def test_get_path_absolute(make_config, monkeypatch, tmp_path):
204 config = make_config()
206 p = (tmp_path / "does_not_exist").resolve()
207 monkeypatch.setenv('NOMINATIM_FOOBAR', str(p))
208 result = config.get_path('FOOBAR')
210 assert isinstance(result, Path)
211 assert str(result) == str(p)
214 def test_get_path_relative(make_config, monkeypatch, tmp_path):
215 config = make_config(tmp_path)
217 monkeypatch.setenv('NOMINATIM_FOOBAR', 'an/oyster')
218 result = config.get_path('FOOBAR')
220 assert isinstance(result, Path)
221 assert str(result) == str(tmp_path / 'an/oyster')
224 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
225 config = make_config()
227 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
229 expected = src_dir / 'lib-lua' / 'import-street.lua'
231 assert config.get_import_style_file() == expected
234 def test_get_import_style_extern_relative(make_config_path, monkeypatch):
235 config = make_config_path()
236 (config.project_dir / 'custom.style').write_text('x', encoding='utf-8')
238 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'custom.style')
240 assert str(config.get_import_style_file()) == str(config.project_dir / 'custom.style')
243 def test_get_import_style_extern_absolute(make_config, tmp_path, monkeypatch):
244 config = make_config()
245 cfgfile = tmp_path / 'test.style'
247 cfgfile.write_text('x', encoding='utf-8')
249 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', str(cfgfile))
251 assert str(config.get_import_style_file()) == str(cfgfile)
254 def test_load_subconf_from_project_dir(make_config_path):
255 config = make_config_path()
257 testfile = config.project_dir / 'test.yaml'
258 testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
260 testfile = config.config_dir / 'test.yaml'
261 testfile.write_text('cow: miau\ncat: muh\n', encoding='utf-8')
263 rules = config.load_sub_configuration('test.yaml')
265 assert rules == dict(cow='muh', cat='miau')
268 def test_load_subconf_from_settings_dir(make_config_path):
269 config = make_config_path()
271 testfile = config.config_dir / 'test.yaml'
272 testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
274 rules = config.load_sub_configuration('test.yaml')
276 assert rules == dict(cow='muh', cat='miau')
279 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
280 monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
281 config = make_config_path()
283 testfile = config.config_dir / 'test.yaml'
284 testfile.write_text('cow: muh\ncat: miau\n', encoding='utf-8')
286 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
288 assert rules == dict(cow='muh', cat='miau')
291 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
292 monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
293 config = make_config_path()
295 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
296 (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n', encoding='utf-8')
298 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
300 assert rules == dict(dog='muh', frog='miau')
303 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
304 monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
305 config = make_config_path()
307 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
309 with pytest.raises(UsageError, match='Config file not found.'):
310 config.load_sub_configuration('test.yaml', config='MY_CONFIG')
313 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
314 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
315 monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
316 config = make_config_path()
318 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
319 (getattr(config, location) / 'other.yaml').write_text('dog: bark\n', encoding='utf-8')
321 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
323 assert rules == dict(dog='bark')
326 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
327 monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
328 config = make_config_path()
330 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n', encoding='utf-8')
332 with pytest.raises(UsageError, match='Config file not found.'):
333 config.load_sub_configuration('test.yaml', config='MY_CONFIG')
336 def test_load_subconf_json(make_config_path):
337 config = make_config_path()
339 (config.project_dir / 'test.json').write_text('{"cow": "muh", "cat": "miau"}', encoding='utf-8')
341 rules = config.load_sub_configuration('test.json')
343 assert rules == dict(cow='muh', cat='miau')
346 def test_load_subconf_not_found(make_config_path):
347 config = make_config_path()
349 with pytest.raises(UsageError, match='Config file not found.'):
350 config.load_sub_configuration('test.yaml')
353 def test_load_subconf_env_unknown_format(make_config_path):
354 config = make_config_path()
356 (config.project_dir / 'test.xml').write_text('<html></html>', encoding='utf-8')
358 with pytest.raises(UsageError, match='unknown format'):
359 config.load_sub_configuration('test.xml')
362 def test_load_subconf_include_absolute(make_config_path, tmp_path):
363 config = make_config_path()
365 testfile = config.config_dir / 'test.yaml'
366 testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n', encoding='utf-8')
367 (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
369 rules = config.load_sub_configuration('test.yaml')
371 assert rules == dict(base=dict(first=1, second=2))
374 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
375 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
376 config = make_config_path()
378 testfile = config.config_dir / 'test.yaml'
379 testfile.write_text('base: !include inc.yaml\n', encoding='utf-8')
380 (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
382 rules = config.load_sub_configuration('test.yaml')
384 assert rules == dict(base=dict(first=1, second=2))
387 def test_load_subconf_include_bad_format(make_config_path):
388 config = make_config_path()
390 testfile = config.config_dir / 'test.yaml'
391 testfile.write_text('base: !include inc.txt\n', encoding='utf-8')
392 (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n', encoding='utf-8')
394 with pytest.raises(UsageError, match='Cannot handle config file format.'):
395 config.load_sub_configuration('test.yaml')
398 def test_load_subconf_include_not_found(make_config_path):
399 config = make_config_path()
401 testfile = config.config_dir / 'test.yaml'
402 testfile.write_text('base: !include inc.txt\n', encoding='utf-8')
404 with pytest.raises(UsageError, match='Config file not found.'):
405 config.load_sub_configuration('test.yaml')
408 def test_load_subconf_include_recursive(make_config_path):
409 config = make_config_path()
411 testfile = config.config_dir / 'test.yaml'
412 testfile.write_text('base: !include inc.yaml\n', encoding='utf-8')
413 (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n', encoding='utf-8')
414 (config.config_dir / 'more.yaml').write_text('- the end\n', encoding='utf-8')
416 rules = config.load_sub_configuration('test.yaml')
418 assert rules == dict(base=[['the end'], 'upper'])
421 @pytest.mark.parametrize("content", [[], None])
422 def test_flatten_config_list_empty(content):
423 assert flatten_config_list(content) == []
426 @pytest.mark.parametrize("content", [{'foo': 'bar'}, 'hello world', 3])
427 def test_flatten_config_list_no_list(content):
428 with pytest.raises(UsageError):
429 flatten_config_list(content)
432 def test_flatten_config_list_allready_flat():
433 assert flatten_config_list([1, 2, 456]) == [1, 2, 456]
436 def test_flatten_config_list_nested():
439 [{'first': '1st', 'second': '2nd'}, {}],
440 [[2, 3], [45, [56, 78], 66]],
444 assert flatten_config_list(content) == \
445 [34, {'first': '1st', 'second': '2nd'}, {}, 2, 3, 45, 56, 78, 66, 'end']