diff --git a/openmc/utility_funcs.py b/openmc/utility_funcs.py index 9d397a89bab..487f498936b 100644 --- a/openmc/utility_funcs.py +++ b/openmc/utility_funcs.py @@ -69,6 +69,14 @@ def input_path(filename: PathLike) -> Path: Path object """ + # An empty filename represents the absence of a path (e.g., a mesh or + # DAGMC universe read back from a summary/statepoint file that was never + # written with a source file) and should not be resolved to the current + # working directory. + filename = os.fspath(filename) + if not filename: + return Path(filename) + if openmc.config['resolve_paths']: path = Path(filename) xml_dir = _XML_INPUT_PATH.get() diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 861937e0c12..3aae6213ae9 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -470,7 +470,8 @@ def test_umesh_from_hdf5_without_filename(run_in_tmpdir): group['connectivity'] = np.zeros((1, 8), dtype=int) group['element_types'] = [0] - mesh = openmc.MeshBase.from_hdf5(group) + with openmc.config.patch('resolve_paths', True): + mesh = openmc.MeshBase.from_hdf5(group) assert mesh.filename == Path() assert mesh.has_statepoint_data diff --git a/tests/unit_tests/test_universe.py b/tests/unit_tests/test_universe.py index 8b3e07a773a..cc13950b7bc 100644 --- a/tests/unit_tests/test_universe.py +++ b/tests/unit_tests/test_universe.py @@ -198,7 +198,8 @@ def test_dagmc_universe_from_hdf5_without_filename(run_in_tmpdir): group.attrs['auto_mat_ids'] = 1 group.attrs['length_multiplier'] = 1.0 - univ = openmc.DAGMCUniverse.from_hdf5(group) + with openmc.config.patch('resolve_paths', True): + univ = openmc.DAGMCUniverse.from_hdf5(group) assert univ.id == 1 assert univ.filename == Path() diff --git a/tests/unit_tests/test_utility_funcs.py b/tests/unit_tests/test_utility_funcs.py new file mode 100644 index 00000000000..77b28df3449 --- /dev/null +++ b/tests/unit_tests/test_utility_funcs.py @@ -0,0 +1,20 @@ +from pathlib import Path + +import openmc +from openmc.utility_funcs import input_path + + +def test_input_path_empty_not_resolved(): + """An empty filename represents "no file" (e.g., an in-memory mesh or + DAGMC universe read back from a summary/statepoint that was never + written with a source file, see UnstructuredMesh.from_hdf5 and + DAGMCUniverse.from_hdf5) and should stay empty rather than being + resolved to the current working directory.""" + with openmc.config.patch('resolve_paths', True): + assert input_path('') == Path() + + +def test_input_path_nonempty_resolved(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + with openmc.config.patch('resolve_paths', True): + assert input_path('foo.h5') == (tmp_path / 'foo.h5').resolve() diff --git a/tests/unit_tests/weightwindows/test.py b/tests/unit_tests/weightwindows/test.py index 1d10f50ac3c..adb563c31a4 100644 --- a/tests/unit_tests/weightwindows/test.py +++ b/tests/unit_tests/weightwindows/test.py @@ -1,11 +1,10 @@ import os from itertools import product +from pathlib import Path import pytest - import numpy as np from uncertainties import ufloat - import openmc import openmc.lib from openmc.stats import Discrete, Point