Skip to content

Commit

Permalink
Add tests for new gh_index module
Browse files Browse the repository at this point in the history
The added pytest-plugin "pytest-subprocess" is used to mock calling
git with subprocess.
  • Loading branch information
dalito committed Aug 30, 2023
1 parent 2010d01 commit bac6461
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
# Authors sorted by number of commits as of 2023-07-04
{name = "David Linke", email = "david.linke@catalysis.de"},
{name = "Peter Philips", email = "peter.philips@surroundaustralia.com"},
{name = "Nicholas Car", email = "nicholas.car@surroundaustralia.com"},
{name = "Nicholas Car", email = "nick@kurrawong.net"},
{name = "Jamie Feiss", email = "jamie.feiss@surroundaustralia.com"},
]
maintainers = [
Expand Down Expand Up @@ -60,6 +60,7 @@ Changelog = "https://github.com/nfdi4cat/voc4cat-tool/blob/master/CHANGELOG.md"
[project.optional-dependencies]
tests = [
"pytest",
"pytest-subprocess",
"coverage",
]
lint = [
Expand Down
15 changes: 6 additions & 9 deletions src/voc4cat/gh_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class IndexPage:
def __init__(self, vocpath=None):
self.include_css = True
self.METADATA = {}
self.METADATA["title"] = "Index of vocabulary versions"
self.vocpath = Path(".") if vocpath is None else Path(vocpath)
Expand All @@ -27,14 +26,14 @@ def __init__(self, vocpath=None):
self.tags = []

def _load_template(self, template_file):
return Environment(loader=FileSystemLoader(TEMPLATES_DIR)).get_template(
template_file
)
return Environment(
loader=FileSystemLoader(TEMPLATES_DIR), autoescape=True
).get_template(template_file)

def get_version_data(self):
cmd = ["git", "-C", str(self.vocpath), "tag", "--list", "v[0-9]*-[0-9]*-[0-9]*"]
logger.debug("Running cmd: %s", " ".join(cmd))
outp = subprocess.run(cmd, capture_output=True) # noqa: S603
outp = subprocess.run(cmd, capture_output=True, check=False) # noqa: S603
if outp.returncode != 0:
logger.error("git command returned with error")
return
Expand All @@ -61,10 +60,8 @@ def _make_versions(self):
)

def _make_document(self):
css = None
if self.include_css:
with open(STYLE_DIR / "pylode.css") as f:
css = f.read()
with open(STYLE_DIR / "pylode.css") as f:
css = f.read()

return self._load_template("index.html").render(
schemaorg=None,
Expand Down
69 changes: 69 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import os
import shutil
from pathlib import Path
from unittest import mock

import pytest
from test_cli import (
Expand All @@ -26,6 +28,7 @@ def test_build_docs_ontospy(datadir, tmp_path, test_file):
assert (outdir / Path(CS_CYCLES_TURTLE).stem / "docs" / "index.html").exists()


@mock.patch.dict(os.environ, clear=True) # required to hide gh-action environment vars
def test_build_docs_pylode(datadir, tmp_path):
"""Check that pylode generates the expected output."""
dst = tmp_path
Expand All @@ -36,6 +39,72 @@ def test_build_docs_pylode(datadir, tmp_path):
assert (outdir / Path(CS_CYCLES_TURTLE).stem / "index.html").exists()


@mock.patch.dict(os.environ, {"CI": "TRUE"})
def test_build_docs_pylode_ci_no_git(monkeypatch, datadir, tmp_path, caplog):
"""Check that pylode generates additional index.html in CI, git error."""
dst = tmp_path
monkeypatch.chdir(dst)
shutil.copy(datadir / CS_CYCLES_TURTLE, dst)
shutil.copy(datadir / "valid_idranges.toml", dst / "idranges.toml")
outdir = dst / "pylode"

with caplog.at_level(logging.ERROR), mock.patch(
"voc4cat.gh_index.subprocess"
) as subprocess:
subprocess.Popen.return_value.returncode = 1
main_cli(
["docs", "--force", "--style", "pylode", "--outdir", str(outdir), str(dst)]
)

assert "git command returned with error" in caplog.text


@mock.patch.dict(os.environ, {"CI": "TRUE"})
def test_build_docs_pylode_ci_no_config(monkeypatch, datadir, tmp_path, caplog):
"""Check that pylode generates additional index.html in CI, git error."""
dst = tmp_path
monkeypatch.chdir(dst)
shutil.copy(datadir / CS_CYCLES_TURTLE, dst)
outdir = dst / "pylode"

with caplog.at_level(logging.ERROR), pytest.raises(
Voc4catError, match="Config file not found"
):
main_cli(
["docs", "--force", "--style", "pylode", "--outdir", str(outdir), str(dst)]
)

assert "Config file not found" in caplog.text


@pytest.mark.parametrize("git_output", [b"v2022.12.22\n", b""])
@mock.patch.dict(os.environ, {"CI": "TRUE"})
def test_build_docs_pylode_in_ci( # noqa: PLR0913
fake_process, monkeypatch, datadir, tmp_path, caplog, git_output
):
"""Check that pylode generates additional index.html in CI."""
dst = tmp_path
monkeypatch.chdir(dst)
shutil.copy(datadir / CS_CYCLES_TURTLE, dst)
shutil.copy(datadir / "valid_idranges.toml", dst / "idranges.toml")
outdir = dst / "pylode"
# fake_process is a fixture from pytest-subprocess
fake_process.register(
["git", "-C", ".", "tag", "--list", "v[0-9]*-[0-9]*-[0-9]*"],
stdout=git_output,
)

with caplog.at_level(logging.DEBUG):
main_cli(
["docs", "--force", "--style", "pylode", "--outdir", str(outdir), str(dst)]
)
assert (outdir / Path(CS_CYCLES_TURTLE).stem / "index.html").exists()
assert (outdir / "index.html").exists()
if git_output:
assert "v2022.12.22" in caplog.text


@mock.patch.dict(os.environ, clear=True) # required to hide gh-action environment vars
def test_build_docs(datadir, tmp_path, caplog):
"""Check overwrite warning and output folder creation."""
dst = tmp_path
Expand Down

0 comments on commit bac6461

Please sign in to comment.