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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
import shlex
import sys
from collections import namedtuple
from textwrap import dedent
import pytest
from pdm.cli import actions
from pdm.cli.options import from_splitted_env
from pdm.signals import pdm_signals
pytestmark = pytest.mark.usefixtures("repository", "working_set", "local_finder")
def test_pre_script_fail_fast(project, pdm, capfd, mocker):
project.pyproject.settings["scripts"] = {
"pre_install": "python -c \"print('PRE INSTALL CALLED'); exit(1)\"",
"post_install": "python -c \"print('POST INSTALL CALLED')\"",
}
project.pyproject.write()
synchronize = mocker.patch("pdm.installers.synchronizers.Synchronizer.synchronize")
result = pdm(["install"], obj=project)
assert result.exit_code == 1
out, _ = capfd.readouterr()
assert "PRE INSTALL CALLED" in out
assert "POST INSTALL CALLED" not in out
synchronize.assert_not_called()
def test_pre_and_post_scripts(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"pre_script": "python echo.py pre_script",
"post_script": "python echo.py post_script",
"pre_test": "python echo.py pre_test",
"test": "python echo.py test",
"post_test": "python echo.py post_test",
"pre_run": "python echo.py pre_run",
"post_run": "python echo.py post_run",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
expected = dedent(
"""
pre_run CALLED
pre_script CALLED
pre_test CALLED
test CALLED
post_test CALLED
post_script CALLED
post_run CALLED
"""
).strip()
assert out.strip() == expected
def test_composite_runs_all_hooks(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second"]},
"pre_test": "python echo.py Pre-Test",
"post_test": "python echo.py Post-Test",
"first": "python echo.py First",
"pre_first": "python echo.py Pre-First",
"second": "python echo.py Second",
"post_second": "python echo.py Post-Second",
"pre_script": "python echo.py Pre-Script",
"post_script": "python echo.py Post-Script",
"pre_run": "python echo.py Pre-Run",
"post_run": "python echo.py Post-Run",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
expected = dedent(
"""
Pre-Run CALLED
Pre-Script CALLED
Pre-Test CALLED
Pre-Script CALLED
Pre-First CALLED
First CALLED
Post-Script CALLED
Pre-Script CALLED
Second CALLED
Post-Second CALLED
Post-Script CALLED
Post-Test CALLED
Post-Script CALLED
Post-Run CALLED
"""
).strip()
assert out.strip() == expected
@pytest.mark.parametrize("option", [":all", ":pre,:post"])
def test_skip_all_hooks_option(project, pdm, capfd, option: str, _echo):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second"]},
"pre_test": "python echo.py Pre-Test",
"post_test": "python echo.py Post-Test",
"first": "python echo.py First",
"pre_first": "python echo.py Pre-First",
"post_first": "python echo.py Post-First",
"second": "python echo.py Second",
"pre_second": "python echo.py Pre-Second",
"post_second": "python echo.py Post-Second",
"pre_script": "python echo.py Pre-Script",
"post_script": "python echo.py Post-Script",
"pre_run": "python echo.py Pre-Run",
"post_run": "python echo.py Post-Run",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", f"--skip={option}", "first"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-First CALLED" not in out
assert "First CALLED" in out
assert "Post-First CALLED" not in out
assert "Pre-Script CALLED" not in out
assert "Post-Script CALLED" not in out
capfd.readouterr()
pdm(["run", f"--skip={option}", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Test CALLED" not in out
assert "Pre-First CALLED" not in out
assert "First CALLED" in out
assert "Post-First CALLED" not in out
assert "Pre-Second CALLED" not in out
assert "Second CALLED" in out
assert "Post-Second CALLED" not in out
assert "Post-Test CALLED" not in out
assert "Pre-Script CALLED" not in out
assert "Post-Script CALLED" not in out
assert "Pre-Run CALLED" not in out
assert "Post-Run CALLED" not in out
@pytest.mark.parametrize(
"args",
[
"--skip pre_test,post_first,second",
"-k pre_test,post_first,second",
"--skip pre_test --skip post_first --skip second",
"-k pre_test -k post_first -k second",
"--skip pre_test --skip post_first,second",
"-k pre_test -k post_first,second",
],
)
def test_skip_option(project, pdm, capfd, args, _echo):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second"]},
"pre_test": "python echo.py Pre-Test",
"post_test": "python echo.py Post-Test",
"first": "python echo.py First",
"pre_first": "python echo.py Pre-First",
"post_first": "python echo.py Post-First",
"second": "python echo.py Second",
"pre_second": "python echo.py Pre-Second",
"post_second": "python echo.py Post-Second",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", *shlex.split(args), "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Test CALLED" not in out
assert "Pre-First CALLED" in out
assert "First CALLED" in out
assert "Post-First CALLED" not in out
assert "Pre-Second CALLED" not in out
assert "Second CALLED" not in out
assert "Post-Second CALLED" not in out
assert "Post-Test CALLED" in out
@pytest.mark.parametrize(
"env, expected",
[
("pre_test", ["pre_test"]),
("pre_test,post_test", ["pre_test", "post_test"]),
("pre_test , post_test", ["pre_test", "post_test"]),
(None, None),
(" ", None),
(" , ", None),
],
)
def test_skip_option_default_from_env(env, expected, monkeypatch):
if env is not None:
monkeypatch.setenv("PDM_SKIP_HOOKS", env)
# Default value is set once and not easily testable
# so we test the function generating this default value
assert from_splitted_env("PDM_SKIP_HOOKS", ",") == expected
HookSpecs = namedtuple("HookSpecs", ["command", "hooks", "fixtures"])
this_python_version = f"{sys.version_info[0]}.{sys.version_info[1]}"
KNOWN_COMMAND_HOOKS = (
("add", "add requests", ("pre_lock", "post_lock"), ["working_set"]),
("build", "build", ("pre_build", "post_build"), []),
("init", "init --non-interactive", ("post_init",), []),
(
"install",
"install",
("pre_install", "post_install", "pre_lock", "post_lock"),
["repository"],
),
("lock", "lock", ("pre_lock", "post_lock"), []),
(
"publish",
"publish --username abc --password 123",
("pre_publish", "pre_build", "post_build", "post_publish"),
["mock_publish"],
),
("remove", "remove requests", ("pre_lock", "post_lock"), ["lock"]),
("sync", "sync", ("pre_install", "post_install"), ["lock"]),
("update", "update", ("pre_install", "post_install", "pre_lock", "post_lock"), []),
("use", f"use -f {this_python_version}", ("post_use",), []),
)
parametrize_with_commands = pytest.mark.parametrize(
"specs",
[pytest.param(HookSpecs(command, hooks, fixtures), id=id) for id, command, hooks, fixtures in KNOWN_COMMAND_HOOKS],
)
parametrize_with_hooks = pytest.mark.parametrize(
"specs,hook",
[
pytest.param(HookSpecs(command, hooks, fixtures), hook, id=f"{id}-{hook}")
for id, command, hooks, fixtures in KNOWN_COMMAND_HOOKS
for hook in hooks
],
)
@pytest.fixture
def hooked_project(project, capfd, specs, request):
project.pyproject.settings["scripts"] = {hook: f"python -c \"print('{hook} CALLED')\"" for hook in pdm_signals}
project.pyproject.write()
for fixture in specs.fixtures:
request.getfixturevalue(fixture)
capfd.readouterr()
return project
@pytest.fixture
def lock(project, capfd):
project.add_dependencies(["requests"])
actions.do_lock(project)
capfd.readouterr()
@parametrize_with_commands
def test_hooks(hooked_project, pdm, capfd, specs: HookSpecs):
pdm(shlex.split(specs.command), strict=True, obj=hooked_project)
out, _ = capfd.readouterr()
for hook in specs.hooks:
assert f"{hook} CALLED" in out
@parametrize_with_hooks # Iterate over hooks as we need a clean slate for each run
def test_skip_option_from_signal(hooked_project, pdm, capfd, specs: HookSpecs, hook: str):
pdm([*shlex.split(specs.command), f"--skip={hook}"], strict=True, obj=hooked_project)
out, _ = capfd.readouterr()
assert f"{hook} CALLED" not in out
for known_hook in specs.hooks:
if known_hook != hook:
assert f"{known_hook} CALLED" in out
@parametrize_with_commands
@pytest.mark.parametrize("option", [":all", ":pre,:post"])
def test_skip_all_option_from_signal(hooked_project, pdm, capfd, specs: HookSpecs, option: str):
pdm(
[*shlex.split(specs.command), f"--skip={option}"],
strict=True,
obj=hooked_project,
)
out, _ = capfd.readouterr()
for hook in pdm_signals:
assert f"{hook} CALLED" not in out
@parametrize_with_commands
@pytest.mark.parametrize("prefix", ["pre", "post"])
def test_skip_pre_post_option_from_signal(hooked_project, pdm, capfd, specs: HookSpecs, prefix: str):
pdm(
[*shlex.split(specs.command), f"--skip=:{prefix}"],
strict=True,
obj=hooked_project,
)
out, _ = capfd.readouterr()
for hook in specs.hooks:
if hook.startswith(prefix):
assert f"{hook} CALLED" not in out
else:
assert f"{hook} CALLED" in out
|