Skip to content

Commit

Permalink
test(MFSimulation): test load with abs/rel paths and forward/back sla…
Browse files Browse the repository at this point in the history
…shes
  • Loading branch information
wpbonelli committed Mar 12, 2023
1 parent 656751a commit 0513c6b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- master
- develop
- ci-diagnose*
- test*
pull_request:
branches:
- master
Expand Down
81 changes: 80 additions & 1 deletion autotest/test_mf6.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
from os.path import join
from pathlib import Path
from shutil import which
from shutil import copytree, which

import numpy as np
import pytest
from modflow_devtools.markers import requires_exe
from modflow_devtools.misc import set_dir

import flopy
from flopy.mf6 import (
Expand Down Expand Up @@ -264,6 +266,83 @@ def test_string_to_file_path():
assert rel_path == new_path, "Relative path error"


def to_msdos_sep(s):
return s.replace("/", "\\")


def to_posix_sep(s):
return s.replace("\\", "/")


def to_os_sep(s):
return s.replace("\\", os.sep).replace("/", os.sep)


def test_load_namefile_with_filenames(function_tmpdir, example_data_path):
ml_name = "freyberg"
nam_name = "mfsim.nam"

sim = MFSimulation.load(
nam_name, sim_ws=example_data_path / f"mf6-{ml_name}"
)
sim.check()


@pytest.mark.skip(reason="abs paths not supported yet")
def test_load_namefile_with_abs_paths(function_tmpdir, example_data_path):
ws = function_tmpdir / "ws"
ml_name = "freyberg"
nam_name = "mfsim.nam"
nam_path = ws / nam_name
copytree(example_data_path / f"mf6-{ml_name}", ws)

with set_dir(ws):
lines = open(nam_path).readlines()
with open(nam_path, "w") as f:
for l in lines:
pattern = f"{ml_name}."
if pattern in l:
l = l.replace(
pattern, str(ws.absolute()) + os.sep + pattern
)
f.write(l + os.linesep)

sim = MFSimulation.load(nam_name, sim_ws=ws)
sim.check()


@pytest.mark.parametrize("sep", ["msdos", "posix"])
def test_load_namefile_with_rel_paths(function_tmpdir, example_data_path, sep):
ws = function_tmpdir / "ws"
ml_name = "freyberg"
nam_name = "mfsim.nam"
nam_path = ws / nam_name
copytree(example_data_path / f"mf6-{ml_name}", ws)

with set_dir(ws):
lines = open(nam_path).readlines()
with open(nam_path, "w") as f:
for l in lines:
pattern = f"{ml_name}."
if pattern in l:
if sep == "msdos":
l = to_msdos_sep(
l.replace(
pattern, "../" + ws.name + "/" + ml_name + "."
)
)
else:
l = to_msdos_sep(
l.replace(
pattern, "../" + ws.name + "/" + ml_name + "."
)
)
f.write(l + os.linesep)

sim = MFSimulation.load(nam_name, sim_ws=ws)
sim.check()


def test_subdir(function_tmpdir):
sim = MFSimulation(sim_ws=function_tmpdir)
assert sim.sim_path == function_tmpdir
Expand Down

0 comments on commit 0513c6b

Please sign in to comment.