From 788fab3cdf01333cf308972efe99eecf78b568de Mon Sep 17 00:00:00 2001 From: Atsushi Togo Date: Fri, 13 Jun 2025 13:43:04 +0900 Subject: [PATCH] Avoid writing files in running tests --- test/file_IO/test_file_IO.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/test/file_IO/test_file_IO.py b/test/file_IO/test_file_IO.py index a6da0028..b2f359c1 100644 --- a/test/file_IO/test_file_IO.py +++ b/test/file_IO/test_file_IO.py @@ -1,6 +1,8 @@ """Tests of Phono3py API.""" +import os import pathlib +import tempfile from collections.abc import Sequence from typing import Optional @@ -11,7 +13,6 @@ from phono3py import Phono3py from phono3py.file_IO import _get_filename_suffix cwd = pathlib.Path(__file__).parent -cwd_called = pathlib.Path.cwd() def test_kappa_filename(): @@ -60,16 +61,25 @@ def test_kappa_hdf5_with_boundary_mpf(si_pbesol: Phono3py): ] boundary_mfp = 10000.0 - kappa_filename = _set_kappa( - si_pbesol, [4, 4, 4], write_kappa=True, boundary_mfp=boundary_mfp - ) - file_path = pathlib.Path(cwd_called / kappa_filename) - with h5py.File(file_path, "r") as f: - np.testing.assert_almost_equal(f["boundary_mfp"][()], boundary_mfp) - assert set(list(f)) == set(key_ref) - if file_path.exists(): - file_path.unlink() + with tempfile.TemporaryDirectory() as temp_dir: + original_cwd = pathlib.Path.cwd() + os.chdir(temp_dir) + + try: + kappa_filename = _set_kappa( + si_pbesol, [4, 4, 4], write_kappa=True, boundary_mfp=boundary_mfp + ) + file_path = pathlib.Path(kappa_filename) + with h5py.File(file_path, "r") as f: + np.testing.assert_almost_equal(f["boundary_mfp"][()], boundary_mfp) + assert set(list(f)) == set(key_ref) + + if file_path.exists(): + file_path.unlink() + + finally: + os.chdir(original_cwd) def _set_kappa(