From af52eed0dda746f2a495cd6e33e777c43c04d0b3 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 19 May 2021 16:42:35 +0200 Subject: [PATCH] test: avoid use of tempfile module Use the tmp_path fixture instead which provides automatic cleanup. --- test/python/conftest.py | 13 +- test/python/test_tools_exec_utils.py | 123 +++++++++--------- .../test_tools_import_special_phrases.py | 15 +-- 3 files changed, 68 insertions(+), 83 deletions(-) diff --git a/test/python/conftest.py b/test/python/conftest.py index 77093c4e..c2544948 100644 --- a/test/python/conftest.py +++ b/test/python/conftest.py @@ -1,6 +1,5 @@ import itertools import sys -import tempfile from pathlib import Path import psycopg2 @@ -88,6 +87,9 @@ def temp_db_cursor(temp_db): @pytest.fixture def table_factory(temp_db_cursor): + """ A fixture that creates new SQL tables, potentially filled with + content. + """ def mk_table(name, definition='id INT', content=None): temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition)) if content is not None: @@ -127,18 +129,11 @@ def cli_call(): return _call_nominatim -@pytest.fixture -def tmp_phplib_dir(): - with tempfile.TemporaryDirectory() as phpdir: - (Path(phpdir) / 'admin').mkdir() - - yield Path(phpdir) - - @pytest.fixture def property_table(table_factory): table_factory('nominatim_properties', 'property TEXT, value TEXT') + @pytest.fixture def status_table(temp_db_conn): """ Create an empty version of the status table and diff --git a/test/python/test_tools_exec_utils.py b/test/python/test_tools_exec_utils.py index 3abe9818..e713300e 100644 --- a/test/python/test_tools_exec_utils.py +++ b/test/python/test_tools_exec_utils.py @@ -3,97 +3,90 @@ Tests for tools.exec_utils module. """ from pathlib import Path import subprocess -import tempfile import pytest import nominatim.tools.exec_utils as exec_utils -@pytest.fixture -def nominatim_env(tmp_phplib_dir, def_config): - class _NominatimEnv: - config = def_config - phplib_dir = tmp_phplib_dir - data_dir = Path('data') - project_dir = Path('.') - sqllib_dir = Path('lib-sql') - config_dir = Path('settings') - module_dir = 'module' - osm2pgsql_path = 'osm2pgsql' - - return _NominatimEnv - -@pytest.fixture -def test_script(nominatim_env): - def _create_file(code): - with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd: - fd.write('