Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions 03_Code/off/off.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program (see COPYING file). If not, see <https://www.gnu.org/licenses/>.

import os
from pathlib import Path

import logging
lg = logging.getLogger('off')
Expand Down Expand Up @@ -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):
Expand Down
47 changes: 47 additions & 0 deletions test_off_dir_init.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

Original file line number Diff line number Diff line change
@@ -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()