Skip to content

Commit

Permalink
fix: Mitigate Windows errors from missing NamedTempFile kwargs on < 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
adigitoleo committed Apr 17, 2024
1 parent b62fdad commit 14efb05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""> Configuration and fixtures for PyDRex tests."""

import sys

import matplotlib
import pytest
from _pytest.logging import LoggingPlugin, _LiveLoggingStreamHandler
Expand Down Expand Up @@ -142,6 +144,14 @@ def ncpus(request):
return max(1, request.config.getoption("--ncpus"))


@pytest.fixture(scope="session")
def named_tempfile_kwargs(request):
if sys.platform == "win32":
return {"delete": False}
else:
return dict()


@pytest.fixture(scope="function")
def console_handler(request):
if request.config.option.verbose > 0: # Show console logs if -v/--verbose given.
Expand Down
10 changes: 5 additions & 5 deletions tests/test_scsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_read_specfile():


@pytest.mark.skipif(sys.platform == "win32", reason="Items are not equal")
def test_save_specfile(outdir):
def test_save_specfile(outdir, named_tempfile_kwargs):
"""Test SCSV spec file reproduction."""
schema = {
"delimiter": ",",
Expand Down Expand Up @@ -159,8 +159,8 @@ def test_save_specfile(outdir):
_io.save_scsv(f"{outdir}/spec_out_alt.scsv", schema_alt, data_alt)

# https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
temp = tempfile.NamedTemporaryFile(delete_on_close=False)
temp_alt = tempfile.NamedTemporaryFile(delete_on_close=False)
temp = tempfile.NamedTemporaryFile(**named_tempfile_kwargs)
temp_alt = tempfile.NamedTemporaryFile(**named_tempfile_kwargs)
_io.save_scsv(temp.name, schema, data)
_io.save_scsv(temp_alt.name, schema_alt, data_alt)
raw_read = []
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_read_Kaminski2002():
# fmt: on


def test_save_scsv_errors():
def test_save_scsv_errors(named_tempfile_kwargs):
"""Check that we raise errors when attempting to write bad SCSV data."""
schema = {
"delimiter": ",",
Expand All @@ -208,7 +208,7 @@ def test_save_scsv_errors():
],
}
# https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
temp = tempfile.NamedTemporaryFile(delete_on_close=False)
temp = tempfile.NamedTemporaryFile(**named_tempfile_kwargs)
with pytest.raises(_err.SCSVError):
foo = [1, 5, 0.2]
_io.save_scsv(temp.name, schema, [foo])
Expand Down

0 comments on commit 14efb05

Please sign in to comment.