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 Test for loading dotenv configuration.
10 from pathlib import Path
13 from nominatim.config import Configuration, flatten_config_list
14 from nominatim.errors import UsageError
18 """ Create a configuration object from the given project directory.
20 def _mk_config(project_dir=None):
21 return Configuration(project_dir)
26 def make_config_path(tmp_path):
27 """ Create a configuration object with project and config directories
28 in a temporary directory.
31 (tmp_path / 'project').mkdir()
32 (tmp_path / 'config').mkdir()
33 conf = Configuration(tmp_path / 'project')
34 conf.config_dir = tmp_path / 'config'
40 def test_no_project_dir(make_config):
41 config = make_config()
43 assert config.DATABASE_WEBUSER == 'www-data'
46 @pytest.mark.parametrize("val", ('apache', '"apache"'))
47 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
48 envfile = tmp_path / '.env'
49 envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
51 config = make_config(tmp_path)
53 assert config.DATABASE_WEBUSER == 'apache'
56 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
57 envfile = tmp_path / '.env'
58 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
60 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
62 config = make_config(tmp_path)
64 assert config.DATABASE_WEBUSER == 'nobody'
67 def test_prefer_os_environ_can_unset_project_setting(make_config, monkeypatch, tmp_path):
68 envfile = tmp_path / '.env'
69 envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
71 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', '')
73 config = make_config(tmp_path)
75 assert config.DATABASE_WEBUSER == ''
78 def test_get_os_env_add_defaults(make_config, monkeypatch):
79 config = make_config()
81 monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
83 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
86 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
87 config = make_config()
89 monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
91 assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
94 def test_get_libpq_dsn_convert_default(make_config):
95 config = make_config()
97 assert config.get_libpq_dsn() == 'dbname=nominatim'
100 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
101 config = make_config()
103 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
104 'pgsql:dbname=gis;password=foo;host=localhost')
106 assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
109 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
112 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
113 config = make_config()
115 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
116 'pgsql:dbname=gis;password={}'.format(val))
118 assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
121 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
122 config = make_config()
124 monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
125 'host=localhost dbname=gis password=foo')
127 assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
130 @pytest.mark.parametrize("value,result",
131 [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
132 [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
133 def test_get_bool(make_config, monkeypatch, value, result):
134 config = make_config()
136 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
138 assert config.get_bool('FOOBAR') == result
140 def test_get_bool_empty(make_config):
141 config = make_config()
143 assert config.DATABASE_MODULE_PATH == ''
144 assert not config.get_bool('DATABASE_MODULE_PATH')
147 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
148 ('85762513444', 85762513444)])
149 def test_get_int_success(make_config, monkeypatch, value, result):
150 config = make_config()
152 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
154 assert config.get_int('FOOBAR') == result
157 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
158 def test_get_int_bad_values(make_config, monkeypatch, value):
159 config = make_config()
161 monkeypatch.setenv('NOMINATIM_FOOBAR', value)
163 with pytest.raises(UsageError):
164 config.get_int('FOOBAR')
167 def test_get_int_empty(make_config):
168 config = make_config()
170 assert config.DATABASE_MODULE_PATH == ''
172 with pytest.raises(UsageError):
173 config.get_int('DATABASE_MODULE_PATH')
176 @pytest.mark.parametrize("value,outlist", [('sd', ['sd']),
177 ('dd,rr', ['dd', 'rr']),
178 (' a , b ', ['a', 'b'])])
179 def test_get_str_list_success(make_config, monkeypatch, value, outlist):
180 config = make_config()
182 monkeypatch.setenv('NOMINATIM_MYLIST', value)
184 assert config.get_str_list('MYLIST') == outlist
187 def test_get_str_list_empty(make_config):
188 config = make_config()
190 assert config.get_str_list('LANGUAGES') is None
193 def test_get_path_empty(make_config):
194 config = make_config()
196 assert config.DATABASE_MODULE_PATH == ''
197 assert not config.get_path('DATABASE_MODULE_PATH')
200 def test_get_path_absolute(make_config, monkeypatch):
201 config = make_config()
203 monkeypatch.setenv('NOMINATIM_FOOBAR', '/dont/care')
204 result = config.get_path('FOOBAR')
206 assert isinstance(result, Path)
207 assert str(result) == '/dont/care'
210 def test_get_path_relative(make_config, monkeypatch, tmp_path):
211 config = make_config(tmp_path)
213 monkeypatch.setenv('NOMINATIM_FOOBAR', 'an/oyster')
214 result = config.get_path('FOOBAR')
216 assert isinstance(result, Path)
217 assert str(result) == str(tmp_path / 'an/oyster')
220 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
221 config = make_config()
223 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
225 expected = src_dir / 'settings' / 'import-street.style'
227 assert config.get_import_style_file() == expected
230 def test_get_import_style_extern_relative(make_config_path, monkeypatch):
231 config = make_config_path()
232 (config.project_dir / 'custom.style').write_text('x')
234 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'custom.style')
236 assert str(config.get_import_style_file()) == str(config.project_dir / 'custom.style')
239 def test_get_import_style_extern_absolute(make_config, tmp_path, monkeypatch):
240 config = make_config()
241 cfgfile = tmp_path / 'test.style'
243 cfgfile.write_text('x')
245 monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', str(cfgfile))
247 assert str(config.get_import_style_file()) == str(cfgfile)
250 def test_load_subconf_from_project_dir(make_config_path):
251 config = make_config_path()
253 testfile = config.project_dir / 'test.yaml'
254 testfile.write_text('cow: muh\ncat: miau\n')
256 testfile = config.config_dir / 'test.yaml'
257 testfile.write_text('cow: miau\ncat: muh\n')
259 rules = config.load_sub_configuration('test.yaml')
261 assert rules == dict(cow='muh', cat='miau')
264 def test_load_subconf_from_settings_dir(make_config_path):
265 config = make_config_path()
267 testfile = config.config_dir / 'test.yaml'
268 testfile.write_text('cow: muh\ncat: miau\n')
270 rules = config.load_sub_configuration('test.yaml')
272 assert rules == dict(cow='muh', cat='miau')
275 def test_load_subconf_empty_env_conf(make_config_path, monkeypatch):
276 monkeypatch.setenv('NOMINATIM_MY_CONFIG', '')
277 config = make_config_path()
279 testfile = config.config_dir / 'test.yaml'
280 testfile.write_text('cow: muh\ncat: miau\n')
282 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
284 assert rules == dict(cow='muh', cat='miau')
287 def test_load_subconf_env_absolute_found(make_config_path, monkeypatch, tmp_path):
288 monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
289 config = make_config_path()
291 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
292 (tmp_path / 'other.yaml').write_text('dog: muh\nfrog: miau\n')
294 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
296 assert rules == dict(dog='muh', frog='miau')
299 def test_load_subconf_env_absolute_not_found(make_config_path, monkeypatch, tmp_path):
300 monkeypatch.setenv('NOMINATIM_MY_CONFIG', str(tmp_path / 'other.yaml'))
301 config = make_config_path()
303 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
305 with pytest.raises(UsageError, match='Config file not found.'):
306 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
309 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
310 def test_load_subconf_env_relative_found(make_config_path, monkeypatch, location):
311 monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
312 config = make_config_path()
314 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
315 (getattr(config, location) / 'other.yaml').write_text('dog: bark\n')
317 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
319 assert rules == dict(dog='bark')
322 def test_load_subconf_env_relative_not_found(make_config_path, monkeypatch):
323 monkeypatch.setenv('NOMINATIM_MY_CONFIG', 'other.yaml')
324 config = make_config_path()
326 (config.config_dir / 'test.yaml').write_text('cow: muh\ncat: miau\n')
328 with pytest.raises(UsageError, match='Config file not found.'):
329 rules = config.load_sub_configuration('test.yaml', config='MY_CONFIG')
332 def test_load_subconf_json(make_config_path):
333 config = make_config_path()
335 (config.project_dir / 'test.json').write_text('{"cow": "muh", "cat": "miau"}')
337 rules = config.load_sub_configuration('test.json')
339 assert rules == dict(cow='muh', cat='miau')
341 def test_load_subconf_not_found(make_config_path):
342 config = make_config_path()
344 with pytest.raises(UsageError, match='Config file not found.'):
345 config.load_sub_configuration('test.yaml')
348 def test_load_subconf_env_unknown_format(make_config_path):
349 config = make_config_path()
351 (config.project_dir / 'test.xml').write_text('<html></html>')
353 with pytest.raises(UsageError, match='unknown format'):
354 config.load_sub_configuration('test.xml')
357 def test_load_subconf_include_absolute(make_config_path, tmp_path):
358 config = make_config_path()
360 testfile = config.config_dir / 'test.yaml'
361 testfile.write_text(f'base: !include {tmp_path}/inc.yaml\n')
362 (tmp_path / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
364 rules = config.load_sub_configuration('test.yaml')
366 assert rules == dict(base=dict(first=1, second=2))
369 @pytest.mark.parametrize("location", ['project_dir', 'config_dir'])
370 def test_load_subconf_include_relative(make_config_path, tmp_path, location):
371 config = make_config_path()
373 testfile = config.config_dir / 'test.yaml'
374 testfile.write_text(f'base: !include inc.yaml\n')
375 (getattr(config, location) / 'inc.yaml').write_text('first: 1\nsecond: 2\n')
377 rules = config.load_sub_configuration('test.yaml')
379 assert rules == dict(base=dict(first=1, second=2))
382 def test_load_subconf_include_bad_format(make_config_path):
383 config = make_config_path()
385 testfile = config.config_dir / 'test.yaml'
386 testfile.write_text(f'base: !include inc.txt\n')
387 (config.config_dir / 'inc.txt').write_text('first: 1\nsecond: 2\n')
389 with pytest.raises(UsageError, match='Cannot handle config file format.'):
390 rules = config.load_sub_configuration('test.yaml')
393 def test_load_subconf_include_not_found(make_config_path):
394 config = make_config_path()
396 testfile = config.config_dir / 'test.yaml'
397 testfile.write_text(f'base: !include inc.txt\n')
399 with pytest.raises(UsageError, match='Config file not found.'):
400 rules = config.load_sub_configuration('test.yaml')
403 def test_load_subconf_include_recursive(make_config_path):
404 config = make_config_path()
406 testfile = config.config_dir / 'test.yaml'
407 testfile.write_text(f'base: !include inc.yaml\n')
408 (config.config_dir / 'inc.yaml').write_text('- !include more.yaml\n- upper\n')
409 (config.config_dir / 'more.yaml').write_text('- the end\n')
411 rules = config.load_sub_configuration('test.yaml')
413 assert rules == dict(base=[['the end'], 'upper'])
416 @pytest.mark.parametrize("content", [[], None])
417 def test_flatten_config_list_empty(content):
418 assert flatten_config_list(content) == []
421 @pytest.mark.parametrize("content", [{'foo': 'bar'}, 'hello world', 3])
422 def test_flatten_config_list_no_list(content):
423 with pytest.raises(UsageError):
424 flatten_config_list(content)
427 def test_flatten_config_list_allready_flat():
428 assert flatten_config_list([1, 2, 456]) == [1, 2, 456]
431 def test_flatten_config_list_nested():
434 [{'first': '1st', 'second': '2nd'}, {}],
435 [[2, 3], [45, [56, 78], 66]],
438 assert flatten_config_list(content) == \
439 [34, {'first': '1st', 'second': '2nd'}, {},
440 2, 3, 45, 56, 78, 66, 'end']