Skip to content

Commit

Permalink
Merge pull request #254 from snek5000/test-readonly-sim-dir
Browse files Browse the repository at this point in the history
Test HAS_TO_SAVE and NEW_DIR_RESULTS with a readonly simulation directory
  • Loading branch information
paugier authored Dec 19, 2022
2 parents 91d0340 + afbc670 commit ecdbf4f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import shutil
import sys
import tempfile
from contextlib import contextmanager
from pathlib import Path

import numpy as np
import pymech
import pytest

from snek5000 import load
from snek5000.util.gfortran_log import log_matches


Expand Down Expand Up @@ -204,6 +206,29 @@ def sim_cbox_executed():
return sim


@pytest.fixture(scope="session")
def sim_cbox_executed_readonly(sim_cbox_executed):
path_run = Path(sim_cbox_executed.output.path_run)
with tempfile.TemporaryDirectory(suffix="snek5000_cbox_executed") as tmp_dir:
path_readonly_sim = Path(tmp_dir) / path_run.name
shutil.copytree(path_run, path_readonly_sim)

for path in path_readonly_sim.rglob("*"):
mod = 0o444
if path.is_dir():
mod = 0o544
path.chmod(mod)

sim = load(path_readonly_sim)
yield sim

for path in path_readonly_sim.rglob("*"):
mod = 0o644
if path.is_dir():
mod = 0o744
path.chmod(mod)


@pytest.fixture(scope="session")
def sim_tgv_executed():
from snek5000_tgv.solver import Simul
Expand Down
10 changes: 5 additions & 5 deletions tests/test_restart_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,34 @@ def test_restart_command_only_init(sim_cbox_executed):
main()


def _test_run(sim_cbox_executed, capsys, command, end_time):
def _test_run(sim_executed, capsys, command, end_time):
with patch.object(sys, "argv", command):
with unset_snek_debug():
main()

out = capsys.readouterr().out
path_new = out.split("# To visualize with IPython:\n\ncd ")[-1].split(";")[0]
sim = load(path_new)
assert sim_cbox_executed.path_run != sim.path_run
assert sim_executed.path_run != sim.path_run
assert (sim.path_run / "session_00").exists()

header = read_header(sim.output.get_field_file())
assert header.time == end_time


@pytest.mark.slow
def test_restart_command_run(sim_cbox_executed, capsys):
def test_restart_command_run(sim_cbox_executed_readonly, capsys):
end_time = 0.012
command = [
"snek-restart",
sim_cbox_executed.output.path_run,
str(sim_cbox_executed_readonly.output.path_run),
"--use-checkpoint",
"1",
"--new-dir-results",
"--end-time",
str(end_time),
]
_test_run(sim_cbox_executed, capsys, command, end_time)
_test_run(sim_cbox_executed_readonly, capsys, command, end_time)


@pytest.mark.slow
Expand Down

0 comments on commit ecdbf4f

Please sign in to comment.