Avoid writing files in running tests

This commit is contained in:
Atsushi Togo 2025-06-13 13:43:04 +09:00
parent e02e7c9921
commit 788fab3cdf
1 changed files with 20 additions and 10 deletions

View File

@ -1,6 +1,8 @@
"""Tests of Phono3py API.""" """Tests of Phono3py API."""
import os
import pathlib import pathlib
import tempfile
from collections.abc import Sequence from collections.abc import Sequence
from typing import Optional from typing import Optional
@ -11,7 +13,6 @@ from phono3py import Phono3py
from phono3py.file_IO import _get_filename_suffix from phono3py.file_IO import _get_filename_suffix
cwd = pathlib.Path(__file__).parent cwd = pathlib.Path(__file__).parent
cwd_called = pathlib.Path.cwd()
def test_kappa_filename(): def test_kappa_filename():
@ -60,16 +61,25 @@ def test_kappa_hdf5_with_boundary_mpf(si_pbesol: Phono3py):
] ]
boundary_mfp = 10000.0 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(): with tempfile.TemporaryDirectory() as temp_dir:
file_path.unlink() 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( def _set_kappa(