Debian Codegraph @

tasks.py

raw
1 import os
2 from pathlib import Path
3 from typing import Optional
4
5 from invocations import checks, ci
6 from invocations.docs import docs, sites, watch_docs, www
7 from invocations.packaging import release, vendorize
8 from invocations.pytest import coverage as coverage_
9 from invocations.pytest import test as test_
10
11 from invoke import Collection, Context, Exit, task
12
13
14 @task
15 def test(
16 c: Context,
17 verbose: bool = False,
18 color: bool = True,
19 capture: str = "no",
20 module: Optional[str] = None,
21 k: Optional[str] = None,
22 x: bool = False,
23 opts: str = "",
24 pty: bool = True,
25 ) -> None:
26 """
27 Run pytest. See `invocations.pytest.test` for details.
28
29 This is a simple wrapper around the abovementioned task, which makes a
30 couple minor defaults changes appropriate for this particular test suite,
31 such as:
32
33 - setting ``capture=no`` instead of ``capture=sys``, as we do a very large
34 amount of subprocess IO testing that even the ``sys`` capture screws up
35 - setting ``verbose=False`` because we have a large number of tests and
36 skipping verbose output by default is a ~20% time savings.)
37 """
38 # TODO: update test suite to use c.config.run.in_stream = False globally.
39 # somehow.
40 test_(
41 c,
42 verbose=verbose,
43 color=color,
44 capture=capture,
45 module=module,
46 k=k,
47 x=x,
48 opts=opts,
49 pty=pty,
50 )
51
52
53 # TODO: replace with invocations' once the "call truly local tester" problem is
54 # solved (see other TODOs). For now this is just a copy/paste/modify.
55 @task(help=test.help) # type: ignore
56 def integration(
57 c: Context, opts: Optional[str] = None, pty: bool = True
58 ) -> None:
59 """
60 Run the integration test suite. May be slow!
61 """
62 # Abort if no default shell on this system - implies some unusual dev
63 # environment. Certain entirely-standalone tests will fail w/o it, even if
64 # tests honoring config overrides (like the unit-test suite) don't.
65 shell = c.config.global_defaults()["run"]["shell"]
66 if not c.run("which {}".format(shell), hide=True, warn=True):
67 err = "No {} on this system - cannot run integration tests! Try a container?" # noqa
68 raise Exit(err.format(shell))
69 opts = opts or ""
70 opts += " integration/"
71 test(c, opts=opts, pty=pty)
72
73
74 @task
75 def coverage(
76 c: Context, report: str = "term", opts: str = "", codecov: bool = False
77 ) -> None:
78 """
79 Run pytest in coverage mode. See `invocations.pytest.coverage` for details.
80 """
81 # Use our own test() instead of theirs.
82 # Also add integration test so this always hits both.
83 # (Not regression, since that's "weird" / doesn't really hit any new
84 # coverage points)
85 coverage_(
86 c,
87 report=report,
88 opts=opts,
89 tester=test,
90 additional_testers=[integration],
91 codecov=codecov,
92 )
93
94
95 @task
96 def regression(c: Context, jobs: int = 8) -> None:
97 """
98 Run an expensive, hard-to-test-in-pytest run() regression checker.
99
100 :param int jobs: Number of jobs to run, in total. Ideally num of CPUs.
101 """
102 os.chdir("integration/_support")
103 cmd = "seq {} | parallel -n0 --halt=now,fail=1 inv -c regression check"
104 c.run(cmd.format(jobs))
105
106
107 # TODO: hoist up into invocations.checks once proven/needed elsewhere
108 @task
109 def typecheck(c: Context, opts: str = "") -> None:
110 """
111 Run type checking against the project.
112 """
113 # For now it seems easiest to just run a series of checks on the subtrees
114 # instead of forcing mypy to think about all the "duplicate" files across
115 # test vs integration vs main codebase (think _util.py or test fixtures)
116 # See also the exclude= key in pyproject.toml/mypy.ini.
117 root = Path(__file__).parent
118 exclude = ("sites", "docs", "build", "tests")
119 for path in root.iterdir():
120 if path.name in exclude:
121 continue
122 if not path.is_dir() and path.suffix != ".py":
123 continue
124 if path.is_dir():
125 if path.name.startswith("."):
126 continue
127 if not list(path.rglob("**/*.py")):
128 continue
129 c.run(f"mypy {opts} {path}", pty=True, echo=True)
130
131
132 ns = Collection(
133 # Local
134 integration,
135 regression,
136 test,
137 typecheck,
138 # Imported
139 checks,
140 checks.blacken,
141 ci,
142 coverage,
143 docs,
144 integration,
145 regression,
146 release,
147 sites,
148 test,
149 vendorize,
150 watch_docs,
151 www,
152 )
153 ns.configure(
154 {
155 "blacken": {
156 # Skip vendor, build dirs when blackening.
157 # TODO: this is making it seem like I really do want an explicit
158 # arg/conf-opt in the blacken task for "excluded paths"...ha
159 r"find_opts": "-and -not \\( -path './invoke/vendor*' -or -path './build*' -or -path './.cci_pycache*' \\)" # noqa
160 },
161 "packaging": {
162 "wheel": True,
163 "check_desc": True,
164 "changelog_file": os.path.join(
165 www.configuration()["sphinx"]["source"], "changelog.rst"
166 ),
167 },
168 }
169 )