Skip to content

Commit

Permalink
fix broken tests use pathlib to create temp coastseg directory along cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Jun 11, 2024
1 parent 34a9f82 commit d11b904
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_coastseg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit d11b904

Please sign in to comment.