diff --git a/03_Code/off/off.py b/03_Code/off/off.py index e10c980..5e00355 100644 --- a/03_Code/off/off.py +++ b/03_Code/off/off.py @@ -17,6 +17,7 @@ # along with this program (see COPYING file). If not, see . import os +from pathlib import Path import logging lg = logging.getLogger('off') @@ -133,19 +134,20 @@ def __dir_init__(self, settings_sim: dict): run_id = self.__get_runid__() try: - root_dir = data_dir or f'{os.environ["OFF_PATH"]}/runs/' - lg.info('Root runs directory: ' + root_dir) + root_path = Path(data_dir) if data_dir else Path(os.environ["OFF_PATH"]) / "runs" + lg.info('Root runs directory: ' + str(root_path)) except KeyError: - if os.environ["PWD"].endswith("03_Code"): - root_dir = data_dir or f'{os.environ["PWD"][:-len("03_Code")]}/runs/' + cwd = Path.cwd() + if cwd.name == "03_Code": + root_path = Path(data_dir) if data_dir else cwd.parent / "runs" else: - root_dir = data_dir or f'{os.environ["PWD"]}/runs/' + root_path = Path(data_dir) if data_dir else cwd / "runs" - self.sim_dir = f'{root_dir}/off_run_{run_id}' if sim_dir is None else sim_dir - self.root_dir = root_dir[:-len("runs/")] + self.sim_dir = str(root_path / f'off_run_{run_id}') if sim_dir is None else sim_dir + self.root_dir = str(root_path.parent) - if not os.path.exists(self.sim_dir): - os.makedirs(self.sim_dir) + if not Path(self.sim_dir).exists(): + Path(self.sim_dir).mkdir(parents=True, exist_ok=True) lg.info('Created simulation directory at ' + self.sim_dir) def __logger_init__(self, settings_sim: dict): diff --git a/test_off_dir_init.py b/test_off_dir_init.py new file mode 100644 index 0000000..86d704f --- /dev/null +++ b/test_off_dir_init.py @@ -0,0 +1,47 @@ +import sys +from pathlib import Path + + +sys.path.insert(0, str(Path(__file__).resolve().parent / "03_Code")) + +import off.off as off + + +def _make_off(run_id: int) -> off.OFF: + off_obj = off.OFF.__new__(off.OFF) + off_obj.__get_runid__ = lambda: run_id + return off_obj + + +def test_dir_init_falls_back_to_cwd_when_off_path_missing(tmp_path, monkeypatch): + monkeypatch.delenv("OFF_PATH", raising=False) + monkeypatch.setenv("PWD", str(tmp_path / "not_used")) + + cwd = tmp_path / "workspace" + cwd.mkdir() + monkeypatch.chdir(cwd) + + off_obj = _make_off(run_id=1) + off_obj.__dir_init__({"simulation folder": None}) + + expected_sim_dir = cwd / "runs" / "off_run_1" + assert Path(off_obj.sim_dir) == expected_sim_dir + assert off_obj.root_dir == str(cwd) + assert expected_sim_dir.exists() + + +def test_dir_init_uses_parent_when_cwd_is_03_code(tmp_path, monkeypatch): + monkeypatch.delenv("OFF_PATH", raising=False) + monkeypatch.setenv("PWD", str(tmp_path / "not_used")) + + code_dir = tmp_path / "project" / "03_Code" + code_dir.mkdir(parents=True) + monkeypatch.chdir(code_dir) + + off_obj = _make_off(run_id=2) + off_obj.__dir_init__({"simulation folder": None}) + + expected_sim_dir = tmp_path / "project" / "runs" / "off_run_2" + assert Path(off_obj.sim_dir) == expected_sim_dir + assert off_obj.root_dir == str(tmp_path / "project") + assert expected_sim_dir.exists()