Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with modflow6 release and nightly, add test.common module #108

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
matrix:
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, macos-latest, windows-latest]
modflow6-repo: [modflow6, modflow6-nightly-build]

steps:
- uses: actions/checkout@v4
Expand All @@ -26,14 +27,16 @@ jobs:
python-version: "${{ matrix.python-version }}"
- name: Setup Fortran
uses: fortran-lang/setup-fortran@v1
- name: Install MODFLOW 6
uses: modflowpy/install-modflow-action@v1
with:
repo: ${{ matrix.modflow6-repo }}
- name: Install test dependencies
run: |
pip install -e ".[tests]"
- name: Test with pytest
# The flag -s stops pytest from capturing output
# This is necessary until proper error reporting is implemented by Modflow
- name: Run tests with MODFLOW 6 release
run: |
pytest -vs --cov=xmipy --cov-report=xml tests/
pytest -v --cov=xmipy --cov-report=xml
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove the -s?

MODFLOW 6 still crashes for most errors, which means there will be zero output in the CI when tests fail

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Running pytest -s throws way too much to the console, whereas the default will enable pytest to capture the output, showing more "normal" pytests outputs. I'll test locally to see if MF6 crashes are adequately captured with/without the flag...

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know if you got reasonable results.

You could try e.g. an invalid configuration option.

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ docs = ["pdoc"]
Documentation = "https://deltares.github.io/xmipy/xmipy.html"
Source = "https://github.com/Deltares/xmipy"

[tool.hatch.build.targets.wheel]
packages = ["xmipy"]

[tool.hatch.version]
path = "xmipy/__init__.py"

Expand All @@ -50,6 +53,9 @@ disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = true

[tool.pytest.ini_options]
testpaths = ["tests"]
mwtoews marked this conversation as resolved.
Show resolved Hide resolved

[tool.ruff]
select = [
"ARG",
Expand Down
Empty file added tests/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Common functions for tests. For pytest fixtures, see conftest.py."""

import os
import platform
import shutil
from pathlib import Path


def get_libmf6() -> Path:
"""Get libmf6 based on environment variable or PATH with mf6.

To specify directly, set the environment variable `LIBMF6` to the path of
the library to test.

Otherwise, check for the environment variable `MODFLOW_BIN_PATH` as used by
modflowpy/install-modflow-action to find the mf6 executable.

Lastly, find the mf6 executable from the PATH environment, and
try to find the library in the same directory as the executable.
"""
if "LIBMF6" in os.environ:
lib_path = Path(os.environ["LIBMF6"])
else:
if modflow_bin_path := os.environ.get("MODFLOW_BIN_PATH"):
mf6 = shutil.which("mf6", path=modflow_bin_path)
else:
mf6 = shutil.which("mf6")
if mf6 is None:
raise FileNotFoundError("cannot find mf6 on PATH")
mf6 = Path(mf6)
sysinfo = platform.system()
if sysinfo == "Windows":
lib_path = mf6.parent / "libmf6.dll"
elif sysinfo == "Linux":
lib_path = mf6.parent / "libmf6.so"
elif sysinfo == "Darwin":
lib_path = mf6.parent / "libmf6.dylib"
else:
raise NotImplementedError(f"system not supported: {sysinfo}")
if lib_path.exists():
return lib_path
raise FileNotFoundError(f"{lib_path} not found")


libmf6_path = get_libmf6()
146 changes: 26 additions & 120 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform
"""Fixtures for pytest. Other common functions are in common.py."""

from dataclasses import dataclass
from typing import Any, List, Tuple

Expand All @@ -9,22 +10,20 @@

from xmipy import XmiWrapper

from .common import libmf6_path

@pytest.fixture(scope="session")
def modflow_lib_path(tmp_path_factory):
tmp_path = tmp_path_factory.getbasetemp()
sysinfo = platform.system()
if sysinfo == "Windows":
lib_path = tmp_path / "libmf6.dll"
elif sysinfo == "Linux":
lib_path = tmp_path / "libmf6.so"
elif sysinfo == "Darwin":
lib_path = tmp_path / "libmf6.dylib"
else:
raise RuntimeError(f"system not supported: {sysinfo}")

flopy.utils.get_modflow(bindir=str(tmp_path), repo="modflow6-nightly-build")
return str(lib_path)
def pytest_report_header(**kwargs): # noqa: ARG001
"""Show report header at top of pytest output.

https://docs.pytest.org/reference/reference.html#pytest.hookspec.pytest_report_header
"""
return f"libmf6: {libmf6_path}"


@pytest.fixture(scope="session")
def modflow_lib_path():
return libmf6_path


@dataclass
Expand Down Expand Up @@ -167,6 +166,15 @@ def flopy_disu(tmp_path):
flopy.mf6.ModflowTdis(sim, time_units="DAYS", nper=2, perioddata=flopy_disu.tdis_rc)
flopy.mf6.ModflowIms(sim)
gwf = flopy.mf6.ModflowGwf(sim, modelname=flopy_disu.model_name, save_flows=True)
# fmt: off
ja = [0, 1, 3, 1, 0, 2, 4, 2, 1, 5, 3, 0, 4, 6, 4, 1, 3,
5, 7, 5, 2, 4, 8, 6, 3, 7, 7, 4, 6, 8, 8, 5, 7]
# fmt: on
zero_locs = [0, 3, 7, 10, 14, 19, 23, 26, 30]
hwva = np.full(33, 1.0)
hwva[zero_locs] = 0.0
cl12 = np.full(33, 0.5)
cl12[zero_locs] = 0.0
flopy.mf6.ModflowGwfdisu(
gwf,
nodes=9,
Expand All @@ -176,112 +184,10 @@ def flopy_disu(tmp_path):
bot=[-2.0],
area=np.full(9, 1.0),
iac=[3, 4, 3, 4, 5, 4, 3, 4, 3],
ja=[
0,
1,
3,
1,
0,
2,
4,
2,
1,
5,
3,
0,
4,
6,
4,
1,
3,
5,
7,
5,
2,
4,
8,
6,
3,
7,
7,
4,
6,
8,
8,
5,
7,
],
ja=ja,
ihc=[1],
cl12=[
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
0,
0.5,
0.5,
0.5,
0,
0.5,
0.5,
],
hwva=[
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
0,
1.0,
1.0,
1.0,
0,
1.0,
1.0,
],
cl12=cl12,
hwva=hwva,
vertices=[
[0, 0.0, 0.0],
[1, 0.0, 1.0],
Expand Down
Loading