From d11b90479a68a7b015fb76f2f3d5b607859c95c8 Mon Sep 17 00:00:00 2001 From: Sharon Fitzpatrick Date: Tue, 11 Jun 2024 12:09:31 -0700 Subject: [PATCH] fix broken tests use pathlib to create temp coastseg directory along cwd --- tests/conftest.py | 13 ++++++++----- tests/test_coastseg_map.py | 4 ++-- tests/test_imports.py | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 6f2832e5..f3348b20 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,10 +19,13 @@ # create a custom context manager to create a temporary directory & clean it up after use class NamedTemporaryDirectory: - def __init__(self, name): + def __init__(self, name,base_path=None): + if base_path is None: + base_path = tempfile.gettempdir() self.name = name - self.path = os.path.join(tempfile.gettempdir(), name) + self.path = os.path.join(base_path, name) os.makedirs(self.path, exist_ok=True) + print(f"Created temporary directory: {self.path}") def __enter__(self): return self.path @@ -38,9 +41,9 @@ def __exit__(self, exc_type, exc_val, exc_tb): @pytest.fixture def named_temp_dir(request): # Retrieve the parameter from the request (this would be the name of the temporary directory) - dir_name = request.param - # Setup phase: create the temporary directory with the provided name - with NamedTemporaryDirectory(dir_name) as temp_dir: + dir_name, base_path = request.param + # Setup phase: create the temporary directory with the provided name and base path + with NamedTemporaryDirectory(dir_name, base_path) as temp_dir: yield temp_dir # Provide the directory to the test function # Teardown phase: cleanup is handled by the NamedTemporaryDirectory context manager diff --git a/tests/test_coastseg_map.py b/tests/test_coastseg_map.py index 5a5e3de4..c1b27e12 100644 --- a/tests/test_coastseg_map.py +++ b/tests/test_coastseg_map.py @@ -13,7 +13,7 @@ import geopandas as gpd from ipyleaflet import GeoJSON import platform - +import pathlib def test_imports(): """Test that all the internal coastseg packages are imported correctly""" @@ -166,7 +166,7 @@ def test_save_config(coastseg_map_with_selected_roi_layer, tmp_path): expected_config_geojson_path = tmp_path / "config_gdf.geojson" assert expected_config_geojson_path.exists() -@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True) +@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True) def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, named_temp_dir): """test_save_config_empty_roi_settings tests if save configs will save both a config.json and config_gdf.geojson to the filepath directory when coastseg_map's rois do not have roi_settings. diff --git a/tests/test_imports.py b/tests/test_imports.py index 82adb4c6..d7c19b0a 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -2,7 +2,7 @@ import platform from coastseg.file_utilities import load_package_resource import pytest - +import pathlib def test_import_load_package_resource(): try: @@ -24,7 +24,7 @@ def test_import_bounding_boxes(): except ImportError: assert False, "Failed to import bounding_boxes" -@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True) +@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True) def test_import_coastseg_logs(named_temp_dir): try: # The named_temp_dir fixture created a temporary directory named 'CoastSeg' @@ -55,7 +55,7 @@ def test_import_downloads(): # Use the `pytest.mark.parametrize` to pass the parameter to the fixture -@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True) +@pytest.mark.parametrize('named_temp_dir', [('CoastSeg',str(pathlib.Path(__file__).parent))], indirect=True) def test_import_download_tide_model(named_temp_dir): try: # The named_temp_dir fixture created a temporary directory named 'CoastSeg'