1
0
mirror of https://github.com/fedora-python/tox-current-env.git synced 2024-12-23 16:46:14 +01:00

Add --print-deps-to-file option for easier parsable dependencies

Merges https://github.com/fedora-python/tox-current-env/pull/15
This commit is contained in:
Miro Hrončok 2019-08-12 16:49:14 +02:00 committed by GitHub
commit e7aef3fb2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 6 deletions

View File

@ -14,10 +14,11 @@ The ``tox-current-env`` plugin adds two options:
An attempt to run this with a Python version that doesn't match will fail
(if ``tox`` is invoked from an Python 3.7 environment, any non 3.7 testenv will fail).
``tox --print-deps-only``
``tox --print-deps-only`` / ``--print-deps-to-file``
Instead of running any ``commands``,
simply prints the declared dependencies in ``deps`` to the standard output.
simply prints the declared dependencies in ``deps`` to the standard output or specified file.
This is useful for preparing the current environment for the above.
``--print-deps-to-file`` will overwrite the file if it already exists.
Invoking ``tox`` without any of the above options should behave as regular ``tox`` invocation without this plugin.
Any deviation from this behavior is considered a bug.

View File

@ -18,13 +18,29 @@ def tox_addoption(parser):
action="store_true",
dest="print_deps_only",
default=False,
help="Don't run tests, only print the dependencies",
help="Don't run tests, only print the dependencies to stdout",
)
parser.add_argument(
"--print-deps-to-file",
action="store",
dest="print_deps_path",
metavar="PATH",
default=None,
help="Like --print-deps-only, but to a file. Overwrites the file if it exists.",
)
@tox.hookimpl
def tox_configure(config):
"""Stores options in the config. Makes all commands external and skips sdist"""
if config.option.print_deps_only and config.option.print_deps_path:
raise tox.exception.ConfigError(
"--print-deps-only cannot be used together with --print-deps-to-file"
)
if config.option.print_deps_path is not None:
config.option.print_deps_only = True
with open(config.option.print_deps_path, "w", encoding="utf-8") as f:
f.write("")
if config.option.current_env or config.option.print_deps_only:
config.skipsdist = True
for testenv in config.envconfigs:
@ -136,7 +152,10 @@ def tox_runtest(venv, redirect):
"""If --print-deps-only, prints deps instead of running tests"""
config = venv.envconfig.config
unsupported_raise(config, venv)
if config.option.print_deps_only:
for dependency in venv.get_resolved_dependencies():
print(dependency)
if config.option.print_deps_path is not None:
with open(config.option.print_deps_path, "a", encoding="utf-8") as f:
print(*venv.get_resolved_dependencies(), sep="\n", file=f)
return True
if config.option.print_deps_only:
print(*venv.get_resolved_dependencies(), sep="\n")
return True

View File

@ -113,6 +113,60 @@ def test_allenvs_print_deps_only():
assert result.stdout == expected
@pytest.mark.parametrize("toxenv", ["py36", "py37", "py38"])
def test_print_deps_to_file(toxenv, tmp_path):
depspath = tmp_path / "deps"
result = tox("-e", toxenv, "--print-deps-to-file", str(depspath))
assert depspath.read_text().splitlines() == ["six", "py"]
expected = textwrap.dedent(
f"""
___________________________________ summary ____________________________________
{toxenv}: commands succeeded
congratulations :)
"""
).lstrip()
assert result.stdout == expected
def test_allenvs_print_deps_to_file(tmp_path):
depspath = tmp_path / "deps"
result = tox("--print-deps-to-file", str(depspath))
assert depspath.read_text().splitlines() == ["six", "py"] * 3
expected = textwrap.dedent(
"""
___________________________________ summary ____________________________________
py36: commands succeeded
py37: commands succeeded
py38: commands succeeded
congratulations :)
"""
).lstrip()
assert result.stdout == expected
def test_allenvs_print_deps_to_existing_file(tmp_path):
depspath = tmp_path / "deps"
depspath.write_text("nada")
result = tox("--print-deps-to-file", str(depspath))
lines = depspath.read_text().splitlines()
assert "nada" not in lines
assert "six" in lines
assert "py" in lines
def test_print_deps_only_print_deps_to_file_are_mutually_exclusive():
result = tox(
"-e",
NATIVE_TOXENV,
"--print-deps-only",
"--print-deps-to-file",
"foobar",
check=False,
)
assert result.returncode > 0
assert "cannot be used together" in result.stderr
@needs_py3678
def test_regular_run():
result = tox()