Skip to content

Commit

Permalink
WIP common tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hroncok committed Dec 20, 2022
1 parent 843dee6 commit b12be8c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 197 deletions.
119 changes: 119 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import textwrap

import pytest

from utils import (
DOT_TOX,
NATIVE_EXEC_PREFIX_MSG,
NATIVE_EXECUTABLE,
NATIVE_SITE_PACKAGES,
NATIVE_TOXENV,
TOX_VERSION,
TOX4,
envs_from_tox_ini,
modify_config,
expand_tox,
prep_tox_output,
tox,
tox_footer,
)


def test_native_toxenv_current_env():
result = tox("-e", NATIVE_TOXENV, "--current-env")
assert result.stdout.splitlines()[0] == NATIVE_EXEC_PREFIX_MSG
assert not (DOT_TOX / NATIVE_TOXENV / "lib").is_dir()


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps(toxenv, print_deps_stdout_arg):
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = expand_tox(textwrap.dedent(
f"""
[[TOX4:tox]]
six
py
{tox_footer(toxenv)}
"""
).lstrip())
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
@pytest.mark.parametrize("pre_post", ["pre", "post", "both"])
def test_print_deps_with_commands_pre_post(projdir, toxenv, pre_post, print_deps_stdout_arg):
with modify_config(projdir / 'tox.ini') as config:
if pre_post == "both":
config["testenv"]["commands_pre"] = "echo unexpected"
config["testenv"]["commands_post"] = "echo unexpected"
else:
config["testenv"][f"commands_{pre_post}"] = "echo unexpected"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = expand_tox(textwrap.dedent(
f"""
[[TOX4:tox]]
six
py
{tox_footer(toxenv)}
"""
).lstrip())
assert sorted(prep_tox_output(result.stdout).splitlines()) == sorted(
expected.splitlines()
)
assert result.stderr == ""


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox>=3.13
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_requires(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = expand_tox(textwrap.dedent(
f"""
setuptools>30
pluggy
[[TOX4:tox]]
six
py
{tox_footer(toxenv)}
"""
).lstrip())
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion_and_requires(
projdir, toxenv, print_deps_stdout_arg
):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = expand_tox(textwrap.dedent(
f"""
[[TOX3:tox>=3.13]]
setuptools>30
pluggy
[[TOX4:tox>=3.13]]
six
py
{tox_footer(toxenv)}
"""
).lstrip())
assert prep_tox_output(result.stdout) == expected
94 changes: 0 additions & 94 deletions tests/test_integration_tox3.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
pytest.skip("skipping tests for tox 3", allow_module_level=True)


def test_native_toxenv_current_env():
result = tox("-e", NATIVE_TOXENV, "--current-env")
assert result.stdout.splitlines()[0] == NATIVE_EXEC_PREFIX_MSG
assert not (DOT_TOX / NATIVE_TOXENV / "lib").is_dir()


@needs_all_pythons
def test_all_toxenv_current_env():
result = tox("--current-env", check=False)
Expand Down Expand Up @@ -60,94 +54,6 @@ def test_all_toxenv_current_env_skip_missing():
assert result.returncode == 0


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps(toxenv, print_deps_stdout_arg):
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert result.stdout == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
@pytest.mark.parametrize("pre_post", ["pre", "post", "both"])
def test_print_deps_with_commands_pre_post(projdir, toxenv, pre_post, print_deps_stdout_arg):
with modify_config(projdir / 'tox.ini') as config:
if pre_post == "both":
config["testenv"]["commands_pre"] = "echo unexpected"
config["testenv"]["commands_post"] = "echo unexpected"
else:
config["testenv"][f"commands_{pre_post}"] = "echo unexpected"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert result.stdout == expected
assert result.stderr == ""


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox >= 3.13
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert result.stdout == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_requires(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
setuptools > 30
pluggy
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert result.stdout == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion_and_requires(
projdir, toxenv, print_deps_stdout_arg
):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox >= 3.13
setuptools > 30
pluggy
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert result.stdout == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_extras(toxenv, print_extras_stdout_arg):
result = tox("-e", toxenv, print_extras_stdout_arg)
Expand Down
99 changes: 0 additions & 99 deletions tests/test_integration_tox4.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,105 +24,6 @@
pytest.skip("skipping tests for tox 4", allow_module_level=True)


def test_native_toxenv_current_env():
result = tox("-e", NATIVE_TOXENV, "--current-env")
assert result.stdout.splitlines()[0] == NATIVE_EXEC_PREFIX_MSG
assert not (DOT_TOX / NATIVE_TOXENV / "lib").is_dir()


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps(toxenv, print_deps_stdout_arg):
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
@pytest.mark.parametrize("pre_post", ["pre", "post", "both"])
def test_print_deps_with_commands_pre_post(projdir, toxenv, pre_post, print_deps_stdout_arg):
with modify_config(projdir / 'tox.ini') as config:
if pre_post == "both":
config["testenv"]["commands_pre"] = "echo unexpected"
config["testenv"]["commands_post"] = "echo unexpected"
else:
config["testenv"][f"commands_{pre_post}"] = "echo unexpected"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert sorted(prep_tox_output(result.stdout).splitlines()) == sorted(
expected.splitlines()
)
assert result.stderr == ""


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
tox>=3.13
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_requires(projdir, toxenv, print_deps_stdout_arg):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
setuptools>30
pluggy
tox
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_deps_with_tox_minversion_and_requires(
projdir, toxenv, print_deps_stdout_arg
):
with modify_config(projdir / "tox.ini") as config:
config["tox"]["minversion"] = "3.13"
config["tox"]["requires"] = "\n setuptools > 30\n pluggy"
result = tox("-e", toxenv, print_deps_stdout_arg)
expected = textwrap.dedent(
f"""
setuptools>30
pluggy
tox>=3.13
six
py
{tox_footer(toxenv)}
"""
).lstrip()
assert prep_tox_output(result.stdout) == expected


@pytest.mark.parametrize("toxenv", envs_from_tox_ini())
def test_print_extras(toxenv, print_extras_stdout_arg):
result = tox("-e", toxenv, print_extras_stdout_arg)
Expand Down
34 changes: 30 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,36 @@ def tox_footer(envs=None, spaces=8):


def prep_tox_output(output):
"""Remove time info from tox output"""
result = re.sub(r" \((\d+\.\d+|\d+) seconds\)", "", output)
result = re.sub(r" ✔ in (\d+\.\d+|\d+) seconds", "", result)
return result
"""Remove time info from tox 4 output.
Strip spaces around operators in tox 3 output."""
if TOX4:
output = re.sub(r" \((\d+\.\d+|\d+) seconds\)", "", output)
output = re.sub(r" ✔ in (\d+\.\d+|\d+) seconds", "", output)
else:
output = re.sub(" ((<|>)=?) ", r"\1", output)
return output


def expand_tox(text):
"""Replace [[TOX4:...]] and [[TOX3:...]] expressions,
empty liens are stripped if created."""
source_lines = text.splitlines()
out_lines = []
for line in source_lines:
if not line:
out_lines.append(line)
continue
if TOX4:
line = re.sub(r"\[\[TOX3:[^\]]*\]\]", "", line)
line = re.sub(r"\[\[TOX4:([^\]]*)\]\]", r"\1", line)
else:
line = re.sub(r"\[\[TOX4:[^\]]*\]\]", "", line)
line = re.sub(r"\[\[TOX3:([^\]]*)\]\]", r"\1", line)
if line:
out_lines.append(line)
if text[-1] == "\n":
out_lines.append("")
return "\n".join(out_lines)


needs_all_pythons = pytest.mark.skipif(
Expand Down

0 comments on commit b12be8c

Please sign in to comment.