-
-
Notifications
You must be signed in to change notification settings - Fork 363
Use context manager for temporary directories in store tests #3110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Darn, this helped locally but apparently there are other problems |
I'm surprised pytest's built-in temporary directory management is insufficient for our needs here. can we not make the |
Perhaps we're running into pytest-dev/pytest-cov#693? Pinning |
I'm giving the pytest pin a go over at #3113 - this PR is definitely a good idea regardless 👍 |
@pytest.fixture( | ||
params=["none", "temp_dir_str", "temp_dir_path", "store_path", "memory_store", "dict"] | ||
) | ||
def store_like(request): | ||
if request.param == "none": | ||
yield None | ||
elif request.param == "temp_dir_str": | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
yield temp_dir | ||
elif request.param == "temp_dir_path": | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
yield Path(temp_dir) | ||
elif request.param == "store_path": | ||
yield StorePath(store=MemoryStore(store_dict={}), path="/") | ||
elif request.param == "memory_store": | ||
yield MemoryStore(store_dict={}) | ||
elif request.param == "dict": | ||
yield {} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify like so:
@pytest.fixture( | |
params=["none", "temp_dir_str", "temp_dir_path", "store_path", "memory_store", "dict"] | |
) | |
def store_like(request): | |
if request.param == "none": | |
yield None | |
elif request.param == "temp_dir_str": | |
with tempfile.TemporaryDirectory() as temp_dir: | |
yield temp_dir | |
elif request.param == "temp_dir_path": | |
with tempfile.TemporaryDirectory() as temp_dir: | |
yield Path(temp_dir) | |
elif request.param == "store_path": | |
yield StorePath(store=MemoryStore(store_dict={}), path="/") | |
elif request.param == "memory_store": | |
yield MemoryStore(store_dict={}) | |
elif request.param == "dict": | |
yield {} | |
@pytest.fixture( | |
params=[ | |
None, | |
str, | |
Path, | |
StorePath(store=MemoryStore(store_dict={}), path="/"), | |
MemoryStore(store_dict={}), | |
{}, | |
], | |
) | |
def store_like(request, tmp_path: Path): | |
return param if not callable(param := request.param) else param(tmp_path) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, this is more concise at the expense of being less obviously interpretable. I'm fine with either version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want something closer to your original, but slightly simpler:
@pytest.fixture(
params=[None, "temp_dir_str", "temp_dir_path", "store_path", "memory_store", "dict"]
)
def store_like(request, tmp_path: Path):
if request.param is None:
return None
if request.param == "temp_dir_str":
return str(tmp_path)
if request.param == "temp_dir_path":
return tmp_path
if request.param == "store_path":
return StorePath(store=MemoryStore(store_dict={}), path="/")
if request.param == "memory_store":
return MemoryStore(store_dict={})
if request.param == "dict":
return {}
Thanks - when we do switch back to pytest 8.4, hopefully this will be a few less errors to fix! |
This PR creates a pytest fixture so that the temporary directory used in the store tests get cleaned up after use. The motivation is that the current usage of temporary directories was causing the failures seen in https://github.com/zarr-developers/zarr-python/actions/runs/15405315759/job/43346759238?pr=2774.