Skip to content

Commit

Permalink
fix testing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Nov 22, 2023
1 parent 2149eab commit 14d4f7c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ name: xhydro
channels:
- conda-forge
dependencies:
# Don't forget to sync changes between environment.yml, environment-dev.yml, and pyproject.toml!
- python >=3.9,<3.12
# Don't forget to sync changes between environment.yml, environment-dev.yml, and pyproject.toml!
# Main packages
- numpy
- statsmodels
- xarray
Expand Down
20 changes: 13 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = [
"statsmodels",
"xarray",
"xclim>=0.45.0",
"xscen"
"xscen>=0.7.1"
]

[project.optional-dependencies]
Expand All @@ -64,15 +64,18 @@ dev = [
]
docs = [
# Documentation and examples
"furo",
"hvplot",
"ipykernel",
"ipython",
"jupyter_client",
"nbsphinx",
"nbval",
"sphinx",
"sphinx-autoapi",
"sphinx-codeautolink",
"sphinx-copybutton",
"sphinx-rtd-theme>=1.0",
"nbsphinx",
"pandoc",
"ipython",
"ipykernel",
"jupyter_client"
"sphinxcontrib-napoleon"
]

[project.urls]
Expand Down Expand Up @@ -155,6 +158,9 @@ addopts = [
]
filterwarnings = ["ignore::UserWarning"]
testpaths = "tests"
markers = [
"requires_docs: mark tests that can only be run with documentation present (deselect with '-m \"not requires_docs\"')"
]

[tool.ruff]
src = [""]
Expand Down
26 changes: 18 additions & 8 deletions tests/test_testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import pytest

import xhydro as xh
import xhydro.testing.utils as xhu


def test_publish_release_notes():
changelog_md = xhu.publish_release_notes(style="md")
@pytest.mark.requires_docs
def test_publish_release_notes(tmp_path):
temp_md_filename = tmp_path.joinpath("version_info.md")
xhu.publish_release_notes(style="md", file=temp_md_filename)

with open(temp_md_filename) as f:
changelog = f.read()

assert changelog_md.startswith("# Changelog")
assert changelog.startswith("# Changelog")
version = xh.__version__
vsplit = version.split(".")

Expand All @@ -15,10 +22,13 @@ def test_publish_release_notes():
+ str(int(vsplit[1]) + 1 if vsplit[2] != "0" else vsplit[1])
+ ".0"
)
assert f"## v{v_4history}" in changelog_md
assert ":user:`" not in changelog_md
assert ":issue:`" not in changelog_md
assert ":pull:`" not in changelog_md
assert f"## v{v_4history}" in changelog
assert ":user:`" not in changelog
assert ":issue:`" not in changelog
assert ":pull:`" not in changelog

changelog_rst = xhu.publish_release_notes(style="rst")
temp_rst_filename = tmp_path.joinpath("version_info.rst")
xhu.publish_release_notes(style="rst", file=temp_rst_filename)
with open(temp_rst_filename) as f:
changelog_rst = f.read()
assert changelog_rst.startswith("=========\nChangelog\n=========")
14 changes: 12 additions & 2 deletions xhydro/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


def publish_release_notes(
style: str = "md", file: Optional[Union[os.PathLike, StringIO, TextIO]] = None
style: str = "md",
file: Optional[Union[os.PathLike, StringIO, TextIO]] = None,
changes: Union[str, os.PathLike] = None,
) -> Optional[str]:
"""Format release history in Markdown or ReStructuredText.
Expand All @@ -18,6 +20,9 @@ def publish_release_notes(
Use ReStructuredText (`rst`) or Markdown (`md`) formatting. Default: Markdown.
file : {os.PathLike, StringIO, TextIO, None}
If provided, prints to the given file-like object. Otherwise, returns a string.
changes : {str, os.PathLike}, optional
If provided, manually points to the file where the changelog can be found.
Assumes a relative path otherwise.
Returns
-------
Expand All @@ -28,7 +33,10 @@ def publish_release_notes(
This function exists solely for development purposes.
Adapted from xclim.testing.utils.publish_release_notes.
"""
changes_file = Path(__file__).parent.parent.parent.joinpath("CHANGES.rst")
if isinstance(changes, (str, Path)):
changes_file = Path(changes).absolute()
else:
changes_file = Path(__file__).absolute().parents[2].joinpath("CHANGES.rst")

if not changes_file.exists():
raise FileNotFoundError("Changes file not found in xhydro file tree.")
Expand Down Expand Up @@ -77,4 +85,6 @@ def publish_release_notes(

if not file:
return
if isinstance(file, (Path, os.PathLike)):
file = Path(file).open("w")
print(changes, file=file)

0 comments on commit 14d4f7c

Please sign in to comment.