Skip to content

Commit

Permalink
Merge pull request #455 from stfc/454_dont_create_files_in_tmpdir_but…
Browse files Browse the repository at this point in the history
…_in_tmp_pytest

#454 Create file in pytest's tmpdir, not in /tmp.
  • Loading branch information
arporter authored Nov 28, 2024
2 parents 44dd1f8 + 156747f commit 3bc75fe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Modifications by (in alphabetical order):
* P. Vitt, University of Siegen, Germany
* A. Voysey, UK Met Office

28/11/2024 PR #455 for #454. Fixes a few tests to use the tmpdir fixture
rather than writing directly to /tmp.

## Release 0.2.0 (26/11/2024) ##

25/11/2024 PR #453 extension of base node types to allow the parse tree to be
deepcopied and pickled.

Expand Down
22 changes: 9 additions & 13 deletions src/fparser/common/tests/test_readfortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

import io
import os.path
import tempfile
import pytest

from fparser.common.readfortran import (
Expand Down Expand Up @@ -787,12 +786,11 @@ def test_multi_put_item(ignore_comments):
##############################################################################


def test_filename_reader():
def test_filename_reader(tmpdir):
"""
Tests that a Fortran source file can be read given its filename.
"""
handle, filename = tempfile.mkstemp(suffix=".f90", text=True)
os.close(handle)
filename = f"{tmpdir}/out.f90"
try:
with io.open(filename, mode="w", encoding="UTF-8") as source_file:
source_file.write(FULL_FREE_SOURCE)
Expand All @@ -811,12 +809,11 @@ def test_filename_reader():
##############################################################################


def test_file_reader():
def test_file_reader(tmpdir):
"""
Tests that a Fortran source file can be read given a file object of it.
"""
handle, filename = tempfile.mkstemp(suffix=".f90", text=True)
os.close(handle)
filename = f"{tmpdir}/out.f90"
try:
with io.open(filename, mode="w", encoding="UTF-8") as source_file:
source_file.write(FULL_FREE_SOURCE)
Expand All @@ -833,12 +830,11 @@ def test_file_reader():
raise


def test_none_in_fifo(log):
def test_none_in_fifo(tmpdir, log):
"""Check that a None entry in the reader FIFO buffer is handled
correctly."""
log.reset()
handle, filename = tempfile.mkstemp(suffix=".f90", text=True)
os.close(handle)
filename = f"{tmpdir}/out.f90"

with io.open(filename, mode="w", encoding="UTF-8") as source_file:
source_file.write(FULL_FREE_SOURCE)
Expand Down Expand Up @@ -913,7 +909,7 @@ def test_reader_ignore_encoding(reader_cls, tmp_path):
assert reader2.format == FortranFormat(False, True)


def test_inherited_f77():
def test_inherited_f77(tmpdir):
"""
A grab bag of functional tests inherited from readfortran.py.
"""
Expand Down Expand Up @@ -948,8 +944,8 @@ def test_inherited_f77():
assert str(item) == stack.pop(0)

# Reading from file
handle, filename = tempfile.mkstemp(suffix=".f", text=True)
os.close(handle)
filename = f"{tmpdir}/out.f"

with open(filename, "w") as fortran_file:
print(string_f77, file=fortran_file)

Expand Down
6 changes: 3 additions & 3 deletions src/fparser/common/tests/test_sourceinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##############################################################################
# Modified M.Hambley, UK Met Office
# Modified by J. Henrichs, Bureau of Meteorology
##############################################################################
"""
Test battery associated with fparser.sourceinfo package.
Expand Down Expand Up @@ -315,7 +316,7 @@ def extension(request):
##############################################################################


def test_get_source_info_filename(extension, header, content):
def test_get_source_info_filename(tmpdir, extension, header, content):
# pylint: disable=redefined-outer-name
"""
Tests that source format is correctly identified when read from a file.
Expand All @@ -326,8 +327,7 @@ def test_get_source_info_filename(extension, header, content):
if content[0] is not None:
full_source += content[0]

source_file, filename = tempfile.mkstemp(suffix=extension[0], text=True)
os.close(source_file)
filename = f"{tmpdir}/out{extension[0]}"

with open(filename, "w") as source_file:
print(full_source, file=source_file)
Expand Down

0 comments on commit 3bc75fe

Please sign in to comment.