| 1 |
import os |
| 2 |
from os.path import join |
| 3 |
from pathlib import Path |
| 4 |
from shutil import copytree, rmtree |
| 5 |
|
| 6 |
from invocations import checks |
| 7 |
from invocations.docs import docs, sites, watch_docs, www |
| 8 |
from invocations.packaging.release import ns as release_coll |
| 9 |
from invocations.packaging.release import publish |
| 10 |
from invocations.testing import count_errors |
| 11 |
from invoke import Collection, task |
| 12 |
|
| 13 |
|
| 14 |
# TODO: this screams out for the invoke missing-feature of "I just wrap task X, |
| 15 |
# assume its signature by default" (even if that is just **kwargs support) |
| 16 |
@task |
| 17 |
def test( |
| 18 |
ctx, |
| 19 |
verbose=True, |
| 20 |
color=True, |
| 21 |
capture="sys", |
| 22 |
module=None, |
| 23 |
k=None, |
| 24 |
x=False, |
| 25 |
opts="", |
| 26 |
coverage=False, |
| 27 |
include_slow=False, |
| 28 |
loop_on_fail=False, |
| 29 |
): |
| 30 |
""" |
| 31 |
Run unit tests via pytest. |
| 32 |
|
| 33 |
By default, known-slow parts of the suite are SKIPPED unless |
| 34 |
``--include-slow`` is given. (Note that ``--include-slow`` does not mesh |
| 35 |
well with explicit ``--opts="-m=xxx"`` - if ``-m`` is found in ``--opts``, |
| 36 |
``--include-slow`` will be ignored!) |
| 37 |
""" |
| 38 |
if verbose and "--verbose" not in opts and "-v" not in opts: |
| 39 |
opts += " --verbose" |
| 40 |
# TODO: forget why invocations.pytest added this; is it to force color when |
| 41 |
# running headless? Probably? |
| 42 |
if color: |
| 43 |
opts += " --color=yes" |
| 44 |
opts += " --capture={}".format(capture) |
| 45 |
if "-m" not in opts and not include_slow: |
| 46 |
opts += " -m 'not slow'" |
| 47 |
if k is not None and not ("-k" in opts if opts else False): |
| 48 |
opts += " -k {}".format(k) |
| 49 |
if x and not ("-x" in opts if opts else False): |
| 50 |
opts += " -x" |
| 51 |
if loop_on_fail and not ("-f" in opts if opts else False): |
| 52 |
opts += " -f" |
| 53 |
modstr = "" |
| 54 |
if module is not None: |
| 55 |
base = f"{module}.py" |
| 56 |
tests = Path("tests") |
| 57 |
legacy = tests / f"test_{base}" |
| 58 |
modstr = str(legacy if legacy.exists() else tests / base) |
| 59 |
# Switch runner depending on coverage or no coverage. |
| 60 |
# TODO: get pytest's coverage plugin working, IIRC it has issues? |
| 61 |
runner = "pytest" |
| 62 |
if coverage: |
| 63 |
# Leverage how pytest can be run as 'python -m pytest', and then how |
| 64 |
# coverage can be told to run things in that manner instead of |
| 65 |
# expecting a literal .py file. |
| 66 |
runner = "coverage run -m pytest" |
| 67 |
# Strip SSH_AUTH_SOCK from parent env to avoid pollution by interactive |
| 68 |
# users. |
| 69 |
# TODO: once pytest coverage plugin works, see if there's a pytest-native |
| 70 |
# way to handle the env stuff too, then we can remove these tasks entirely |
| 71 |
# in favor of just "run pytest"? |
| 72 |
env = dict(os.environ) |
| 73 |
if "SSH_AUTH_SOCK" in env: |
| 74 |
del env["SSH_AUTH_SOCK"] |
| 75 |
cmd = "{} {} {}".format(runner, opts, modstr) |
| 76 |
# NOTE: we have a pytest.ini and tend to use that over PYTEST_ADDOPTS. |
| 77 |
ctx.run(cmd, pty=True, env=env, replace_env=True) |
| 78 |
|
| 79 |
|
| 80 |
@task |
| 81 |
def coverage(ctx, opts=""): |
| 82 |
""" |
| 83 |
Execute all tests (normal and slow) with coverage enabled. |
| 84 |
""" |
| 85 |
test(ctx, coverage=True, include_slow=True, opts=opts) |
| 86 |
# NOTE: codecov now handled purely in invocations/orb |
| 87 |
|
| 88 |
|
| 89 |
@task |
| 90 |
def guard(ctx, opts=""): |
| 91 |
""" |
| 92 |
Execute all tests and then watch for changes, re-running. |
| 93 |
""" |
| 94 |
# TODO if coverage was run via pytest-cov, we could add coverage here too |
| 95 |
return test(ctx, include_slow=True, loop_on_fail=True, opts=opts) |
| 96 |
|
| 97 |
|
| 98 |
# Until we stop bundling docs w/ releases. Need to discover use cases first. |
| 99 |
# TODO: would be nice to tie this into our own version of build() too, but |
| 100 |
# still have publish() use that build()...really need to try out classes! |
| 101 |
# TODO (backwards incompat): I'd like to just axe the 'built docs in sdist', |
| 102 |
# none of my other projects do it. |
| 103 |
@task |
| 104 |
def publish_( |
| 105 |
ctx, sdist=True, wheel=True, sign=False, dry_run=False, index=None |
| 106 |
): |
| 107 |
""" |
| 108 |
Wraps invocations.packaging.publish to add baked-in docs folder. |
| 109 |
""" |
| 110 |
# Build docs first. Use terribad workaround pending invoke #146 |
| 111 |
ctx.run("inv docs", pty=True, hide=False) |
| 112 |
# Move the built docs into where Epydocs used to live |
| 113 |
target = "docs" |
| 114 |
rmtree(target, ignore_errors=True) |
| 115 |
# TODO: make it easier to yank out this config val from the docs coll |
| 116 |
copytree("sites/docs/_build", target) |
| 117 |
# Publish |
| 118 |
publish( |
| 119 |
ctx, sdist=sdist, wheel=wheel, sign=sign, dry_run=dry_run, index=index |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
# Also have to hack up the newly enhanced all_() so it uses our publish |
| 124 |
@task(name="all", default=True) |
| 125 |
def all_(c, dry_run=False): |
| 126 |
release_coll["prepare"](c, dry_run=dry_run) |
| 127 |
publish_(c, dry_run=dry_run) |
| 128 |
release_coll["push"](c, dry_run=dry_run) |
| 129 |
|
| 130 |
|
| 131 |
# TODO: "replace one task with another" needs a better public API, this is |
| 132 |
# using unpublished internals & skips all the stuff add_task() does re: |
| 133 |
# aliasing, defaults etc. |
| 134 |
release_coll.tasks["publish"] = publish_ |
| 135 |
release_coll.tasks["all"] = all_ |
| 136 |
|
| 137 |
ns = Collection( |
| 138 |
test, |
| 139 |
coverage, |
| 140 |
guard, |
| 141 |
release_coll, |
| 142 |
docs, |
| 143 |
www, |
| 144 |
watch_docs, |
| 145 |
sites, |
| 146 |
count_errors, |
| 147 |
checks.blacken, |
| 148 |
checks, |
| 149 |
) |
| 150 |
ns.configure( |
| 151 |
{ |
| 152 |
"packaging": { |
| 153 |
# NOTE: many of these are also set in kwarg defaults above; but |
| 154 |
# having them here too means once we get rid of our custom |
| 155 |
# release(), the behavior stays. |
| 156 |
"sign": False, |
| 157 |
"wheel": True, |
| 158 |
"changelog_file": join( |
| 159 |
www.configuration()["sphinx"]["source"], "changelog.rst" |
| 160 |
), |
| 161 |
}, |
| 162 |
"blacken": {"find_opts": r"-and -not -path '*.cci_pycache*'"}, |
| 163 |
"docs": {"browse": "remote"}, |
| 164 |
} |
| 165 |
) |