From b6a7f62454850e14eb871b2a0ef3af0677bba189 Mon Sep 17 00:00:00 2001 From: Frode Aarstad Date: Fri, 4 Oct 2024 12:42:22 +0200 Subject: [PATCH] Refactor everest tmpdir fixture --- tests/everest/__init__.py | 3 - tests/everest/conftest.py | 41 +++++ .../entry_points/test_config_branch_entry.py | 11 +- .../entry_points/test_everest_entry.py | 43 ++--- tests/everest/entry_points/test_everexport.py | 42 +++-- .../functional/test_main_everest_entry.py | 20 +-- tests/everest/functional/test_ui.py | 5 +- tests/everest/test_api.py | 10 +- tests/everest/test_config_branch.py | 9 +- tests/everest/test_config_file_loader.py | 18 +- tests/everest/test_config_validation.py | 37 ++--- tests/everest/test_controls.py | 46 +++-- tests/everest/test_cvar.py | 5 +- tests/everest/test_detached.py | 33 ++-- tests/everest/test_discrete.py | 5 +- tests/everest/test_egg_simulation.py | 31 ++-- tests/everest/test_environment.py | 16 +- tests/everest/test_everest_initialization.py | 17 +- tests/everest/test_everest_output.py | 12 +- tests/everest/test_everjobs_io.py | 7 +- tests/everest/test_everserver.py | 30 ++-- tests/everest/test_export.py | 38 ++--- tests/everest/test_export_data_dialog.py | 4 +- tests/everest/test_fix_control.py | 5 +- tests/everest/test_logging.py | 5 +- tests/everest/test_math_func.py | 24 +-- tests/everest/test_multiobjective.py | 14 +- tests/everest/test_objective_type.py | 5 +- tests/everest/test_optimization_config.py | 4 +- tests/everest/test_output_constraints.py | 14 +- tests/everest/test_res_initialization.py | 157 ++++++++---------- tests/everest/test_ropt_initialization.py | 55 ++---- tests/everest/test_samplers.py | 8 +- tests/everest/test_simulator_cache.py | 69 ++++---- tests/everest/test_templating.py | 24 +-- tests/everest/test_ui_run.py | 5 +- tests/everest/test_util.py | 21 +-- tests/everest/test_well_tools.py | 43 ++--- tests/everest/test_workflows.py | 5 +- tests/everest/test_yaml_parser.py | 20 +-- .../everest/unit/everest/bin/test_everload.py | 36 ++-- tests/everest/utils/__init__.py | 56 ------- 42 files changed, 417 insertions(+), 636 deletions(-) diff --git a/tests/everest/__init__.py b/tests/everest/__init__.py index 06b590ab516..e69de29bb2d 100644 --- a/tests/everest/__init__.py +++ b/tests/everest/__init__.py @@ -1,3 +0,0 @@ -from .utils import tmp - -__all__ = ["tmp"] diff --git a/tests/everest/conftest.py b/tests/everest/conftest.py index 62dbfbb281e..04b6d888e9f 100644 --- a/tests/everest/conftest.py +++ b/tests/everest/conftest.py @@ -7,6 +7,7 @@ import pytest from everest.config.control_config import ControlConfig +from tests.everest.utils import relpath @pytest.fixture(scope="session") @@ -82,3 +83,43 @@ def control_config( config = deepcopy(control_data_no_variables) config["variables"] = request.param return ControlConfig.model_validate(config) + + +@pytest.fixture +def copy_math_func_test_data_to_tmp(tmp_path, monkeypatch): + path = relpath("..", "..", "test-data", "everest", "math_func") + shutil.copytree(path, tmp_path, dirs_exist_ok=True) + monkeypatch.chdir(tmp_path) + + +@pytest.fixture +def copy_mocked_test_data_to_tmp(tmp_path, monkeypatch): + path = relpath("test_data", "mocked_test_case") + shutil.copytree(path, tmp_path, dirs_exist_ok=True) + monkeypatch.chdir(tmp_path) + + +@pytest.fixture +def copy_test_data_to_tmp(tmp_path, monkeypatch): + path = relpath("test_data") + shutil.copytree(path, tmp_path, dirs_exist_ok=True) + monkeypatch.chdir(tmp_path) + + +@pytest.fixture +def copy_template_test_data_to_tmp(tmp_path, monkeypatch): + path = relpath("test_data", "templating") + shutil.copytree(path, tmp_path, dirs_exist_ok=True) + monkeypatch.chdir(tmp_path) + + +@pytest.fixture +def copy_egg_test_data_to_tmp(tmp_path, monkeypatch): + path = relpath("..", "..", "test-data", "everest", "egg") + shutil.copytree(path, tmp_path, dirs_exist_ok=True) + monkeypatch.chdir(tmp_path) + + +@pytest.fixture +def change_to_tmpdir(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) diff --git a/tests/everest/entry_points/test_config_branch_entry.py b/tests/everest/entry_points/test_config_branch_entry.py index 1fca14d086c..c7af83df306 100644 --- a/tests/everest/entry_points/test_config_branch_entry.py +++ b/tests/everest/entry_points/test_config_branch_entry.py @@ -8,9 +8,8 @@ from everest.config import EverestConfig from everest.config_file_loader import load_yaml from everest.config_keys import ConfigKeys as CK -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE = "config_advanced.yml" CACHED_SEBA_FOLDER = relpath("test_data", "cached_results_config_advanced") @@ -22,8 +21,7 @@ new_callable=PropertyMock, return_value=CACHED_SEBA_FOLDER, ) -@tmpdir(CONFIG_PATH) -def test_config_branch_entry(get_opt_output_dir_mock): +def test_config_branch_entry(get_opt_output_dir_mock, copy_math_func_test_data_to_tmp): new_config_file_name = "new_restart_config.yml" batch_id = 1 @@ -65,8 +63,9 @@ def test_config_branch_entry(get_opt_output_dir_mock): new_callable=PropertyMock, return_value=CACHED_SEBA_FOLDER, ) -@tmpdir(CONFIG_PATH) -def test_config_branch_preserves_config_section_order(get_opt_output_dir_mock): +def test_config_branch_preserves_config_section_order( + get_opt_output_dir_mock, copy_math_func_test_data_to_tmp +): new_config_file_name = "new_restart_config.yml" batch_id = 1 diff --git a/tests/everest/entry_points/test_everest_entry.py b/tests/everest/entry_points/test_everest_entry.py index 1156f1d36aa..472b8ea8066 100644 --- a/tests/everest/entry_points/test_everest_entry.py +++ b/tests/everest/entry_points/test_everest_entry.py @@ -18,9 +18,8 @@ from everest.jobs import shell_commands from everest.simulator import JOB_SUCCESS from ieverest.bin.ieverest_script import ieverest_entry -from tests.everest.utils import capture_streams, relpath, tmpdir +from tests.everest.utils import capture_streams -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_MINIMAL = "config_minimal.yml" @@ -80,13 +79,13 @@ def run_detached_monitor_mock( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_debug( everserver_status_mock, start_server_mock, wait_for_server_mock, start_monitor_mock, caplog, + copy_math_func_test_data_to_tmp, ): """Test running everest with --debug""" with caplog.at_level(logging.DEBUG): @@ -111,12 +110,12 @@ def test_everest_entry_debug( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry( everserver_status_mock, start_server_mock, wait_for_server_mock, start_monitor_mock, + copy_math_func_test_data_to_tmp, ): """Test running everest in detached mode""" everest_entry([CONFIG_FILE_MINIMAL]) @@ -134,13 +133,13 @@ def test_everest_entry( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.completed, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_detached_already_run( everserver_status_mock, start_server_mock, wait_for_server_mock, start_monitor_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test everest detached, when an optimization has already run""" # optimization already run, notify the user @@ -171,11 +170,11 @@ def test_everest_entry_detached_already_run( "everest.bin.monitor_script.everserver_status", return_value={"status": ServerStatus.completed, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_detached_already_run_monitor( everserver_status_mock, start_monitor_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test everest detached, when an optimization has already run""" # optimization already run, notify the user @@ -199,7 +198,6 @@ def test_everest_entry_detached_already_run_monitor( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.completed, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_detached_running( everserver_status_mock, wait_for_server_to_stop_mock, @@ -209,6 +207,7 @@ def test_everest_entry_detached_running( start_monitor_mock, server_is_running_mock_kill_script, server_is_running_mock_everest_script, + copy_math_func_test_data_to_tmp, ): """Test everest detached, optimization is running""" # can't start a new run if one is already running @@ -248,11 +247,11 @@ def test_everest_entry_detached_running( "everest.bin.monitor_script.everserver_status", return_value={"status": ServerStatus.completed, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_detached_running_monitor( everserver_status_mock, start_monitor_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test everest detached, optimization is running, monitoring""" # Attach to a running optimization. @@ -269,11 +268,11 @@ def test_everest_entry_detached_running_monitor( "everest.bin.monitor_script.everserver_status", return_value={"status": ServerStatus.completed, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_monitor_no_run( everserver_status_mock, start_monitor_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test everest detached, optimization is running, monitoring""" # Attach to a running optimization. @@ -300,7 +299,6 @@ def test_everest_entry_monitor_no_run( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_show_all_jobs( everserver_status_mock, get_opt_status_mock, @@ -309,6 +307,7 @@ def test_everest_entry_show_all_jobs( start_server_mock, wait_for_server_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test running everest with --show-all-jobs""" @@ -335,7 +334,6 @@ def test_everest_entry_show_all_jobs( "everest.bin.everest_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_everest_entry_no_show_all_jobs( everserver_status_mock, get_opt_status_mock, @@ -344,6 +342,7 @@ def test_everest_entry_no_show_all_jobs( start_server_mock, wait_for_server_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test running everest without --show-all-jobs""" @@ -372,13 +371,13 @@ def test_everest_entry_no_show_all_jobs( "everest.bin.monitor_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_monitor_entry_show_all_jobs( everserver_status_mock, get_opt_status_mock, get_server_context_mock, query_server_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test running everest with and without --show-all-jobs""" @@ -404,13 +403,13 @@ def test_monitor_entry_show_all_jobs( "everest.bin.monitor_script.everserver_status", return_value={"status": ServerStatus.never_run, "message": None}, ) -@tmpdir(CONFIG_PATH) def test_monitor_entry_no_show_all_jobs( everserver_status_mock, get_opt_status_mock, get_server_context_mock, query_server_mock, server_is_running_mock, + copy_math_func_test_data_to_tmp, ): """Test running everest without --show-all-jobs""" @@ -436,9 +435,11 @@ def test_monitor_entry_no_show_all_jobs( ) @patch("everest.bin.everest_script.wait_for_server") @patch("everest.bin.everest_script.start_server") -@tmpdir(CONFIG_PATH) def test_exception_raised_when_server_run_fails( - start_server_mock, wait_for_server_mock, start_monitor_mock + start_server_mock, + wait_for_server_mock, + start_monitor_mock, + copy_math_func_test_data_to_tmp, ): with pytest.raises(SystemExit, match="Reality was ripped to shreds!"): everest_entry([CONFIG_FILE_MINIMAL]) @@ -453,9 +454,8 @@ def test_exception_raised_when_server_run_fails( error="Reality was ripped to shreds!", ), ) -@tmpdir(CONFIG_PATH) def test_exception_raised_when_server_run_fails_monitor( - start_monitor_mock, server_is_running_mock + start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp ): with pytest.raises(SystemExit, match="Reality was ripped to shreds!"): monitor_entry([CONFIG_FILE_MINIMAL]) @@ -467,9 +467,11 @@ def test_exception_raised_when_server_run_fails_monitor( ) @patch("everest.bin.everest_script.wait_for_server") @patch("everest.bin.everest_script.start_server") -@tmpdir(CONFIG_PATH) def test_complete_status_for_normal_run( - start_server_mock, wait_for_server_mock, start_monitor_mock + start_server_mock, + wait_for_server_mock, + start_monitor_mock, + copy_math_func_test_data_to_tmp, ): everest_entry([CONFIG_FILE_MINIMAL]) config = EverestConfig.load_file(CONFIG_FILE_MINIMAL) @@ -486,9 +488,8 @@ def test_complete_status_for_normal_run( "everest.bin.monitor_script.run_detached_monitor", side_effect=run_detached_monitor_mock, ) -@tmpdir(CONFIG_PATH) def test_complete_status_for_normal_run_monitor( - start_monitor_mock, server_is_running_mock + start_monitor_mock, server_is_running_mock, copy_math_func_test_data_to_tmp ): monitor_entry([CONFIG_FILE_MINIMAL]) config = EverestConfig.load_file(CONFIG_FILE_MINIMAL) diff --git a/tests/everest/entry_points/test_everexport.py b/tests/everest/entry_points/test_everexport.py index 1d9fb5b60da..383d50c5c5a 100644 --- a/tests/everest/entry_points/test_everexport.py +++ b/tests/everest/entry_points/test_everexport.py @@ -13,17 +13,14 @@ from everest.config import EverestConfig from tests.everest.utils import ( create_cached_mocked_test_case, - relpath, satisfy, satisfy_callable, - tmpdir, ) -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_MINIMAL = "config_minimal.yml" CONFIG_FILE_MOCKED_TEST_CASE = "mocked_multi_batch.yml" -CONFIG_PATH_MOCKED_TEST_CASE = relpath("test_data", "mocked_test_case") + pytestmark = pytest.mark.xdist_group(name="starts_everest") @@ -71,8 +68,7 @@ def cache_dir(request, monkeypatch): @patch("everest.bin.utils.export_with_progress", side_effect=export_mock) -@tmpdir(CONFIG_PATH) -def test_everexport_entry_run(mocked_func): +def test_everexport_entry_run(mocked_func, copy_math_func_test_data_to_tmp): """Test running everexport with not flags""" # NOTE: there is probably a bug concerning output folders. Everexport # seems to assume that the folder where the file will be saved exists. @@ -88,8 +84,7 @@ def test_everexport_entry_run(mocked_func): @patch("everest.bin.utils.export_with_progress", side_effect=empty_mock) -@tmpdir(CONFIG_PATH) -def test_everexport_entry_empty(mocked_func): +def test_everexport_entry_empty(mocked_func, copy_math_func_test_data_to_tmp): """Test running everexport with no data""" # NOTE: When there is no data (ie, the optimization has not yet run) # the current behavior is to create an empty .csv file. It is arguable @@ -112,9 +107,10 @@ def test_everexport_entry_empty(mocked_func): side_effect=validate_export_mock, ) @patch("everest.bin.utils.export") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everexport_entry_batches(mocked_func, validate_export_mock): +def test_everexport_entry_batches( + mocked_func, validate_export_mock, copy_math_func_test_data_to_tmp +): """Test running everexport with the --batches flag""" everexport_entry([CONFIG_FILE_MINIMAL, "--batches", "0", "2"]) @@ -135,8 +131,7 @@ def check_export_batches(config: EverestConfig): @patch("everest.bin.everexport_script.export_to_csv") -@tmpdir(CONFIG_PATH) -def test_everexport_entry_no_export(mocked_func): +def test_everexport_entry_no_export(mocked_func, copy_math_func_test_data_to_tmp): """Test running everexport on config file with skip_export flag set to true""" @@ -157,8 +152,7 @@ def test_everexport_entry_no_export(mocked_func): @patch("everest.bin.everexport_script.export_to_csv") -@tmpdir(CONFIG_PATH) -def test_everexport_entry_empty_export(mocked_func): +def test_everexport_entry_empty_export(mocked_func, copy_math_func_test_data_to_tmp): """Test running everexport on config file with empty export section""" # Add empty export section to config file @@ -171,9 +165,10 @@ def test_everexport_entry_empty_export(mocked_func): @patch("everest.bin.utils.export") -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_everexport_entry_no_usr_def_ecl_keys(mocked_func): +def test_everexport_entry_no_usr_def_ecl_keys( + mocked_func, copy_mocked_test_data_to_tmp +): """Test running everexport with config file containing only the keywords label without any list of keys""" @@ -204,9 +199,10 @@ def condition(config: EverestConfig): @patch("everest.bin.utils.export") -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_everexport_entry_internalized_usr_def_ecl_keys(mocked_func, cache_dir): +def test_everexport_entry_internalized_usr_def_ecl_keys( + mocked_func, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everexport with config file containing a key in the list of user defined ecl keywords, that has been internalized on a previous run""" @@ -246,9 +242,10 @@ def condition(config: EverestConfig): @patch("everest.bin.utils.export") -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_everexport_entry_non_int_usr_def_ecl_keys(mocked_func, cache_dir, caplog): +def test_everexport_entry_non_int_usr_def_ecl_keys( + mocked_func, cache_dir, caplog, copy_mocked_test_data_to_tmp +): """Test running everexport when config file contains non internalized ecl keys in the user defined keywords list""" @@ -294,9 +291,10 @@ def condition(config: EverestConfig): @patch("everest.bin.utils.export") -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_everexport_entry_not_available_batches(mocked_func, cache_dir, caplog): +def test_everexport_entry_not_available_batches( + mocked_func, cache_dir, caplog, copy_mocked_test_data_to_tmp +): """Test running everexport when config file contains non existing batch numbers in the list of user defined batches""" diff --git a/tests/everest/functional/test_main_everest_entry.py b/tests/everest/functional/test_main_everest_entry.py index 42af2f6423c..279e63bc3b0 100644 --- a/tests/everest/functional/test_main_everest_entry.py +++ b/tests/everest/functional/test_main_everest_entry.py @@ -6,9 +6,7 @@ from seba_sqlite.snapshot import SebaSnapshot from tests.everest.utils import ( capture_streams, - relpath, skipif_no_everest_models, - tmpdir, ) from everest import __version__ as everest_version @@ -21,10 +19,7 @@ wait_for_context, ) -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_MINIMAL = "config_minimal.yml" - -EGG_CONFIG_PATH = relpath("..", "..", "test-data", "everest", "egg") WELL_ORDER = "everest/model/config.yml" pytestmark = pytest.mark.xdist_group(name="starts_everest") @@ -84,9 +79,8 @@ def test_everest_main_entry_bad_command(): @pytest.mark.flaky(reruns=5) -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everest_entry_run(): +def test_everest_entry_run(copy_math_func_test_data_to_tmp): wait_for_context() # Setup command line arguments with capture_streams(): @@ -119,8 +113,7 @@ def test_everest_entry_run(): context_stop_and_wait() -@tmpdir(CONFIG_PATH) -def test_everest_entry_monitor_no_run(): +def test_everest_entry_monitor_no_run(copy_math_func_test_data_to_tmp): with capture_streams(): start_everest(["everest", "monitor", CONFIG_FILE_MINIMAL]) @@ -132,16 +125,14 @@ def test_everest_entry_monitor_no_run(): context_stop_and_wait() -@tmpdir(CONFIG_PATH) -def test_everest_main_export_entry(): +def test_everest_main_export_entry(copy_math_func_test_data_to_tmp): # Setup command line arguments with capture_streams(): start_everest(["everest", "export", CONFIG_FILE_MINIMAL]) assert os.path.exists(os.path.join("everest_output", "config_minimal.csv")) -@tmpdir(CONFIG_PATH) -def test_everest_main_lint_entry(): +def test_everest_main_lint_entry(copy_math_func_test_data_to_tmp): # Setup command line arguments with capture_streams() as (out, err): start_everest(["everest", "lint", CONFIG_FILE_MINIMAL]) @@ -172,11 +163,10 @@ def test_everest_main_lint_entry(): assert validation_msg in err.getvalue() -@tmpdir(EGG_CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow @skipif_no_everest_models @pytest.mark.everest_models_test -def test_everest_main_configdump_entry(): +def test_everest_main_configdump_entry(copy_egg_test_data_to_tmp): # Setup command line arguments with capture_streams() as (out, _): start_everest(["everest", "render", WELL_ORDER]) diff --git a/tests/everest/functional/test_ui.py b/tests/everest/functional/test_ui.py index 94c3e7dbb77..88acc52d6b8 100644 --- a/tests/everest/functional/test_ui.py +++ b/tests/everest/functional/test_ui.py @@ -3,7 +3,6 @@ from qtpy.QtCore import Qt from seba_sqlite.snapshot import SebaSnapshot from tests.everest.dialogs_mocker import mock_dialogs_all -from tests.everest.utils import relpath, tmpdir from everest.config import EverestConfig from everest.detached import ( @@ -14,15 +13,13 @@ ) from ieverest import IEverest -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_MINIMAL = "config_minimal.yml" @pytest.mark.flaky(reruns=5) -@tmpdir(CONFIG_PATH) @pytest.mark.ui_test @pytest.mark.xdist_group(name="starts_everest") -def test_ui_optimization(qapp, qtbot, mocker): +def test_ui_optimization(qapp, qtbot, mocker, copy_math_func_test_data_to_tmp): """Load a configuration and run it from the UI""" wait_for_context() diff --git a/tests/everest/test_api.py b/tests/everest/test_api.py index 88522666fd5..4195ca67de3 100644 --- a/tests/everest/test_api.py +++ b/tests/everest/test_api.py @@ -10,7 +10,7 @@ from everest.api import EverestDataAPI from everest.config import EverestConfig from everest.detached import ServerStatus -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath # Global values used to create the mock snapshot. _functions = ["f0", "f1"] @@ -531,8 +531,7 @@ def test_get_summary_keys_single_key(_, api_no_gradient): @patch.object(EverestConfig, "optimization_output_dir", new_callable=PropertyMock) @patch("everest.api.everest_data_api.SebaSnapshot") @patch("everest.api.everest_data_api.SebaSnapshot.get_snapshot") -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_output_folder(_1, _2, _3): +def test_output_folder(_1, _2, _3, copy_math_func_test_data_to_tmp): config_file = "config_multiobj.yml" config = EverestConfig.load_file(config_file) assert config.environment is not None @@ -548,8 +547,9 @@ def test_output_folder(_1, _2, _3): "everest.api.everest_data_api.everserver_status", return_value={"status": ServerStatus.completed}, ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everest_csv(everserver_status_mock, _1, _2, _3): +def test_everest_csv( + everserver_status_mock, _1, _2, _3, copy_math_func_test_data_to_tmp +): config_file = "config_multiobj.yml" config = EverestConfig.load_file(config_file) expected = config.export_path diff --git a/tests/everest/test_config_branch.py b/tests/everest/test_config_branch.py index 4860d459c7e..94ef60f84a3 100644 --- a/tests/everest/test_config_branch.py +++ b/tests/everest/test_config_branch.py @@ -6,15 +6,13 @@ ) from everest.config_file_loader import load_yaml from everest.config_keys import ConfigKeys as CK -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE = "config_advanced.yml" CACHED_SEBA_FOLDER = relpath("test_data", "cached_results_config_advanced") -@tmpdir(CONFIG_PATH) -def test_get_controls_for_batch(): +def test_get_controls_for_batch(copy_math_func_test_data_to_tmp): assert opt_controls_by_batch(CACHED_SEBA_FOLDER, 1) is not None assert opt_controls_by_batch(CACHED_SEBA_FOLDER, 42) is None @@ -38,8 +36,7 @@ def test_get_controls_for_batch(): ) -@tmpdir(CONFIG_PATH) -def test_update_controls_initial_guess(): +def test_update_controls_initial_guess(copy_math_func_test_data_to_tmp): old_controls = load_yaml(CONFIG_FILE)[CK.CONTROLS] assert len(old_controls) == 1 diff --git a/tests/everest/test_config_file_loader.py b/tests/everest/test_config_file_loader.py index b13f9ff4bde..d1711f89c00 100644 --- a/tests/everest/test_config_file_loader.py +++ b/tests/everest/test_config_file_loader.py @@ -9,7 +9,7 @@ from everest import ConfigKeys as CK from everest import config_file_loader as loader from everest.config import EverestConfig -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath mocked_root = relpath(os.path.join("test_data", "mocked_test_case")) @@ -57,11 +57,9 @@ def test_load_config_as_yaml(): assert rendered_template is not None -@tmpdir(mocked_root) -def test_configpath_in_defs(): +def test_configpath_in_defs(copy_mocked_test_data_to_tmp): config_file = "mocked_multi_batch.yml" config = EverestConfig.load_file(config_file) - defs = { "numeric_key": 1, "bool_key": True, @@ -73,8 +71,7 @@ def test_configpath_in_defs(): assert defs == config.definitions -@tmpdir(mocked_root) -def test_dependent_definitions(): +def test_dependent_definitions(copy_mocked_test_data_to_tmp): config_file = "mocked_multi_batch.yml" with open(config_file, encoding="utf-8") as f: raw_config = YAML(typ="safe", pure=True).load(f) @@ -103,8 +100,7 @@ def test_dependent_definitions(): assert defs == config.definitions -@tmpdir(mocked_root) -def test_dependent_definitions_value_error(): +def test_dependent_definitions_value_error(copy_mocked_test_data_to_tmp): config_file = "mocked_multi_batch.yml" with open(config_file, encoding="utf-8") as f: raw_config = YAML(typ="safe", pure=True).load(f) @@ -121,16 +117,14 @@ def test_dependent_definitions_value_error(): EverestConfig.load_file(config_file) -@tmpdir(mocked_root) -def test_load_empty_configuration(): +def test_load_empty_configuration(copy_mocked_test_data_to_tmp): with open("empty_config.yml", mode="w", encoding="utf-8") as fh: fh.writelines("") with pytest.raises(ValidationError, match="missing"): EverestConfig.load_file("empty_config.yml") -@tmpdir(mocked_root) -def test_load_invalid_configuration(): +def test_load_invalid_configuration(copy_mocked_test_data_to_tmp): with open("invalid_config.yml", mode="w", encoding="utf-8") as fh: fh.writelines("asdf") with pytest.raises(ValidationError, match="missing"): diff --git a/tests/everest/test_config_validation.py b/tests/everest/test_config_validation.py index 53c16e33da1..bb43657325c 100644 --- a/tests/everest/test_config_validation.py +++ b/tests/everest/test_config_validation.py @@ -10,7 +10,6 @@ from everest.config import EverestConfig, ModelConfig from everest.config.control_variable_config import ControlVariableConfig from everest.config.sampler_config import SamplerConfig -from tests.everest.utils import tmpdir def has_error(error: Union[ValidationError, List[dict]], match: str): @@ -579,8 +578,9 @@ def test_that_min_realizations_success_is_nonnegative(): ("./", False), ], ) -@tmpdir(None) -def test_that_install_data_allows_runpath_root_as_target(target, link): +def test_that_install_data_allows_runpath_root_as_target( + target, link, change_to_tmpdir +): data = {"source": "relative/path_", "target": target, "link": link} os.makedirs("config_dir/relative/path_0") with open("config_dir/test.yml", "w", encoding="utf-8") as f: @@ -595,8 +595,7 @@ def test_that_install_data_allows_runpath_root_as_target(target, link): assert install_data_config.target == Path(data["source"]).name -@tmpdir(None) -def test_that_install_data_source_exists(): +def test_that_install_data_source_exists(change_to_tmpdir): data = { "source": "relative/path", "target": "xxx", @@ -619,8 +618,7 @@ def test_that_install_data_source_exists(): ) -@tmpdir(None) -def test_that_model_data_file_exists(): +def test_that_model_data_file_exists(change_to_tmpdir): os.makedirs("config_dir") with open("config_dir/test.yml", "w", encoding="utf-8") as f: f.write(" ") @@ -641,8 +639,7 @@ def test_that_model_data_file_exists(): ) -@tmpdir(None) -def test_that_model_report_steps_invalid_dates_errors(): +def test_that_model_report_steps_invalid_dates_errors(change_to_tmpdir): os.makedirs("config_dir/relative/path") with open("config_dir/test.yml", "w", encoding="utf-8") as f: f.write(" ") @@ -676,8 +673,7 @@ def test_that_model_report_steps_invalid_dates_errors(): ("install_workflow_jobs",), ], ) -@tmpdir(None) -def test_that_non_existing_install_job_errors(install_keyword): +def test_that_non_existing_install_job_errors(install_keyword, change_to_tmpdir): os.makedirs("config_dir") with open("config_dir/test.yml", "w", encoding="utf-8") as f: f.write(" ") @@ -700,8 +696,9 @@ def test_that_non_existing_install_job_errors(install_keyword): ("install_workflow_jobs",), ], ) -@tmpdir(None) -def test_that_existing_install_job_with_malformed_executable_errors(install_keyword): +def test_that_existing_install_job_with_malformed_executable_errors( + install_keyword, change_to_tmpdir +): with open("malformed.ert", "w+", encoding="utf-8") as f: f.write( """EXECUTABLE @@ -739,9 +736,8 @@ def test_that_existing_install_job_with_malformed_executable_errors(install_keyw ("install_workflow_jobs",), ], ) -@tmpdir(None) def test_that_existing_install_job_with_non_executable_executable_errors( - install_keyword, + install_keyword, change_to_tmpdir ): with open("exec.ert", "w+", encoding="utf-8") as f: f.write( @@ -778,8 +774,9 @@ def test_that_existing_install_job_with_non_executable_executable_errors( ("install_workflow_jobs",), ], ) -@tmpdir(None) -def test_that_existing_install_job_with_non_existing_executable_errors(install_keyword): +def test_that_existing_install_job_with_non_existing_executable_errors( + install_keyword, change_to_tmpdir +): with open("exec.ert", "w+", encoding="utf-8") as f: f.write( """EXECUTABLE non_existing @@ -864,8 +861,7 @@ def test_that_objective_function_aliases_are_consistent(): ) -@tmpdir(None) -def test_that_install_templates_must_have_unique_names(): +def test_that_install_templates_must_have_unique_names(change_to_tmpdir): for f in ["hey", "hesy", "heyyy"]: pathlib.Path(f).write_text(f, encoding="utf-8") @@ -892,8 +888,7 @@ def test_that_install_templates_must_have_unique_names(): ) -@tmpdir(None) -def test_that_install_template_template_must_be_existing_file(): +def test_that_install_template_template_must_be_existing_file(change_to_tmpdir): os.makedirs("config_dir") with open("config_dir/test.yml", "w", encoding="utf-8") as f: f.write(" ") diff --git a/tests/everest/test_controls.py b/tests/everest/test_controls.py index 90650632122..a62cf91cd78 100644 --- a/tests/everest/test_controls.py +++ b/tests/everest/test_controls.py @@ -16,7 +16,7 @@ ) from everest.config.input_constraint_config import InputConstraintConfig from everest.config.well_config import WellConfig -from tests.everest.utils import relpath, tmp, tmpdir +from tests.everest.utils import relpath cfg_dir = relpath("test_data", "mocked_test_case") mocked_config = relpath(cfg_dir, "mocked_test_case.yml") @@ -114,8 +114,7 @@ def _perturb_control_zero( return exp_var_def -@tmpdir(relpath("test_data")) -def test_variable_name_index_validation(): +def test_variable_name_index_validation(copy_test_data_to_tmp): config = EverestConfig.load_file( os.path.join("mocked_test_case", "mocked_test_case.yml") ) @@ -174,7 +173,7 @@ def test_variable_name_index_validation(): @pytest.mark.integration_test -def test_individual_control_variable_config(): +def test_individual_control_variable_config(copy_test_data_to_tmp): config_file = os.path.join("mocked_test_case", "config_input_constraints.yml") global_min = (0, 0.7, 1.3, None) @@ -184,26 +183,25 @@ def test_individual_control_variable_config(): test_base = (global_min, global_max, global_init, fill_missing) for gmin, gmax, ginit, fill in itertools.product(*test_base): - with tmp(relpath("test_data")): - config = EverestConfig.load_file(config_file) - exp_var_def = _perturb_control_zero(config, gmin, gmax, ginit, fill) - - # Not complete configuration - if None in [gmin, gmax, ginit] and not fill: - with pytest.raises(expected_exception=ValidationError): - EverestConfig.model_validate(config.to_dict()) - continue - - # Invalid parameters - def valid_control(var: ControlVariableConfig) -> bool: - return var.min <= var.initial_guess <= var.max - - if not all(map(valid_control, exp_var_def)): - with pytest.raises(expected_exception=ValidationError): - EverestConfig.model_validate(config.to_dict()) - continue - - EverestConfig.model_validate(config.to_dict()) + config = EverestConfig.load_file(config_file) + exp_var_def = _perturb_control_zero(config, gmin, gmax, ginit, fill) + + # Not complete configuration + if None in [gmin, gmax, ginit] and not fill: + with pytest.raises(expected_exception=ValidationError): + EverestConfig.model_validate(config.to_dict()) + continue + + # Invalid parameters + def valid_control(var: ControlVariableConfig) -> bool: + return var.min <= var.initial_guess <= var.max + + if not all(map(valid_control, exp_var_def)): + with pytest.raises(expected_exception=ValidationError): + EverestConfig.model_validate(config.to_dict()) + continue + + EverestConfig.model_validate(config.to_dict()) def test_control_variable_name(): diff --git a/tests/everest/test_cvar.py b/tests/everest/test_cvar.py index 9bddeab100a..f439fb68521 100644 --- a/tests/everest/test_cvar.py +++ b/tests/everest/test_cvar.py @@ -2,14 +2,11 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_CVAR = "config_cvar.yml" -@tmpdir(CONFIG_PATH) -def test_mathfunc_cvar(): +def test_mathfunc_cvar(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_CVAR) workflow = _EverestWorkflow(config) diff --git a/tests/everest/test_detached.py b/tests/everest/test_detached.py index c8c6605b282..7554aa56e78 100644 --- a/tests/everest/test_detached.py +++ b/tests/everest/test_detached.py @@ -37,7 +37,6 @@ SIMULATION_DIR, ) from everest.util import makedirs_if_needed -from tests.everest.utils import relpath, tmpdir class MockContext: @@ -61,8 +60,7 @@ def job_progress(*args): @pytest.mark.integration_test @pytest.mark.fails_on_macos_github_workflow @pytest.mark.xdist_group(name="starts_everest") -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_https_requests(): +def test_https_requests(copy_math_func_test_data_to_tmp): everest_config = EverestConfig.load_file("config_minimal_slow.yml") expected_server_status = ServerStatus.never_run @@ -121,8 +119,7 @@ def test_https_requests(): assert ServerStatus.stopped == server_status["status"] -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_server_status(): +def test_server_status(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file("config_minimal.yml") # Check status file does not exist before initial status update @@ -157,8 +154,10 @@ def test_server_status(): @patch("everest.detached.server_is_running", return_value=False) -@tmpdir(relpath("test_data", "detached")) -def test_wait_for_server(server_is_running_mock, caplog): +def test_wait_for_server( + server_is_running_mock, caplog, copy_test_data_to_tmp, monkeypatch +): + monkeypatch.chdir("detached") config = EverestConfig.load_file("valid_yaml_config.yml") with caplog.at_level(logging.DEBUG), pytest.raises(Exception): @@ -219,8 +218,7 @@ def _get_reference_config(): return everest_config, reference_config -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_detached_mode_config_base(): +def test_detached_mode_config_base(copy_math_func_test_data_to_tmp): everest_config, reference = _get_reference_config() ert_config = generate_everserver_ert_config(everest_config) @@ -237,8 +235,9 @@ def test_detached_mode_config_base(): ("slurm", 5, "test_slurm"), ], ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_queue_config_equal_to_run_config(queue_system, cores, name): +def test_everserver_queue_config_equal_to_run_config( + copy_math_func_test_data_to_tmp, queue_system, cores, name +): everest_config, _ = _get_reference_config() simulator_config = {CK.QUEUE_SYSTEM: queue_system, CK.CORES: cores} @@ -267,8 +266,7 @@ def test_everserver_queue_config_equal_to_run_config(queue_system, cores, name): ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_detached_mode_config_debug(): +def test_detached_mode_config_debug(copy_math_func_test_data_to_tmp): everest_config, reference = _get_reference_config() ert_config = generate_everserver_ert_config(everest_config, debug_mode=True) @@ -279,8 +277,7 @@ def test_detached_mode_config_debug(): @pytest.mark.parametrize("queue_system", ["lsf", "slurm"]) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_detached_mode_config_only_sim(queue_system): +def test_detached_mode_config_only_sim(copy_math_func_test_data_to_tmp, queue_system): everest_config, reference = _get_reference_config() reference["QUEUE_SYSTEM"] = queue_system.upper() @@ -292,8 +289,7 @@ def test_detached_mode_config_only_sim(queue_system): assert ert_config == reference -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_detached_mode_config_error(): +def test_detached_mode_config_error(copy_math_func_test_data_to_tmp): """ We are not allowing the simulator queue to be local and at the same time the everserver queue to be something other than local @@ -305,8 +301,7 @@ def test_detached_mode_config_error(): generate_everserver_ert_config(everest_config) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_detached_mode_config_queue_name(): +def test_detached_mode_config_queue_name(copy_math_func_test_data_to_tmp): everest_config, reference = _get_reference_config() queue_name = "put_me_in_the_queue" diff --git a/tests/everest/test_discrete.py b/tests/everest/test_discrete.py index 1ecdd251450..ca92aa7d10e 100644 --- a/tests/everest/test_discrete.py +++ b/tests/everest/test_discrete.py @@ -1,13 +1,10 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_DISCRETE = "config_discrete.yml" -@tmpdir(CONFIG_PATH) -def test_discrete_optimizer(): +def test_discrete_optimizer(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_DISCRETE) workflow = _EverestWorkflow(config) diff --git a/tests/everest/test_egg_simulation.py b/tests/everest/test_egg_simulation.py index f810322d9ff..5b913ed0497 100644 --- a/tests/everest/test_egg_simulation.py +++ b/tests/everest/test_egg_simulation.py @@ -1,6 +1,5 @@ import json import os -import shutil import pytest @@ -15,11 +14,9 @@ from tests.everest.utils import ( everest_default_jobs, hide_opm, - relpath, skipif_no_everest_models, skipif_no_opm, skipif_no_simulator, - tmpdir, ) CONFIG_FILE = "everest/model/config.yml" @@ -461,7 +458,6 @@ for group in ["FIELD", "INJECT", "PRODUC"] ] ] -ROOT = os.path.join("..", "..", "test-data", "everest", "egg") def sort_res_summary(ert_config): @@ -565,8 +561,7 @@ def _generate_exp_ert_config(config_path, output_dir): @skipif_no_opm -@tmpdir(relpath(ROOT)) -def test_egg_model_convert(): +def test_egg_model_convert(copy_egg_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) ert_config = everest_to_ert_config(config) @@ -581,10 +576,9 @@ def test_egg_model_convert(): @hide_opm -@tmpdir(relpath(ROOT)) @skipif_no_everest_models @pytest.mark.everest_models_test -def test_egg_model_convert_no_opm(): +def test_egg_model_convert_no_opm(copy_egg_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) ert_config = everest_to_ert_config(config) @@ -601,8 +595,7 @@ def test_egg_model_convert_no_opm(): @skipif_no_everest_models @pytest.mark.everest_models_test -@tmpdir(relpath(ROOT)) -def test_opm_fail_default_summary_keys(): +def test_opm_fail_default_summary_keys(copy_egg_test_data_to_tmp): pytest.importorskip("everest_models") config = EverestConfig.load_file(CONFIG_FILE) @@ -628,8 +621,7 @@ def test_opm_fail_default_summary_keys(): @skipif_no_everest_models @pytest.mark.everest_models_test @skipif_no_opm -@tmpdir(relpath(ROOT)) -def test_opm_fail_explicit_summary_keys(): +def test_opm_fail_explicit_summary_keys(copy_egg_test_data_to_tmp): extra_sum_keys = [ "GOIR:PRODUC", "GOIT:INJECT", @@ -674,8 +666,7 @@ def test_opm_fail_explicit_summary_keys(): @skipif_no_everest_models @pytest.mark.everest_models_test @pytest.mark.integration_test -@tmpdir(relpath(ROOT)) -def test_init_egg_model(): +def test_init_egg_model(copy_egg_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) ert_config = everest_to_ert_config(config, site_config=ErtConfig.read_site_config()) ErtConfig.with_plugins().from_dict(config_dict=ert_config) @@ -685,8 +676,7 @@ def test_init_egg_model(): @pytest.mark.everest_models_test @skipif_no_simulator @pytest.mark.simulation_test -@tmpdir(relpath(ROOT)) -def test_run_egg_model(): +def test_run_egg_model(copy_egg_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) # test callback @@ -791,8 +781,7 @@ def sweetcallbackofmine(self, *args, **kwargs): @skipif_no_everest_models @pytest.mark.everest_models_test @skipif_no_opm -@tmpdir(relpath(ROOT)) -def test_egg_model_wells_json_output_no_none(): +def test_egg_model_wells_json_output_no_none(copy_egg_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) _ = everest_to_ert_config(config) @@ -812,9 +801,9 @@ def test_egg_model_wells_json_output_no_none(): @skipif_no_simulator @pytest.mark.simulation_test @pytest.mark.timeout(0) -def test_egg_snapshot(tmp_path, snapshot, monkeypatch): - shutil.copytree(relpath(ROOT), tmp_path, dirs_exist_ok=True) - monkeypatch.chdir(tmp_path) +def test_egg_snapshot(snapshot, copy_egg_test_data_to_tmp): + # shutil.copytree(relpath(ROOT), tmp_path, dirs_exist_ok=True) + # monkeypatch.chdir(tmp_path) config = EverestConfig.load_file(CONFIG_FILE) class CBTracker(object): diff --git a/tests/everest/test_environment.py b/tests/everest/test_environment.py index 48a250124ef..a47c7f1bb1c 100644 --- a/tests/everest/test_environment.py +++ b/tests/everest/test_environment.py @@ -1,21 +1,16 @@ -import os - import pytest import everest from everest.config import EverestConfig from everest.simulator.everest_to_ert import everest_to_ert_config -from tests.everest.utils import relpath, tmpdir -root = os.path.join("..", "..", "test-data", "everest", "math_func") -config_file = "config_minimal.yml" +CONFIG_FILE = "config_minimal.yml" @pytest.mark.integration_test -@tmpdir(relpath(root)) -def test_seed(): +def test_seed(copy_math_func_test_data_to_tmp): random_seed = 42 - config = EverestConfig.load_file(config_file) + config = EverestConfig.load_file(CONFIG_FILE) config.environment.random_seed = random_seed ever_workflow = everest.suite._EverestWorkflow(config) @@ -28,9 +23,8 @@ def test_seed(): @pytest.mark.integration_test -@tmpdir(relpath(root)) -def test_loglevel(): - config = EverestConfig.load_file(config_file) +def test_loglevel(copy_math_func_test_data_to_tmp): + config = EverestConfig.load_file(CONFIG_FILE) config.environment.log_level = "info" ever_workflow = everest.suite._EverestWorkflow(config) config = ever_workflow.config diff --git a/tests/everest/test_everest_initialization.py b/tests/everest/test_everest_initialization.py index 67918206d39..e6e8e060deb 100644 --- a/tests/everest/test_everest_initialization.py +++ b/tests/everest/test_everest_initialization.py @@ -4,7 +4,6 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmp NO_PROJECT_RES = ( os.environ.get("NO_PROJECT_RES", False), @@ -13,20 +12,16 @@ @pytest.mark.skipif(NO_PROJECT_RES[0], reason=NO_PROJECT_RES[1]) -def test_init_no_project_res(): - root_dir = relpath("..", "..", "test-data", "everest", "egg") +def test_init_no_project_res(copy_egg_test_data_to_tmp): config_file = os.path.join("everest", "model", "config.yml") - with tmp(root_dir): - config_dict = EverestConfig.load_file(config_file) - _EverestWorkflow(config_dict) + config_dict = EverestConfig.load_file(config_file) + _EverestWorkflow(config_dict) -def test_init(): - root_dir = relpath("test_data", "mocked_test_case") +def test_init(copy_mocked_test_data_to_tmp): config_file = os.path.join("mocked_test_case.yml") - with tmp(root_dir): - config_dict = EverestConfig.load_file(config_file) - _EverestWorkflow(config_dict) + config_dict = EverestConfig.load_file(config_file) + _EverestWorkflow(config_dict) def test_no_config_init(): diff --git a/tests/everest/test_everest_output.py b/tests/everest/test_everest_output.py index 9d0b76c7cc9..8fc1f23ee60 100644 --- a/tests/everest/test_everest_output.py +++ b/tests/everest/test_everest_output.py @@ -17,11 +17,11 @@ ) from everest.suite import _EverestWorkflow from everest.util import makedirs_if_needed -from tests.everest.utils import relpath, tmpdir -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_that_one_experiment_creates_one_ensemble_per_batch(): +def test_that_one_experiment_creates_one_ensemble_per_batch( + copy_math_func_test_data_to_tmp, +): config = EverestConfig.load_file("config_minimal.yml") workflow = _EverestWorkflow(config) assert workflow is not None @@ -43,8 +43,7 @@ def test_that_one_experiment_creates_one_ensemble_per_batch(): @pytest.mark.integration_test @patch("ert.simulator.BatchSimulator.start", return_value=None) -@tmpdir(relpath("test_data", "mocked_test_case")) -def test_everest_output(start_mock): +def test_everest_output(start_mock, copy_mocked_test_data_to_tmp): config_folder = os.getcwd() config = EverestConfig.load_file("mocked_test_case.yml") everest_output_dir = config.output_dir @@ -107,8 +106,7 @@ def useless_cb(*args, **kwargs): @patch("ert.simulator.BatchSimulator.start", return_value=None) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_save_running_config(start_mock): +def test_save_running_config(start_mock, copy_math_func_test_data_to_tmp): file_name = "config_minimal.yml" config = EverestConfig.load_file(file_name) ert_config = ErtConfig.with_plugins().from_dict( diff --git a/tests/everest/test_everjobs_io.py b/tests/everest/test_everjobs_io.py index 4207a6fdbed..bd27a9d7c3f 100644 --- a/tests/everest/test_everjobs_io.py +++ b/tests/everest/test_everjobs_io.py @@ -5,11 +5,9 @@ from ruamel.yaml import YAML import everest -from tests.everest.utils import tmpdir -@tmpdir(None) -def test_safe_open(): +def test_safe_open(change_to_tmpdir): filename = "a/relative/path" assert not os.path.exists(filename) @@ -24,8 +22,7 @@ def test_safe_open(): assert "".join(fin.readlines()) == "testing testing" -@tmpdir(None) -def test_load_data(): +def test_load_data(change_to_tmpdir): data = {"secret": "data"} json_file = "data.json" diff --git a/tests/everest/test_everserver.py b/tests/everest/test_everserver.py index 46ee6c192ba..255a09ea172 100644 --- a/tests/everest/test_everserver.py +++ b/tests/everest/test_everserver.py @@ -11,7 +11,6 @@ from everest.detached.jobs import everserver from everest.simulator import JOB_FAILURE, JOB_SUCCESS from everest.strings import OPT_FAILURE_REALIZATIONS, SIM_PROGRESS_ENDPOINT -from tests.everest.utils import relpath, tmpdir def configure_everserver_logger(*args, **kwargs): @@ -51,8 +50,7 @@ def set_shared_status(context_status, event, shared_data, progress): } -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_certificate_generation(): +def test_certificate_generation(copy_math_func_test_data_to_tmp): everest_config = EverestConfig.load_file("config_minimal.yml") cert, key, pw = everserver._generate_certificate(everest_config) @@ -65,8 +63,7 @@ def test_certificate_generation(): ctx.load_cert_chain(cert, key, pw) # raise on error -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_hostfile_storage(): +def test_hostfile_storage(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file("config_minimal.yml") expected_result = { @@ -85,8 +82,7 @@ def test_hostfile_storage(): "everest.detached.jobs.everserver.configure_logger", side_effect=configure_everserver_logger, ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_status_failure(mf_1): +def test_everserver_status_failure(_1, copy_math_func_test_data_to_tmp): config_file = "config_minimal.yml" config = EverestConfig.load_file(config_file) everserver.main() @@ -121,8 +117,9 @@ def test_everserver_status_failure(mf_1): "everest.detached.jobs.everserver.export_to_csv", side_effect=partial(check_status, status=ServerStatus.exporting_to_csv), ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_status_running_complete(*args): +def test_everserver_status_running_complete( + _1, _2, _3, _4, _5, _6, _7, _8, _9, copy_math_func_test_data_to_tmp +): config_file = "config_minimal.yml" config = EverestConfig.load_file(config_file) everserver.main() @@ -165,8 +162,9 @@ def test_everserver_status_running_complete(*args): ], ), ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_status_failed_job(*args): +def test_everserver_status_failed_job( + _1, _2, _3, _4, _5, _6, _7, _8, copy_math_func_test_data_to_tmp +): config_file = "config_minimal.yml" config = EverestConfig.load_file(config_file) everserver.main() @@ -199,8 +197,9 @@ def test_everserver_status_failed_job(*args): "everest.detached.jobs.everserver._sim_monitor", side_effect=partial(set_shared_status, progress=[]), ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_status_exception(*args): +def test_everserver_status_exception( + _1, _2, _3, _4, _5, _6, _7, _8, copy_math_func_test_data_to_tmp +): config_file = "config_minimal.yml" config = EverestConfig.load_file(config_file) everserver.main() @@ -229,8 +228,9 @@ def test_everserver_status_exception(*args): "everest.detached.jobs.everserver._sim_monitor", side_effect=partial(set_shared_status, progress=[]), ) -@tmpdir(relpath("..", "..", "test-data", "everest", "math_func")) -def test_everserver_status_max_batch_num(*args): +def test_everserver_status_max_batch_num( + _1, _2, _3, _4, _5, _6, _7, copy_math_func_test_data_to_tmp +): config_file = "config_one_batch.yml" config = EverestConfig.load_file(config_file) everserver.main() diff --git a/tests/everest/test_export.py b/tests/everest/test_export.py index 3b82e1b1589..3652c668ff2 100644 --- a/tests/everest/test_export.py +++ b/tests/everest/test_export.py @@ -9,12 +9,9 @@ from everest.config import EverestConfig from everest.config.export_config import ExportConfig from everest.export import export, validate_export -from tests.everest.utils import create_cached_mocked_test_case, relpath, tmpdir - -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") +from tests.everest.utils import create_cached_mocked_test_case, relpath CONFIG_FILE_MOCKED_TEST_CASE = "mocked_multi_batch.yml" -CONFIG_PATH_MOCKED_TEST_CASE = relpath("test_data", "mocked_test_case") CASHED_RESULTS_FOLDER = relpath("test_data", "cached_results_config_multiobj") CONFIG_FILE = "config_multiobj.yml" DATA = pd.DataFrame( @@ -68,8 +65,7 @@ def test_filter_double_wildcard(): ) -@tmpdir(CONFIG_PATH) -def test_export_only_non_gradient_with_increased_merit(): +def test_export_only_non_gradient_with_increased_merit(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -91,8 +87,7 @@ def test_export_only_non_gradient_with_increased_merit(): assert merit_flag == 1 -@tmpdir(CONFIG_PATH) -def test_export_only_non_gradient(): +def test_export_only_non_gradient(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -115,8 +110,7 @@ def test_export_only_non_gradient(): assert 1 in df["increased_merit"].values -@tmpdir(CONFIG_PATH) -def test_export_only_increased_merit(): +def test_export_only_increased_merit(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -139,8 +133,7 @@ def test_export_only_increased_merit(): assert 0 not in df["increased_merit"].values -@tmpdir(CONFIG_PATH) -def test_export_all_batches(): +def test_export_all_batches(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -162,8 +155,7 @@ def test_export_all_batches(): assert 0 in df["increased_merit"].values -@tmpdir(CONFIG_PATH) -def test_export_only_give_batches(): +def test_export_only_give_batches(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -180,9 +172,8 @@ def test_export_only_give_batches(): assert id == 2 -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_export_batches_progress(cache_dir): +def test_export_batches_progress(cache_dir, copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_MOCKED_TEST_CASE) shutil.copytree( @@ -200,8 +191,7 @@ def test_export_batches_progress(cache_dir): assert id == 2 -@tmpdir(CONFIG_PATH) -def test_export_nothing_for_empty_batch_list(): +def test_export_nothing_for_empty_batch_list(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -219,8 +209,7 @@ def test_export_nothing_for_empty_batch_list(): assert df.empty -@tmpdir(CONFIG_PATH) -def test_export_nothing(): +def test_export_nothing(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( @@ -238,8 +227,7 @@ def test_export_nothing(): assert df.empty -@tmpdir(CONFIG_PATH) -def test_get_export_path(): +def test_get_export_path(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) # Test default export path when no csv_output_filepath is defined @@ -275,9 +263,8 @@ def test_get_export_path(): assert expected_export_path == new_config.export_path -@tmpdir(CONFIG_PATH_MOCKED_TEST_CASE) @pytest.mark.fails_on_macos_github_workflow -def test_validate_export(cache_dir): +def test_validate_export(cache_dir, copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_MOCKED_TEST_CASE) shutil.copytree( @@ -353,8 +340,7 @@ def check_error(expected_error, reported_errors): assert config.export.batches == [0] -@tmpdir(CONFIG_PATH) -def test_export_gradients(): +def test_export_gradients(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) os.makedirs(config.optimization_output_dir) shutil.copy( diff --git a/tests/everest/test_export_data_dialog.py b/tests/everest/test_export_data_dialog.py index 260ecfbce3d..f1cd44a61c3 100644 --- a/tests/everest/test_export_data_dialog.py +++ b/tests/everest/test_export_data_dialog.py @@ -8,13 +8,11 @@ from everest.config.everest_config import EverestConfig from ieverest import IEverest from ieverest.widgets.export_data_dialog import ExportDataDialog -from tests.everest.utils import tmpdir @pytest.mark.ui_test @pytest.mark.xdist_group(name="starts_everest") -@tmpdir(None) -def test_export_data_dialog(qtbot, monkeypatch): +def test_export_data_dialog(qtbot, monkeypatch, change_to_tmpdir): ieverest = IEverest() config = EverestConfig.with_defaults() diff --git a/tests/everest/test_fix_control.py b/tests/everest/test_fix_control.py index ca3ec5dcfd0..3c04a2698c7 100644 --- a/tests/everest/test_fix_control.py +++ b/tests/everest/test_fix_control.py @@ -1,13 +1,10 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_ADVANCED = "config_advanced_scipy.yml" -@tmpdir(CONFIG_PATH) -def test_fix_control(): +def test_fix_control(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_ADVANCED) config.controls[0].variables[0].enabled = False diff --git a/tests/everest/test_logging.py b/tests/everest/test_logging.py index 7cd4263b0cf..6675bcb6323 100644 --- a/tests/everest/test_logging.py +++ b/tests/everest/test_logging.py @@ -13,9 +13,7 @@ wait_for_server, ) from everest.util import makedirs_if_needed -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE = "config_fm_failure.yml" @@ -29,8 +27,7 @@ def string_exists_in_file(file_path, string): @pytest.mark.integration_test @pytest.mark.xdist_group(name="starts_everest") @pytest.mark.fails_on_macos_github_workflow -@tmpdir(CONFIG_PATH) -def test_logging_setup(): +def test_logging_setup(copy_math_func_test_data_to_tmp): everest_config = EverestConfig.load_file(CONFIG_FILE) wait_for_context() diff --git a/tests/everest/test_math_func.py b/tests/everest/test_math_func.py index 317b60f0f88..f2cd7522247 100644 --- a/tests/everest/test_math_func.py +++ b/tests/everest/test_math_func.py @@ -11,9 +11,6 @@ from everest.export import export from everest.suite import _EverestWorkflow from everest.util import makedirs_if_needed -from tests.everest.utils import relpath, tmpdir - -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_MINIMAL = "config_minimal.yml" CONFIG_FILE_IN_CONSTR = "config_in_constr.yml" @@ -25,8 +22,7 @@ @pytest.mark.redundant_test # superseded by test_math_func_advanced and _multiobj -@tmpdir(CONFIG_PATH) -def test_math_func_minimal(): +def test_math_func_minimal(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_MINIMAL) workflow = _EverestWorkflow(config) assert workflow is not None @@ -62,8 +58,7 @@ def test_math_func_minimal(): @pytest.mark.redundant_test # superseded by test_math_func_advanced and _multiobj -@tmpdir(CONFIG_PATH) -def test_math_func_in_constr(): +def test_math_func_in_constr(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_IN_CONSTR) workflow = _EverestWorkflow(config) assert workflow is not None @@ -94,8 +89,7 @@ def test_math_func_in_constr(): @pytest.mark.redundant_test # superseded by test_math_func_advanced and _multiobj -@tmpdir(CONFIG_PATH) -def test_math_func_out_constr(): +def test_math_func_out_constr(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_OUT_CONSTR) workflow = _EverestWorkflow(config) assert workflow is not None @@ -130,8 +124,7 @@ def test_math_func_out_constr(): @pytest.mark.integration_test -@tmpdir(CONFIG_PATH) -def test_math_func_multiobj(): +def test_math_func_multiobj(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_MULTIOBJ) workflow = _EverestWorkflow(config) @@ -216,8 +209,7 @@ def test_math_func_multiobj(): @pytest.mark.integration_test -@tmpdir(CONFIG_PATH) -def test_math_func_advanced(): +def test_math_func_advanced(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_ADVANCED) workflow = _EverestWorkflow(config) @@ -287,8 +279,7 @@ def test_math_func_advanced(): @pytest.mark.integration_test -@tmpdir(CONFIG_PATH) -def test_remove_run_path(): +def test_remove_run_path(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_REMOVE_RUN_PATH) simulation_should_fail = "simulation_2" @@ -335,8 +326,7 @@ def test_remove_run_path(): ), "Simulation folder should be there, something went wrong and was removed" -@tmpdir(CONFIG_PATH) -def test_math_func_auto_scaled_controls(): +def test_math_func_auto_scaled_controls(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_AUTO_SCALED_CONTROLS) workflow = _EverestWorkflow(config) diff --git a/tests/everest/test_multiobjective.py b/tests/everest/test_multiobjective.py index d743a07c1f9..9f2d8df4901 100644 --- a/tests/everest/test_multiobjective.py +++ b/tests/everest/test_multiobjective.py @@ -7,14 +7,11 @@ from everest.simulator.everest_to_ert import everest_to_ert_config from everest.suite import _EverestWorkflow from tests.everest.test_config_validation import has_error -from tests.everest.utils import relpath, tmpdir -CONFIG_DIR = relpath("test_data", "mocked_test_case") CONFIG_FILE = "config_multi_objectives.yml" -@tmpdir(CONFIG_DIR) -def test_config_multi_objectives(): +def test_config_multi_objectives(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) config_dict = config.to_dict() @@ -71,15 +68,13 @@ def test_config_multi_objectives(): _EverestWorkflow(config) -@tmpdir(CONFIG_DIR) -def test_multi_objectives2res(): +def test_multi_objectives2res(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) res = everest_to_ert_config(config, site_config=ErtConfig.read_site_config()) ErtConfig.with_plugins().from_dict(config_dict=res) -@tmpdir(CONFIG_DIR) -def test_multi_objectives2ropt(): +def test_multi_objectives2ropt(copy_mocked_test_data_to_tmp): # pylint: disable=unbalanced-tuple-unpacking config = EverestConfig.load_file(CONFIG_FILE) config_dict = config.to_dict() @@ -103,8 +98,7 @@ def test_multi_objectives2ropt(): @pytest.mark.integration_test -@tmpdir(CONFIG_DIR) -def test_multi_objectives_run(): +def test_multi_objectives_run(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) workflow = _EverestWorkflow(config) workflow.start_optimization() diff --git a/tests/everest/test_objective_type.py b/tests/everest/test_objective_type.py index 397252cae3d..26f9a3b750a 100644 --- a/tests/everest/test_objective_type.py +++ b/tests/everest/test_objective_type.py @@ -2,14 +2,11 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_STDDEV = "config_stddev.yml" -@tmpdir(CONFIG_PATH) -def test_mathfunc_stddev(): +def test_mathfunc_stddev(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_STDDEV) workflow = _EverestWorkflow(config) diff --git a/tests/everest/test_optimization_config.py b/tests/everest/test_optimization_config.py index d7cb30caf88..7283d553c32 100644 --- a/tests/everest/test_optimization_config.py +++ b/tests/everest/test_optimization_config.py @@ -3,11 +3,9 @@ import pytest from everest.config import EverestConfig -from tests.everest.utils import relpath, tmpdir -@tmpdir(relpath("test_data")) -def test_optimization_config(): +def test_optimization_config(copy_test_data_to_tmp): config_directory = "mocked_test_case" cfg = os.path.join(config_directory, "config_full_gradient_info.yml") full_config_dict = EverestConfig.load_file(cfg) diff --git a/tests/everest/test_output_constraints.py b/tests/everest/test_output_constraints.py index a1239468457..49ef017bd5d 100644 --- a/tests/everest/test_output_constraints.py +++ b/tests/everest/test_output_constraints.py @@ -10,14 +10,11 @@ from everest.suite import _EverestWorkflow from .test_config_validation import has_error -from .utils import relpath, tmpdir -CONFIG_DIR = relpath("test_data", "mocked_test_case") CONFIG_FILE = "config_output_constraints.yml" -@tmpdir(CONFIG_DIR) -def test_constraints_init(): +def test_constraints_init(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) constr = list(config.output_constraints or []) @@ -45,8 +42,7 @@ def test_constraints_init(): assert [cn.scale for cn in constr] == 16 * [7500] -@tmpdir(CONFIG_DIR) -def test_wrong_output_constr_def(): +def test_wrong_output_constr_def(copy_mocked_test_data_to_tmp): # No RHS errors = EverestConfig.lint_config_dict( { @@ -188,8 +184,7 @@ def test_wrong_output_constr_def(): assert has_error(errors, "unable to parse string as a number") -@tmpdir(CONFIG_DIR) -def test_upper_bound_output_constraint_def(): +def test_upper_bound_output_constraint_def(copy_mocked_test_data_to_tmp): with open("conf_file", "w", encoding="utf-8") as f: f.write(" ") @@ -237,8 +232,7 @@ def test_upper_bound_output_constraint_def(): @pytest.mark.integration_test -@tmpdir(CONFIG_DIR) -def test_sim_output_constraints(): +def test_sim_output_constraints(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) workflow = _EverestWorkflow(config) assert workflow is not None diff --git a/tests/everest/test_res_initialization.py b/tests/everest/test_res_initialization.py index a006feec338..92af23134eb 100644 --- a/tests/everest/test_res_initialization.py +++ b/tests/everest/test_res_initialization.py @@ -20,15 +20,12 @@ relpath, skipif_no_everest_models, skipif_no_opm, - tmp, - tmpdir, ) NO_PROJECT_RES = ( os.environ.get("NO_PROJECT_RES", False), "Skipping tests when no access to /project/res", ) - SNAKE_CONFIG_DIR = "snake_oil/everest/model" SNAKE_CONFIG_PATH = os.path.join(SNAKE_CONFIG_DIR, "snake_oil.yml") TUTORIAL_CONFIG_DIR = "mocked_test_case" @@ -213,8 +210,7 @@ def build_tutorial_dict(config_dir, output_dir): } -@tmpdir(relpath("test_data")) -def test_snake_everest_to_ert(): +def test_snake_everest_to_ert(copy_test_data_to_tmp): # Load config file ever_config_dict = EverestConfig.load_file(SNAKE_CONFIG_PATH) @@ -233,8 +229,7 @@ def test_snake_everest_to_ert(): ) -@tmpdir(relpath("test_data")) -def test_snake_everest_to_ert_slurm(): +def test_snake_everest_to_ert_slurm(copy_test_data_to_tmp): snake_slurm_config_path = os.path.join(SNAKE_CONFIG_DIR, "snake_oil_slurm.yml") # Load config file ever_config_dict = EverestConfig.load_file(snake_slurm_config_path) @@ -255,8 +250,7 @@ def test_snake_everest_to_ert_slurm(): @patch.dict("os.environ", {"USER": "NO_USERNAME"}) -@tmpdir(relpath("test_data")) -def test_tutorial_everest_to_ert(): +def test_tutorial_everest_to_ert(copy_test_data_to_tmp): tutorial_config_path = os.path.join(TUTORIAL_CONFIG_DIR, "mocked_test_case.yml") # Load config file ever_config_dict = EverestConfig.load_file(tutorial_config_path) @@ -279,8 +273,7 @@ def test_tutorial_everest_to_ert(): @skipif_no_opm -@tmpdir(relpath("test_data")) -def test_combined_wells_everest_to_ert(): +def test_combined_wells_everest_to_ert(copy_test_data_to_tmp): config_mocked_multi_batch = os.path.join( TUTORIAL_CONFIG_DIR, "mocked_multi_batch.yml" ) @@ -303,8 +296,7 @@ def test_combined_wells_everest_to_ert(): assert any(inj_in_strings) -@tmpdir(relpath("test_data")) -def test_lsf_queue_system(): +def test_lsf_queue_system(copy_test_data_to_tmp): snake_all_path = os.path.join(SNAKE_CONFIG_DIR, "snake_oil_all.yml") ever_config = EverestConfig.load_file(snake_all_path) @@ -316,8 +308,7 @@ def test_lsf_queue_system(): assert queue_system == "LSF" -@tmpdir(relpath("test_data")) -def test_queue_configuration(): +def test_queue_configuration(copy_test_data_to_tmp): snake_all_path = os.path.join(SNAKE_CONFIG_DIR, "snake_oil_all.yml") ever_config = EverestConfig.load_file(snake_all_path) @@ -353,7 +344,7 @@ def test_queue_config(): @patch.dict("os.environ", {"USER": "NO_USERNAME"}) -def test_install_data_no_init(): +def test_install_data_no_init(copy_test_data_to_tmp): """ TODO: When default jobs are handled in Everest this test should be deleted as it is superseded by test_install_data. @@ -365,44 +356,42 @@ def test_install_data_no_init(): test_base = list(zip(sources, targets, links, cmd_list)) tutorial_config_path = os.path.join(TUTORIAL_CONFIG_DIR, "mocked_test_case.yml") for source, target, link, cmd in test_base[1:2]: - with tmp(relpath("test_data")): - ever_config = EverestConfig.load_file(tutorial_config_path) - - if ever_config.install_data is None: - ever_config.install_data = [] - - ever_config.install_data.append( - InstallDataConfig( - source=source, - target=target, - link=link, - ) + ever_config = EverestConfig.load_file(tutorial_config_path) + + if ever_config.install_data is None: + ever_config.install_data = [] + + ever_config.install_data.append( + InstallDataConfig( + source=source, + target=target, + link=link, ) + ) - errors = EverestConfig.lint_config_dict(ever_config.to_dict()) - assert len(errors) == 0 + errors = EverestConfig.lint_config_dict(ever_config.to_dict()) + assert len(errors) == 0 - ert_config_dict = everest_to_ert_config(ever_config) + ert_config_dict = everest_to_ert_config(ever_config) - output_dir = ever_config.output_dir - tutorial_dict = build_tutorial_dict( - os.path.abspath(TUTORIAL_CONFIG_DIR), output_dir - ) + output_dir = ever_config.output_dir + tutorial_dict = build_tutorial_dict( + os.path.abspath(TUTORIAL_CONFIG_DIR), output_dir + ) - config_dir = ever_config.config_directory - tutorial_dict["SIMULATION_JOB"].insert( - 0, - (cmd, os.path.join(config_dir, source), target), - ) - assert tutorial_dict == ert_config_dict + config_dir = ever_config.config_directory + tutorial_dict["SIMULATION_JOB"].insert( + 0, + (cmd, os.path.join(config_dir, source), target), + ) + assert tutorial_dict == ert_config_dict @skipif_no_opm @skipif_no_everest_models @pytest.mark.everest_models_test @pytest.mark.integration_test -@tmpdir(relpath("../../test-data/everest/egg")) -def test_summary_default(): +def test_summary_default(copy_egg_test_data_to_tmp): config_dir = "everest/model" config_file = os.path.join(config_dir, "config.yml") everconf = EverestConfig.load_file(config_file) @@ -435,8 +424,7 @@ def test_summary_default(): @skipif_no_everest_models @pytest.mark.everest_models_test @pytest.mark.fails_on_macos_github_workflow -@tmpdir(relpath("../../test-data/everest/egg")) -def test_summary_default_no_opm(): +def test_summary_default_no_opm(copy_egg_test_data_to_tmp): config_dir = "everest/model" config_file = os.path.join(config_dir, "config.yml") everconf = EverestConfig.load_file(config_file) @@ -460,7 +448,7 @@ def test_summary_default_no_opm(): @pytest.mark.simulation_test -def test_install_data(): +def test_install_data(copy_test_data_to_tmp): """ TODO: When default jobs are handled in Everest this test should not be a simulation test. @@ -473,46 +461,44 @@ def test_install_data(): test_base = zip(sources, targets, links, cmds) tutorial_config_path = os.path.join(TUTORIAL_CONFIG_DIR, "mocked_test_case.yml") for source, target, link, cmd in test_base: - with tmp(relpath("test_data")): - ever_config = EverestConfig.load_file(tutorial_config_path) - - if ever_config.install_data is None: - ever_config.install_data = [] - - ever_config.install_data.append( - InstallDataConfig( - source=source, - target=target, - link=link, - ) + ever_config = EverestConfig.load_file(tutorial_config_path) + + if ever_config.install_data is None: + ever_config.install_data = [] + + ever_config.install_data.append( + InstallDataConfig( + source=source, + target=target, + link=link, ) + ) - errors = EverestConfig.lint_config_dict(ever_config.to_dict()) - assert len(errors) == 0 + errors = EverestConfig.lint_config_dict(ever_config.to_dict()) + assert len(errors) == 0 - ert_config_dict = everest_to_ert_config(ever_config) + ert_config_dict = everest_to_ert_config(ever_config) - output_dir = ever_config.output_dir - tutorial_dict = build_tutorial_dict( - os.path.abspath(TUTORIAL_CONFIG_DIR), output_dir - ) - config_dir = ever_config.config_directory - tutorial_dict["SIMULATION_JOB"].insert( - 0, - (cmd, os.path.join(config_dir, source), target), - ) - assert tutorial_dict == ert_config_dict + output_dir = ever_config.output_dir + tutorial_dict = build_tutorial_dict( + os.path.abspath(TUTORIAL_CONFIG_DIR), output_dir + ) + config_dir = ever_config.config_directory + tutorial_dict["SIMULATION_JOB"].insert( + 0, + (cmd, os.path.join(config_dir, source), target), + ) + assert tutorial_dict == ert_config_dict - # Instantiate res - ErtConfig.with_plugins().from_dict( - config_dict=everest_to_ert_config( - ever_config, site_config=ErtConfig.read_site_config() - ) + # Instantiate res + ErtConfig.with_plugins().from_dict( + config_dict=everest_to_ert_config( + ever_config, site_config=ErtConfig.read_site_config() ) + ) -@tmpdir(relpath("test_data")) -def test_strip_date_job_insertion(): +def test_strip_date_job_insertion(copy_test_data_to_tmp): # Load config file ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) ever_config.model.report_steps = [ @@ -530,8 +516,7 @@ def test_strip_date_job_insertion(): assert snake_dict == ert_config_dict -@tmpdir(relpath("test_data")) -def test_forward_model_job_insertion(): +def test_forward_model_job_insertion(copy_test_data_to_tmp): # Load config file ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) @@ -544,8 +529,7 @@ def test_forward_model_job_insertion(): assert res_job in jobs -@tmpdir(relpath("test_data")) -def test_workflow_job(): +def test_workflow_job(copy_test_data_to_tmp): workflow_jobs = [{"name": "test", "source": "jobs/TEST"}] ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) ever_config.install_workflow_jobs = workflow_jobs @@ -558,8 +542,7 @@ def test_workflow_job(): ) -@tmpdir(relpath("test_data")) -def test_workflows(): +def test_workflows(copy_test_data_to_tmp): workflow_jobs = [{"name": "test", "source": "jobs/TEST"}] ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) ever_config.install_workflow_jobs = workflow_jobs @@ -577,8 +560,7 @@ def test_workflows(): assert hooks[0] == ("pre_simulation", "PRE_SIMULATION") -@tmpdir(relpath("test_data")) -def test_user_config_jobs_precedence(): +def test_user_config_jobs_precedence(copy_test_data_to_tmp): # Load config file ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) first_job = everest.jobs.script_names[0] @@ -594,8 +576,7 @@ def test_user_config_jobs_precedence(): assert job[0][1] == os.path.join(config_dir, "expected_source") -@tmpdir(relpath("test_data")) -def test_user_config_num_cpu(): +def test_user_config_num_cpu(copy_test_data_to_tmp): # Load config file ever_config = EverestConfig.load_file(SNAKE_CONFIG_PATH) diff --git a/tests/everest/test_ropt_initialization.py b/tests/everest/test_ropt_initialization.py index cf76477eaa5..fde52b8c81c 100644 --- a/tests/everest/test_ropt_initialization.py +++ b/tests/everest/test_ropt_initialization.py @@ -9,16 +9,14 @@ from everest.config import EverestConfig from everest.config_file_loader import yaml_file_to_substituted_config_dict from everest.optimizer.everest2ropt import everest2ropt -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath _CONFIG_DIR = relpath("test_data/mocked_test_case") _CONFIG_FILE = "mocked_test_case.yml" -@tmpdir(_CONFIG_DIR) def test_tutorial_everest2ropt(): - ever_config = EverestConfig.load_file(_CONFIG_FILE) - + ever_config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE)) ropt_config = EnOptConfig.model_validate(everest2ropt(ever_config)) realizations = ropt_config.realizations @@ -28,11 +26,8 @@ def test_tutorial_everest2ropt(): assert realizations.weights[0] == 0.5 -@tmpdir(relpath("test_data")) def test_everest2ropt_controls(): - config = EverestConfig.load_file( - os.path.join("mocked_test_case", "mocked_test_case.yml") - ) + config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE)) controls = config.controls assert len(controls) == 1 @@ -43,11 +38,8 @@ def test_everest2ropt_controls(): assert len(ropt_config.variables.upper_bounds) == 16 -@tmpdir(relpath("test_data")) def test_everest2ropt_controls_auto_scale(): - config = EverestConfig.load_file( - os.path.join("mocked_test_case", "mocked_test_case.yml") - ) + config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE)) controls = config.controls controls[0].auto_scale = True controls[0].scaled_range = [0.3, 0.7] @@ -56,11 +48,8 @@ def test_everest2ropt_controls_auto_scale(): assert numpy.allclose(ropt_config.variables.upper_bounds, 0.7) -@tmpdir(relpath("test_data")) def test_everest2ropt_variables_auto_scale(): - config = EverestConfig.load_file( - os.path.join("mocked_test_case", "mocked_test_case.yml") - ) + config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE)) controls = config.controls controls[0].variables[1].auto_scale = True controls[0].variables[1].scaled_range = [0.3, 0.7] @@ -73,10 +62,9 @@ def test_everest2ropt_variables_auto_scale(): assert numpy.allclose(ropt_config.variables.upper_bounds[2:], 0.1) -@tmpdir(relpath("test_data")) def test_everest2ropt_controls_input_constraint(): config = EverestConfig.load_file( - os.path.join("mocked_test_case", "config_input_constraints.yml") + os.path.join(_CONFIG_DIR, "config_input_constraints.yml") ) input_constraints_ever_config = config.input_constraints # Check that there are two input constraints entries in the config @@ -100,10 +88,9 @@ def test_everest2ropt_controls_input_constraint(): assert exp_rhs == ropt_config.linear_constraints.rhs_values.tolist() -@tmpdir(relpath("test_data")) def test_everest2ropt_controls_input_constraint_auto_scale(): config = EverestConfig.load_file( - os.path.join("mocked_test_case", "config_input_constraints.yml") + os.path.join(_CONFIG_DIR, "config_input_constraints.yml") ) input_constraints_ever_config = config.input_constraints # Check that there are two input constraints entries in the config @@ -141,10 +128,8 @@ def test_everest2ropt_controls_input_constraint_auto_scale(): ) -@tmpdir(relpath("test_data")) def test_everest2ropt_controls_optimizer_setting(): - config = os.path.join("mocked_test_case", "config_full_gradient_info.yml") - + config = os.path.join(_CONFIG_DIR, "config_full_gradient_info.yml") config = EverestConfig.load_file(config) ropt_config = EnOptConfig.model_validate(everest2ropt(config)) assert len(ropt_config.realizations.names) == 15 @@ -153,19 +138,15 @@ def test_everest2ropt_controls_optimizer_setting(): assert ropt_config.realizations.names == tuple(range(15)) -@tmpdir(relpath("test_data")) def test_everest2ropt_constraints(): - config = os.path.join("mocked_test_case", "config_output_constraints.yml") + config = os.path.join(_CONFIG_DIR, "config_output_constraints.yml") config = EverestConfig.load_file(config) - ropt_config = EnOptConfig.model_validate(everest2ropt(config)) - assert len(ropt_config.nonlinear_constraints.names) == 16 -@tmpdir(relpath("test_data")) def test_everest2ropt_backend_options(): - config = os.path.join("mocked_test_case", "config_output_constraints.yml") + config = os.path.join(_CONFIG_DIR, "config_output_constraints.yml") config = EverestConfig.load_file(config) config.optimization.options = ["test = 1"] @@ -182,9 +163,8 @@ def test_everest2ropt_backend_options(): assert ropt_config.optimizer.options["test"] == 1 -@tmpdir(relpath("test_data")) def test_everest2ropt_samplers(): - config = os.path.join("mocked_test_case", "config_samplers.yml") + config = os.path.join(_CONFIG_DIR, "config_samplers.yml") config = EverestConfig.load_file(config) ropt_config = EnOptConfig.model_validate(everest2ropt(config)) @@ -203,9 +183,10 @@ def test_everest2ropt_samplers(): assert not ropt_config.samplers[idx].shared -@tmpdir(_CONFIG_DIR) def test_everest2ropt_cvar(): - config_dict = yaml_file_to_substituted_config_dict(_CONFIG_FILE) + config_dict = yaml_file_to_substituted_config_dict( + os.path.join(_CONFIG_DIR, _CONFIG_FILE) + ) config_dict["optimization"]["cvar"] = {} @@ -249,11 +230,8 @@ def test_everest2ropt_cvar(): assert ropt_config.realization_filters[0].options["percentile"] == 0.3 -@tmpdir(relpath("test_data")) def test_everest2ropt_arbitrary_backend_options(): - config = EverestConfig.load_file( - os.path.join("mocked_test_case", "mocked_test_case.yml") - ) + config = EverestConfig.load_file(os.path.join(_CONFIG_DIR, _CONFIG_FILE)) config.optimization.backend_options = {"a": [1]} ropt_config = EnOptConfig.model_validate(everest2ropt(config)) @@ -261,8 +239,7 @@ def test_everest2ropt_arbitrary_backend_options(): assert ropt_config.optimizer.options["a"] == [1] -@tmpdir(relpath("test_data")) -def test_everest2ropt_no_algorithm_name(): +def test_everest2ropt_no_algorithm_name(copy_test_data_to_tmp): config = EverestConfig.load_file( os.path.join("valid_config_file", "valid_yaml_config_no_algorithm.yml") ) diff --git a/tests/everest/test_samplers.py b/tests/everest/test_samplers.py index 62ade9f7a02..70488ad2395 100644 --- a/tests/everest/test_samplers.py +++ b/tests/everest/test_samplers.py @@ -3,14 +3,11 @@ from everest.config import EverestConfig from everest.config.sampler_config import SamplerConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, tmpdir -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE_ADVANCED = "config_advanced_scipy.yml" -@tmpdir(CONFIG_PATH) -def test_sampler_uniform(): +def test_sampler_uniform(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_ADVANCED) config.controls[0].sampler = SamplerConfig(**{"method": "uniform"}) @@ -39,8 +36,7 @@ def test_sampler_uniform(): assert expected_opt == pytest.approx(workflow.result.total_objective, abs=0.001) -@tmpdir(CONFIG_PATH) -def test_sampler_mixed(): +def test_sampler_mixed(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE_ADVANCED) config.controls[0].variables[0].sampler = SamplerConfig(**{"method": "uniform"}) config.controls[0].variables[1].sampler = SamplerConfig(**{"method": "norm"}) diff --git a/tests/everest/test_simulator_cache.py b/tests/everest/test_simulator_cache.py index abd724b9c0c..9cb769195c2 100644 --- a/tests/everest/test_simulator_cache.py +++ b/tests/everest/test_simulator_cache.py @@ -4,13 +4,11 @@ from everest.config import EverestConfig, SimulatorConfig from everest.optimizer.everest2ropt import everest2ropt from everest.simulator import Simulator -from tests.everest.utils import relpath, tmp -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") CONFIG_FILE = "config_advanced_scipy.yml" -def test_simulator_cache(monkeypatch): +def test_simulator_cache(monkeypatch, copy_math_func_test_data_to_tmp): n_evals = 0 original_call = Simulator.__call__ @@ -22,39 +20,38 @@ def new_call(*args): monkeypatch.setattr(Simulator, "__call__", new_call) - with tmp(CONFIG_PATH): - config = EverestConfig.load_file(CONFIG_FILE) - config.simulator = SimulatorConfig(enable_cache=True) - - ropt_config = everest2ropt(config) - simulator = Simulator(config) - - # Run once, populating the cache of the simulator: - variables1 = ( - OptimizationPlanRunner( - enopt_config=ropt_config, - evaluator=simulator, - seed=config.environment.random_seed, - ) - .run() - .variables + config = EverestConfig.load_file(CONFIG_FILE) + config.simulator = SimulatorConfig(enable_cache=True) + + ropt_config = everest2ropt(config) + simulator = Simulator(config) + + # Run once, populating the cache of the simulator: + variables1 = ( + OptimizationPlanRunner( + enopt_config=ropt_config, + evaluator=simulator, + seed=config.environment.random_seed, ) - assert variables1 is not None - assert np.allclose(variables1, [0.1, 0, 0.4], atol=0.02) - assert n_evals > 0 - - # Run again with the same simulator: - n_evals = 0 - variables2 = ( - OptimizationPlanRunner( - enopt_config=ropt_config, - evaluator=simulator, - seed=config.environment.random_seed, - ) - .run() - .variables + .run() + .variables + ) + assert variables1 is not None + assert np.allclose(variables1, [0.1, 0, 0.4], atol=0.02) + assert n_evals > 0 + + # Run again with the same simulator: + n_evals = 0 + variables2 = ( + OptimizationPlanRunner( + enopt_config=ropt_config, + evaluator=simulator, + seed=config.environment.random_seed, ) - assert variables2 is not None - assert n_evals == 0 + .run() + .variables + ) + assert variables2 is not None + assert n_evals == 0 - assert np.array_equal(variables1, variables2) + assert np.array_equal(variables1, variables2) diff --git a/tests/everest/test_templating.py b/tests/everest/test_templating.py index c3ae3bd760c..38bfeba0ec7 100644 --- a/tests/everest/test_templating.py +++ b/tests/everest/test_templating.py @@ -7,19 +7,15 @@ import everest from everest.config import EverestConfig -from tests.everest.utils import relpath, tmpdir -TMPL_TEST_PATH = os.path.join("test_data", "templating") TMPL_CONFIG_FILE = "config.yml" TMPL_WELL_DRILL_FILE = os.path.join("templates", "well_drill_info.tmpl") TMPL_DUAL_INPUT_FILE = os.path.join("templates", "dual_input.tmpl") -MATH_TEST_PATH = relpath("..", "..", "test-data", "everest", "math_func") MATH_CONFIG_FILE = "config_minimal.yml" -@tmpdir(relpath(TMPL_TEST_PATH)) -def test_render_invalid(): +def test_render_invalid(copy_template_test_data_to_tmp): render = everest.jobs.templating.render prod_wells = {"PROD%d" % idx: 0.3 * idx for idx in range(4)} @@ -45,8 +41,7 @@ def test_render_invalid(): render(prod_in, TMPL_WELL_DRILL_FILE, None) -@tmpdir(relpath(TMPL_TEST_PATH)) -def test_render(): +def test_render(copy_template_test_data_to_tmp): render = everest.jobs.templating.render wells = {"PROD%d" % idx: 0.2 * idx for idx in range(1, 5)} @@ -76,8 +71,7 @@ def test_render(): assert expected_string == line -@tmpdir(relpath(TMPL_TEST_PATH)) -def test_render_multiple_input(): +def test_render_multiple_input(copy_template_test_data_to_tmp): render = everest.jobs.templating.render wells_north = {"PROD%d" % idx: 0.2 * idx for idx in range(1, 5)} @@ -99,8 +93,7 @@ def test_render_multiple_input(): assert output == ["0.2 vs 0.8"] -@tmpdir(relpath(TMPL_TEST_PATH)) -def test_render_executable(): +def test_render_executable(copy_template_test_data_to_tmp): assert os.access(everest.jobs.render, os.X_OK) # Dump input @@ -132,15 +125,13 @@ def test_render_executable(): @pytest.mark.integration_test -@tmpdir(relpath(TMPL_TEST_PATH)) -def test_install_template(): +def test_install_template(copy_template_test_data_to_tmp): config = EverestConfig.load_file(TMPL_CONFIG_FILE) workflow = everest.suite._EverestWorkflow(config) workflow.start_optimization() -@tmpdir(None) -def test_well_order_template(): +def test_well_order_template(change_to_tmpdir): order_tmpl = everest.templates.fetch_template("well_order.tmpl") well_order = { @@ -173,8 +164,7 @@ def test_well_order_template(): @pytest.mark.integration_test -@tmpdir(relpath(MATH_TEST_PATH)) -def test_user_specified_data_n_template(): +def test_user_specified_data_n_template(copy_math_func_test_data_to_tmp): """ Ensure that a user specifying a data resource and an installed_template with "extra_data", the results of that template will be passed to the diff --git a/tests/everest/test_ui_run.py b/tests/everest/test_ui_run.py index 555718b4d70..4d19b3c1956 100644 --- a/tests/everest/test_ui_run.py +++ b/tests/everest/test_ui_run.py @@ -3,15 +3,14 @@ from ieverest import IEverest from tests.everest.dialogs_mocker import mock_dialogs_all -from tests.everest.utils import relpath, tmpdir +from tests.everest.utils import relpath CASE_DIR = relpath("test_data", "mocked_test_case") CONFIG_FILE = "mocked_test_case.yml" @pytest.mark.ui_test -@tmpdir(CASE_DIR) -def test_load_run(qapp, qtbot, mocker): +def test_load_run(qapp, qtbot, mocker, copy_mocked_test_data_to_tmp): """Load a configuration and run it from the UI""" qapp.setAttribute(Qt.AA_X11InitThreads) diff --git a/tests/everest/test_util.py b/tests/everest/test_util.py index b397c38588e..72937e88697 100644 --- a/tests/everest/test_util.py +++ b/tests/everest/test_util.py @@ -16,7 +16,6 @@ hide_opm, relpath, skipif_no_opm, - tmpdir, ) EGG_DATA = relpath( @@ -25,8 +24,6 @@ ) SPE1_DATA = relpath("test_data/eclipse/SPE1.DATA") -CONFIG_PATH = relpath("..", "..", "test-data", "everest", "math_func") - @skipif_no_opm def test_loadwells(): @@ -52,8 +49,7 @@ def test_loadgroups_no_opm(): util.read_groupnames(EGG_DATA) -@tmpdir(None) -def test_get_values(): +def test_get_values(change_to_tmpdir): exp_dir = "the_config_directory" exp_file = "the_config_file" rel_out_dir = "the_output_directory" @@ -75,8 +71,7 @@ def test_get_values(): config.environment.output_folder = rel_out_dir -@tmpdir(None) -def test_makedirs(): +def test_makedirs(change_to_tmpdir): output_dir = os.path.join("unittest_everest_output") cwd = os.getcwd() @@ -92,8 +87,7 @@ def test_makedirs(): assert len(os.listdir(cwd)) == 1 -@tmpdir(None) -def test_makedirs_already_exists(): +def test_makedirs_already_exists(change_to_tmpdir): output_dir = os.path.join("unittest_everest_output") cwd = os.getcwd() @@ -108,8 +102,7 @@ def test_makedirs_already_exists(): assert len(os.listdir(cwd)) == 1 -@tmpdir(None) -def test_makedirs_roll_existing(): +def test_makedirs_roll_existing(change_to_tmpdir): output_dir = os.path.join("unittest_everest_output") cwd = os.getcwd() @@ -129,8 +122,7 @@ def test_makedirs_roll_existing(): assert len(os.listdir(cwd)) == 3 -@tmpdir(CONFIG_PATH) -def test_get_everserver_status_path(): +def test_get_everserver_status_path(copy_math_func_test_data_to_tmp): config = EverestConfig.load_file("config_minimal.yml") cwd = os.getcwd() session_path = os.path.join( @@ -153,8 +145,7 @@ def test_get_system_installed_job_names(): "everest.bin.utils.everserver_status", return_value={"status": ServerStatus.failed, "message": "mock error"}, ) -@tmpdir(None) -def test_report_on_previous_run(_): +def test_report_on_previous_run(_, change_to_tmpdir): with open("config_file", "w", encoding="utf-8") as f: f.write(" ") config = EverestConfig.with_defaults(**{ConfigKeys.CONFIGPATH: "config_file"}) diff --git a/tests/everest/test_well_tools.py b/tests/everest/test_well_tools.py index ae1be8d0872..0f5e43ead26 100644 --- a/tests/everest/test_well_tools.py +++ b/tests/everest/test_well_tools.py @@ -6,7 +6,6 @@ from ruamel.yaml import YAML import everest -from tests.everest.utils import tmpdir def _dump_sort_data(well_data_file, well_order_file): @@ -41,8 +40,7 @@ def _dump_sort_data(well_data_file, well_order_file): ] -@tmpdir(None) -def test_well_reorder(): +def test_well_reorder(change_to_tmpdir): well_data_file = "well_data.json" well_order_file = "well_order.yml" output_file = "ordered_well_data.json" @@ -63,8 +61,7 @@ def test_well_reorder(): @pytest.mark.integration_test -@tmpdir(None) -def test_well_reorder_script(): +def test_well_reorder_script(change_to_tmpdir): assert os.access(everest.jobs.wdreorder, os.X_OK) well_data_file = "well_data.json" @@ -120,8 +117,7 @@ def _dump_filter_data(well_data_file, well_filter_file): ] -@tmpdir(None) -def test_well_filter(): +def test_well_filter(change_to_tmpdir): well_data_file = "well_data.yml" well_filter_file = "well_filter.json" output_file = "ordered_well_data.json" @@ -142,8 +138,7 @@ def test_well_filter(): @pytest.mark.integration_test -@tmpdir(None) -def test_well_filter_script(): +def test_well_filter_script(change_to_tmpdir): assert os.access(everest.jobs.wdfilter, os.X_OK) well_data_file = "well_data.json" @@ -214,8 +209,7 @@ def _dump_merge_data(well_data_file, additional_data_files): @pytest.mark.integration_test -@tmpdir(None) -def test_well_update(): +def test_well_update(change_to_tmpdir): assert os.access(everest.jobs.wdupdate, os.X_OK) well_data_file = "well_data.json" @@ -251,8 +245,7 @@ def test_well_update(): assert merge_well_data == load_data -@tmpdir(None) -def test_well_set_invalid_data_length(): +def test_well_set_invalid_data_length(change_to_tmpdir): name = everest.ConfigKeys.NAME well_data_file = "well_data.yml" @@ -278,8 +271,7 @@ def test_well_set_invalid_data_length(): ) -@tmpdir(None) -def test_well_set_too_many_entries(): +def test_well_set_too_many_entries(change_to_tmpdir): name = everest.ConfigKeys.NAME well_data_file = "well_data.yml" @@ -305,8 +297,7 @@ def test_well_set_too_many_entries(): ) -@tmpdir(None) -def test_well_set_new_entry(): +def test_well_set_new_entry(change_to_tmpdir): name = everest.ConfigKeys.NAME well_data_file = "well_data.yml" @@ -341,8 +332,7 @@ def test_well_set_new_entry(): assert set_well_data == json.load(f) -@tmpdir(None) -def test_well_set_entry(): +def test_well_set_entry(change_to_tmpdir): name = everest.ConfigKeys.NAME well_data_file = "well_data.yml" @@ -378,8 +368,7 @@ def test_well_set_entry(): @pytest.mark.integration_test -@tmpdir(None) -def test_well_set_script(): +def test_well_set_script(change_to_tmpdir): assert os.access(everest.jobs.wdset, os.X_OK) name = everest.ConfigKeys.NAME @@ -500,8 +489,7 @@ def _dump_completion_data(well_data_file, start_date): ] -@tmpdir(None) -def test_add_completion_date(): +def test_add_completion_date(change_to_tmpdir): well_data_file = "well_data.yml" output_file = "humble_wells.json" start_date = "2001-01-02" @@ -522,8 +510,7 @@ def test_add_completion_date(): @pytest.mark.integration_test -@tmpdir(None) -def test_completion_date_script(): +def test_completion_date_script(change_to_tmpdir): assert os.access(everest.jobs.wdcompl, os.X_OK) well_data_file = "well_data.yml" @@ -640,8 +627,7 @@ def _dump_compl_filter_data(well_data_file): return filtered_well_data, "2001-01-30", "2003-03-07" -@tmpdir(None) -def test_filter_completion_date(): +def test_filter_completion_date(change_to_tmpdir): well_data_file = "well_data.yml" output_file = "filtered_well_data.json" @@ -660,8 +646,7 @@ def test_filter_completion_date(): @pytest.mark.integration_test -@tmpdir(None) -def test_filter_completion_date_script(): +def test_filter_completion_date_script(change_to_tmpdir): assert os.access(everest.jobs.wddatefilter, os.X_OK) well_data_file = "well_data.yml" diff --git a/tests/everest/test_workflows.py b/tests/everest/test_workflows.py index 4869a1c4796..7b2f6641c57 100644 --- a/tests/everest/test_workflows.py +++ b/tests/everest/test_workflows.py @@ -5,15 +5,14 @@ from everest.config import EverestConfig from everest.suite import _EverestWorkflow -from tests.everest.utils import relpath, skipif_no_everest_models, tmpdir +from tests.everest.utils import relpath, skipif_no_everest_models CONFIG_DIR = relpath("test_data", "mocked_test_case") CONFIG_FILE = "config_workflow.yml" @pytest.mark.integration_test -@tmpdir(CONFIG_DIR) -def test_workflow_run(): +def test_workflow_run(copy_mocked_test_data_to_tmp): config = EverestConfig.load_file(CONFIG_FILE) workflow = _EverestWorkflow(config) diff --git a/tests/everest/test_yaml_parser.py b/tests/everest/test_yaml_parser.py index dfe883dfc68..43ad9e23bf4 100644 --- a/tests/everest/test_yaml_parser.py +++ b/tests/everest/test_yaml_parser.py @@ -8,14 +8,14 @@ from everest import ConfigKeys from everest.config import EverestConfig from everest.simulator.everest_to_ert import everest_to_ert_config -from tests.everest.utils import MockParser, relpath, skipif_no_everest_models, tmpdir +from tests.everest.utils import MockParser, relpath, skipif_no_everest_models -snake_oil_folder = relpath("test_data", "snake_oil") +# snake_oil_folder = relpath("test_data", "snake_oil") @pytest.mark.integration_test -@tmpdir(snake_oil_folder) -def test_default_seed(): +def test_default_seed(copy_test_data_to_tmp, monkeypatch): + monkeypatch.chdir("snake_oil") config_file = os.path.join("everest/model", "snake_oil_all.yml") config = EverestConfig.load_file(config_file) assert config.environment.random_seed is None @@ -55,8 +55,8 @@ def test_read_file(): assert exp_fn == everest_config.config_file -@tmpdir(relpath("test_data", "valid_config_file")) -def test_valid_config_file(): +def test_valid_config_file(copy_test_data_to_tmp, monkeypatch): + monkeypatch.chdir("valid_config_file") # pylint: disable=unsupported-membership-test parser = MockParser() @@ -96,11 +96,11 @@ def test_valid_config_file(): assert "could not find expected ':'" in parser.get_error() -@tmpdir(relpath("test_data", "valid_config_file", "forward_models")) @pytest.mark.fails_on_macos_github_workflow @skipif_no_everest_models @pytest.mark.everest_models_test -def test_valid_forward_model_config_files(): +def test_valid_forward_model_config_files(copy_test_data_to_tmp, monkeypatch): + monkeypatch.chdir("valid_config_file/forward_models") parser = MockParser() EverestConfig.load_file_with_argparser( "valid_config_maintained_forward_models.yml", parser=parser @@ -109,11 +109,11 @@ def test_valid_forward_model_config_files(): assert parser.get_error() is None -@tmpdir(relpath("test_data", "valid_config_file", "forward_models")) @skipif_no_everest_models @pytest.mark.everest_models_test @pytest.mark.fails_on_macos_github_workflow -def test_invalid_forward_model_config_files(): +def test_invalid_forward_model_config_files(copy_test_data_to_tmp, monkeypatch): + monkeypatch.chdir("valid_config_file/forward_models") parser = MockParser() next((Path.cwd() / "input" / "templates").glob("*")).unlink() EverestConfig.load_file_with_argparser( diff --git a/tests/everest/unit/everest/bin/test_everload.py b/tests/everest/unit/everest/bin/test_everload.py index 02b4137ff6d..38c9b5da349 100644 --- a/tests/everest/unit/everest/bin/test_everload.py +++ b/tests/everest/unit/everest/bin/test_everload.py @@ -7,8 +7,6 @@ from tests.everest.utils import ( capture_streams, create_cached_mocked_test_case, - relpath, - tmpdir, ) from ert.config import ErtConfig @@ -18,7 +16,6 @@ from everest.config import EverestConfig from everest.strings import STORAGE_DIR -CONFIG_PATH = relpath("test_data", "mocked_test_case") CONFIG_FILE = "mocked_multi_batch.yml" pytestmark = pytest.mark.xdist_group(name="starts_everest") @@ -62,9 +59,10 @@ def assertBackup(config: EverestConfig): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_run(mocked_internalize, cache_dir): +def test_everload_entry_run( + mocked_internalize, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everload on an optimization case""" config = get_config(cache_dir) everload_entry([CONFIG_FILE, "-s"]) @@ -76,9 +74,8 @@ def test_everload_entry_run(mocked_internalize, cache_dir): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_run_empty_batch_list(_): +def test_everload_entry_run_empty_batch_list(_, copy_mocked_test_data_to_tmp): """Test running everload on an optimization case""" with pytest.raises(SystemExit), capture_streams() as (_, err): everload_entry([CONFIG_FILE, "-s", "-b"]) @@ -89,9 +86,10 @@ def test_everload_entry_run_empty_batch_list(_): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_missing_folders(mocked_internalize, cache_dir): +def test_everload_entry_missing_folders( + mocked_internalize, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everload when output folders are missing""" config = get_config(cache_dir) shutil.rmtree(config.simulation_dir) @@ -104,9 +102,10 @@ def test_everload_entry_missing_folders(mocked_internalize, cache_dir): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_batches(mocked_internalize, cache_dir): +def test_everload_entry_batches( + mocked_internalize, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everload with a selection of batches""" config = get_config(cache_dir) # pick every second batch (assume there are at least 2) @@ -122,9 +121,10 @@ def test_everload_entry_batches(mocked_internalize, cache_dir): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_invalid_batches(mocked_internalize): +def test_everload_entry_invalid_batches( + mocked_internalize, copy_mocked_test_data_to_tmp +): """Test running everload with no or wrong batches""" with pytest.raises(SystemExit), capture_streams() as (_, err): everload_entry([CONFIG_FILE, "-s", "-b", "-2", "5412"]) @@ -138,9 +138,10 @@ def test_everload_entry_invalid_batches(mocked_internalize): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_overwrite(mocked_internalize, cache_dir): +def test_everload_entry_overwrite( + mocked_internalize, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everload with the --overwrite flag""" config = get_config(cache_dir) everload_entry([CONFIG_FILE, "-s", "--overwrite"]) @@ -156,9 +157,10 @@ def test_everload_entry_overwrite(mocked_internalize, cache_dir): @patch("everest.bin.everload_script._internalize_batch") -@tmpdir(CONFIG_PATH) @pytest.mark.fails_on_macos_github_workflow -def test_everload_entry_not_silent(mocked_internalize, cache_dir): +def test_everload_entry_not_silent( + mocked_internalize, cache_dir, copy_mocked_test_data_to_tmp +): """Test running everload without the -s flag""" config = get_config(cache_dir) diff --git a/tests/everest/utils/__init__.py b/tests/everest/utils/__init__.py index d5b62a23b17..3e0d9dfcdb7 100644 --- a/tests/everest/utils/__init__.py +++ b/tests/everest/utils/__init__.py @@ -1,11 +1,9 @@ import contextlib import importlib.util -import logging import os import pathlib import shutil import sys -import tempfile from io import StringIO from unittest import mock @@ -59,60 +57,6 @@ def relpath(*path): return os.path.join(os.path.dirname(os.path.dirname(__file__)), *path) -def tmpdir(path, teardown=True): - """Decorator based on the `tmp` context""" - - def real_decorator(function): - def wrapper(function, *args, **kwargs): - with tmp(path, teardown=teardown): - return function(*args, **kwargs) - - return decorator.decorator(wrapper, function) - - return real_decorator - - -@contextlib.contextmanager -def tmp(path=None, teardown=True): - """Create and go into tmp directory, returns the path. - - This function creates a temporary directory and enters that directory. The - returned object is the path to the created directory. - - If @path is not specified, we create an empty directory, otherwise, it must - be a path to an existing directory. In that case, the directory will be - copied into the temporary directory. - - If @teardown is True (defaults to True), the directory is (attempted) - deleted after context, otherwise it is kept as is. - - """ - cwd = os.getcwd() - fname = tempfile.NamedTemporaryFile().name # noqa: SIM115 - - if path: - if not os.path.isdir(path): - logging.debug("tmp:raise no such path") - raise IOError("No such directory: %s" % path) - shutil.copytree(path, fname) - else: - # no path to copy, create empty dir - os.mkdir(fname) - - os.chdir(fname) - - yield fname # give control to caller scope - - os.chdir(cwd) - - if teardown: - try: - shutil.rmtree(fname) - except OSError as oserr: - logging.debug("tmp:rmtree failed %s (%s)" % (fname, oserr)) - shutil.rmtree(fname, ignore_errors=True) - - @contextlib.contextmanager def capture_streams(): """Context that allows capturing text sent to stdout and stderr