~javiljoen/yatte

6ebe5993e78eaa34917f340ad055d18a65879ceb — JA Viljoen 7 months ago d070de9 master
Change utils.check_envvars() name & return type

It now throws an error if any of the given variables are undefined,
instead of returning them as a non-empty set[str].
This and the new name should make the intent clearer at the call site.
3 files changed, 27 insertions(+), 7 deletions(-)

M docs/doctasks.py
M tests/test_utils.py
M yatte/utils.py
M docs/doctasks.py => docs/doctasks.py +6 -4
@@ 6,7 6,7 @@ from pathlib import Path

import yatte
from yatte import task
from yatte.utils import check_envvars, is_newer, mkdir, run, stderr
from yatte.utils import assert_defined, is_newer, mkdir, run, stderr

docs = Path("docs")
pages = docs / "pages"


@@ 45,8 45,10 @@ def render_docs_site():
@task("publish")
def upload_docs():
    """Upload documentation site."""
    if check_envvars({"PAGES_SRHT_TOKEN"}):
        error("Environment variable PAGES_SRHT_TOKEN is undefined; cannot upload docs.")
    try:
        assert_defined({"PAGES_SRHT_TOKEN"})
    except AssertionError as e:
        error(e)
        raise SystemExit(1)

    render_docs_site()


@@ 116,7 118,7 @@ def get_title(doc):
    h1 = xml.fromstring(doc).find("h1")

    if h1 is None or h1.text is None:
        raise ValueError("missing <h1> as child of document root")
        raise ValueError("Missing <h1> as child of document root")

    return h1.text


M tests/test_utils.py => tests/test_utils.py +13 -0
@@ 59,3 59,16 @@ def test_run_commands_that_fail_in_parallel(capfd):
        "$ sleep 0.01; echo ok",
    ]
    assert std.out == "ok\n"


def test_assert_defined():
    defined_vars = {"HOME", "PATH"}
    undefined_vars = {"SOME_UNDEFINED_ENV_VAR", "ANOTHER_UNKNOWN_VAR"}

    yatte.utils.assert_defined(defined_vars)

    with pytest.raises(AssertionError) as e:
        yatte.utils.assert_defined(defined_vars | undefined_vars)

    for v in undefined_vars:
        assert v in str(e.value)

M yatte/utils.py => yatte/utils.py +8 -3
@@ 69,6 69,11 @@ def is_newer(f, than):
    return f.is_file() and f.stat().st_mtime > than.stat().st_mtime


def check_envvars(names):
    """Return the environment variables in names that are undefined."""
    return names - set(os.environ)
def assert_defined(names):
    """Check that the given names are defined as environment variables.

    Throws an AssertionError if any are undefined.
    """
    undefined = set(names) - set(os.environ)
    undefined_s = ", ".join(sorted(undefined))
    assert not undefined, f"Undefined environment variables: {undefined_s}"