1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""
Functions for use with cog in the documentation.
"""
# For help text in doc/cmd.rst:
# optparse wraps help to the COLUMNS value. Set it here to be sure it's
# consistent regardless of the environment. Has to be set before we
# import cmdline.py, which creates the optparse objects.
# pylint: disable=wrong-import-position
import os
os.environ["COLUMNS"] = "80"
import contextlib
import io
import re
import textwrap
import cog # pylint: disable=import-error
from coverage.cmdline import CoverageScript
from coverage.config import read_coverage_config
def show_help(cmd):
"""
Insert the help output from a command.
"""
with contextlib.redirect_stdout(io.StringIO()) as stdout:
CoverageScript().command_line([cmd, "--help"])
help_text = stdout.getvalue()
help_text = help_text.replace("__main__.py", "coverage")
help_text = re.sub(r"(?m)^Full doc.*$", "", help_text)
help_text = help_text.rstrip()
print(".. code::\n")
print(f" $ coverage {cmd} --help")
print(textwrap.indent(help_text, " "))
def _read_config(text, fname):
"""
Prep and read configuration text.
Returns the prepared text, and a dict of the settings.
"""
# Text will be triple-quoted with an initial ignored newline.
assert text[0] == "\n"
text = textwrap.dedent(text[1:])
os.makedirs("tmp", exist_ok=True)
with open(f"tmp/{fname}", "w") as f:
f.write(text)
config = read_coverage_config(f"tmp/{fname}", warn=cog.error)
values = {}
for name, val in vars(config).items():
if name.startswith("_"):
continue
if "config_file" in name:
continue
values[name] = val
return text, values
def show_configs(ini, toml):
"""
Show configuration text in a tabbed box.
`ini` is the ini-file syntax, `toml` is the equivalent TOML syntax.
The equivalence is checked for accuracy, and the process fails if there's
a mismatch.
A three-tabbed box will be produced.
"""
ini, ini_vals = _read_config(ini, "covrc")
toml, toml_vals = _read_config(toml, "covrc.toml")
for key, val in ini_vals.items():
if val != toml_vals[key]:
cog.error(f"Mismatch! {key}:\nini: {val!r}\ntoml: {toml_vals[key]!r}")
ini2 = re.sub(r"(?m)^\[", "[coverage:", ini)
print()
print(".. tabs::\n")
for name, syntax, text in [
(".coveragerc", "ini", ini),
("pyproject.toml", "toml", toml),
("setup.cfg or tox.ini", "ini", ini2),
]:
print(f" .. code-tab:: {syntax}")
print(f" :caption: {name}")
print()
print(textwrap.indent(text, " " * 8))
|