| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
if TYPE_CHECKING: |
| 6 |
from tox.pytest import ToxProjectCreator |
| 7 |
|
| 8 |
|
| 9 |
def test_extra_setup_commands_runs_with_notest(tox_project: ToxProjectCreator) -> None: |
| 10 |
ini = """ |
| 11 |
[testenv] |
| 12 |
package = skip |
| 13 |
deps = pip |
| 14 |
extra_setup_commands = python -c 'print("extra setup")' |
| 15 |
commands = python -c 'print("main command")' |
| 16 |
""" |
| 17 |
proj = tox_project({"tox.ini": ini}) |
| 18 |
result = proj.run("r", "--notest") |
| 19 |
result.assert_success() |
| 20 |
assert "extra setup" in result.out |
| 21 |
assert "main command" not in result.out |
| 22 |
|
| 23 |
|
| 24 |
def test_extra_setup_commands_runs_without_notest(tox_project: ToxProjectCreator) -> None: |
| 25 |
ini = """ |
| 26 |
[testenv] |
| 27 |
package = skip |
| 28 |
deps = pip |
| 29 |
extra_setup_commands = python -c 'print("extra setup")' |
| 30 |
commands = python -c 'print("main command")' |
| 31 |
""" |
| 32 |
proj = tox_project({"tox.ini": ini}) |
| 33 |
result = proj.run("r") |
| 34 |
result.assert_success() |
| 35 |
assert "extra setup" in result.out |
| 36 |
assert "main command" in result.out |
| 37 |
|
| 38 |
|
| 39 |
def test_extra_setup_commands_with_package_skip(tox_project: ToxProjectCreator) -> None: |
| 40 |
ini = """ |
| 41 |
[testenv] |
| 42 |
package = skip |
| 43 |
extra_setup_commands = python -c 'import sys; sys.exit(0)' |
| 44 |
""" |
| 45 |
proj = tox_project({"tox.ini": ini}) |
| 46 |
result = proj.run("r", "--notest") |
| 47 |
result.assert_success() |
| 48 |
|
| 49 |
|
| 50 |
def test_extra_setup_commands_with_package_install(tox_project: ToxProjectCreator) -> None: |
| 51 |
ini = "[testenv]\nextra_setup_commands = python -c 'print(\"after package install\")'" |
| 52 |
proj = tox_project({"tox.ini": ini}) |
| 53 |
result = proj.run("r", "--notest") |
| 54 |
result.assert_success() |
| 55 |
assert "after package install" in result.out |
| 56 |
|
| 57 |
|
| 58 |
def test_extra_setup_commands_failure_stops_execution(tox_project: ToxProjectCreator) -> None: |
| 59 |
ini = """ |
| 60 |
[testenv] |
| 61 |
package = skip |
| 62 |
extra_setup_commands = python -c 'import sys; sys.exit(1)' |
| 63 |
commands = python -c 'print("should not run")' |
| 64 |
""" |
| 65 |
proj = tox_project({"tox.ini": ini}) |
| 66 |
result = proj.run("r", "--notest") |
| 67 |
result.assert_failed() |
| 68 |
assert "extra_setup_commands failed" in result.out |
| 69 |
|
| 70 |
|
| 71 |
def test_extra_setup_commands_failure_with_ignore_errors(tox_project: ToxProjectCreator) -> None: |
| 72 |
ini = """ |
| 73 |
[testenv] |
| 74 |
package = skip |
| 75 |
ignore_errors = true |
| 76 |
extra_setup_commands = python -c 'import sys; sys.exit(1)' |
| 77 |
commands = python -c 'print("main command")' |
| 78 |
""" |
| 79 |
proj = tox_project({"tox.ini": ini}) |
| 80 |
result = proj.run("r") |
| 81 |
result.assert_success() |
| 82 |
assert "main command" in result.out |
| 83 |
|
| 84 |
|
| 85 |
def test_extra_setup_commands_multiple_commands(tox_project: ToxProjectCreator) -> None: |
| 86 |
ini = """ |
| 87 |
[testenv] |
| 88 |
package = skip |
| 89 |
extra_setup_commands = |
| 90 |
python -c 'print("cmd1")' |
| 91 |
python -c 'print("cmd2")' |
| 92 |
python -c 'print("cmd3")' |
| 93 |
""" |
| 94 |
proj = tox_project({"tox.ini": ini}) |
| 95 |
result = proj.run("r", "--notest") |
| 96 |
result.assert_success() |
| 97 |
assert "cmd1" in result.out |
| 98 |
assert "cmd2" in result.out |
| 99 |
assert "cmd3" in result.out |
| 100 |
|
| 101 |
|
| 102 |
def test_extra_setup_commands_empty_default(tox_project: ToxProjectCreator) -> None: |
| 103 |
ini = "[testenv]\npackage = skip" |
| 104 |
proj = tox_project({"tox.ini": ini}) |
| 105 |
result = proj.run("r", "--notest") |
| 106 |
result.assert_success() |
| 107 |
|
| 108 |
|
| 109 |
def test_extra_setup_commands_execution_order(tox_project: ToxProjectCreator) -> None: |
| 110 |
ini = """ |
| 111 |
[testenv] |
| 112 |
package = skip |
| 113 |
deps = pip |
| 114 |
extra_setup_commands = python -c 'print("EXTRA_SETUP")' |
| 115 |
commands_pre = python -c 'print("COMMANDS_PRE")' |
| 116 |
commands = python -c 'print("COMMANDS")' |
| 117 |
commands_post = python -c 'print("COMMANDS_POST")' |
| 118 |
""" |
| 119 |
proj = tox_project({"tox.ini": ini}) |
| 120 |
result = proj.run("r") |
| 121 |
result.assert_success() |
| 122 |
|
| 123 |
assert "EXTRA_SETUP" in result.out |
| 124 |
assert "COMMANDS_PRE" in result.out |
| 125 |
assert "COMMANDS" in result.out |
| 126 |
assert "COMMANDS_POST" in result.out |
| 127 |
|
| 128 |
lines = result.out.split("\n") |
| 129 |
extra_setup_idx = next(i for i, line in enumerate(lines) if "EXTRA_SETUP" in line) |
| 130 |
commands_pre_idx = next(i for i, line in enumerate(lines) if "COMMANDS_PRE" in line) |
| 131 |
commands_idx = next( |
| 132 |
i |
| 133 |
for i, line in enumerate(lines) |
| 134 |
if "COMMANDS" in line and "COMMANDS_PRE" not in line and "COMMANDS_POST" not in line |
| 135 |
) |
| 136 |
commands_post_idx = next(i for i, line in enumerate(lines) if "COMMANDS_POST" in line) |
| 137 |
|
| 138 |
assert extra_setup_idx < commands_pre_idx < commands_idx < commands_post_idx |