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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
|
import json
import os
import subprocess
import textwrap
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from pdm import termui
from pdm.cli import actions
from pdm.cli.utils import get_pep582_path
from pdm.utils import cd, path_to_url
@pytest.fixture
def _args(project):
(project.root / "args.py").write_text(
textwrap.dedent(
"""
import os
import sys
name = sys.argv[1]
args = ", ".join(sys.argv[2:])
print(f"{name} CALLED with {args}" if args else f"{name} CALLED")
"""
)
)
def test_pep582_launcher_for_python_interpreter(project, local_finder, pdm):
project.root.joinpath("main.py").write_text("import first;print(first.first([0, False, 1, 2]))\n")
result = pdm(["add", "first"], obj=project)
assert result.exit_code == 0, result.stderr
env = os.environ.copy()
env.update({"PYTHONPATH": get_pep582_path(project)})
output = subprocess.check_output(
[str(project.python.executable), str(project.root.joinpath("main.py"))],
env=env,
)
assert output.decode().strip() == "1"
def test_auto_isolate_site_packages(project, pdm):
env = os.environ.copy()
env.update({"PYTHONPATH": get_pep582_path(project)})
proc = subprocess.run(
[str(project.python.executable), "-c", "import sys;print(sys.path, sep='\\n')"],
env=env,
capture_output=True,
text=True,
cwd=str(project.root),
check=True,
)
assert any("site-packages" in path for path in proc.stdout.splitlines())
result = pdm(
["run", "python", "-c", "import sys;print(sys.path, sep='\\n')"],
obj=project,
strict=True,
)
assert not any("site-packages" in path for path in result.stdout.splitlines())
def test_run_with_site_packages(project, pdm):
project.pyproject.settings["scripts"] = {
"foo": {
"cmd": ["python", "-c", "import sys;print(sys.path, sep='\\n')"],
"site_packages": True,
}
}
project.pyproject.write()
result = pdm(
[
"run",
"--site-packages",
"python",
"-c",
"import sys;print(sys.path, sep='\\n')",
],
obj=project,
)
assert result.exit_code == 0
result = pdm(["run", "foo"], obj=project)
assert result.exit_code == 0
def test_run_command_not_found(pdm):
result = pdm(["run", "foobar"])
assert "Command 'foobar' is not found in your PATH." in result.stderr
assert result.exit_code == 1
def test_run_pass_exit_code(pdm):
result = pdm(["run", "python", "-c", "1/0"])
assert result.exit_code == 1
def test_run_cmd_script(project, pdm):
project.pyproject.settings["scripts"] = {"test_script": "python -V"}
project.pyproject.write()
result = pdm(["run", "test_script"], obj=project)
assert result.exit_code == 0
def test_run_cmd_script_with_array(project, pdm):
project.pyproject.settings["scripts"] = {"test_script": ["python", "-c", "import sys; sys.exit(22)"]}
project.pyproject.write()
result = pdm(["run", "test_script"], obj=project)
assert result.exit_code == 22
def test_run_script_pass_project_root(project, pdm, capfd):
project.pyproject.settings["scripts"] = {
"test_script": [
"python",
"-c",
"import os;print(os.getenv('PDM_PROJECT_ROOT'))",
]
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["run", "test_script"], obj=project)
assert result.exit_code == 0
out, _ = capfd.readouterr()
assert Path(out.strip()) == project.root
def test_run_shell_script(project, pdm):
project.pyproject.settings["scripts"] = {
"test_script": {
"shell": "echo hello > output.txt",
"help": "test it won't fail",
}
}
project.pyproject.write()
with cd(project.root):
result = pdm(["run", "test_script"], obj=project)
assert result.exit_code == 0
assert (project.root / "output.txt").read_text().strip() == "hello"
def test_run_script_with_relative_path(project, pdm, capfd):
if os.name == "nt":
(project.root / "test_script.bat").write_text("@echo Hello\n")
else:
(project.root / "test_script.sh").write_text("#!/bin/bash\necho Hello\n")
(project.root / "test_script.sh").chmod(0o755)
with cd(project.root):
pdm(["run", "./test_script.bat" if os.name == "nt" else "./test_script.sh"], obj=project, strict=True)
out, _ = capfd.readouterr()
assert out.strip() == "Hello"
def test_run_non_existing_local_script(project, pdm):
with cd(project.root):
result = pdm(["run", "./test_script.sh"], obj=project)
assert result.exit_code != 0
assert "not a valid executable" in result.stderr
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["hello"], "ok hello", id="with-args"),
pytest.param([], "ok", id="without-args"),
),
)
def test_run_shell_script_with_args_placeholder(project, pdm, args, expected):
project.pyproject.settings["scripts"] = {
"test_script": {
"shell": "echo ok {args} > output.txt",
"help": "test it won't fail",
}
}
project.pyproject.write()
with cd(project.root):
result = pdm(["run", "test_script", *args], obj=project)
assert result.exit_code == 0
assert (project.root / "output.txt").read_text().strip() == expected
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["hello"], "hello", id="with-args"),
pytest.param([], "default", id="with-default"),
),
)
def test_run_shell_script_with_args_placeholder_with_default(project, pdm, args, expected):
project.pyproject.settings["scripts"] = {
"test_script": {
"shell": "echo {args:default} > output.txt",
"help": "test it won't fail",
}
}
project.pyproject.write()
with cd(project.root):
result = pdm(["run", "test_script", *args], obj=project)
assert result.exit_code == 0
assert (project.root / "output.txt").read_text().strip() == expected
def test_run_call_script(project, pdm):
(project.root / "test_script.py").write_text(
textwrap.dedent(
"""
import argparse
import sys
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--code", type=int)
args = parser.parse_args(argv)
sys.exit(args.code)
"""
)
)
project.pyproject.settings["scripts"] = {
"test_script": {"call": "test_script:main"},
"test_script_with_args": {"call": "test_script:main(['-c', '9'])"},
}
project.pyproject.write()
with cd(project.root):
result = pdm(["run", "test_script", "-c", "8"], obj=project)
assert result.exit_code == 8
result = pdm(["run", "test_script_with_args"], obj=project)
assert result.exit_code == 9
def test_run_script_with_extra_args(project, pdm, capfd):
(project.root / "test_script.py").write_text(
textwrap.dedent(
"""
import sys
print(*sys.argv[1:], sep='\\n')
"""
)
)
project.pyproject.settings["scripts"] = {"test_script": "python test_script.py"}
project.pyproject.write()
with cd(project.root):
pdm(["run", "test_script", "-a", "-b", "-c"], obj=project)
out, _ = capfd.readouterr()
assert out.splitlines()[-3:] == ["-a", "-b", "-c"]
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["-a", "-b", "-c"], ["-a", "-b", "-c", "-x"], id="with-args"),
pytest.param([], ["-x"], id="without-args"),
),
)
@pytest.mark.parametrize(
"script",
(
pytest.param("python test_script.py {args} -x", id="as-str"),
pytest.param(["python", "test_script.py", "{args}", "-x"], id="as-list"),
),
)
def test_run_script_with_args_placeholder(project, pdm, capfd, script, args, expected):
(project.root / "test_script.py").write_text(
textwrap.dedent(
"""
import sys
print(*sys.argv[1:], sep='\\n')
"""
)
)
project.pyproject.settings["scripts"] = {"test_script": script}
project.pyproject.write()
with cd(project.root):
pdm(["run", "-v", "test_script", *args], obj=project)
out, _ = capfd.readouterr()
assert out.strip().splitlines()[1:] == expected
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["-a", "-b", "-c"], ["-a", "-b", "-c", "-x"], id="with-args"),
pytest.param([], ["--default", "--value", "-x"], id="default"),
),
)
@pytest.mark.parametrize(
"script",
(
pytest.param("python test_script.py {args:--default --value} -x", id="as-str"),
pytest.param(["python", "test_script.py", "{args:--default --value}", "-x"], id="as-list"),
),
)
def test_run_script_with_args_placeholder_with_default(project, pdm, capfd, script, args, expected):
(project.root / "test_script.py").write_text(
textwrap.dedent(
"""
import sys
print(*sys.argv[1:], sep='\\n')
"""
)
)
project.pyproject.settings["scripts"] = {"test_script": script}
project.pyproject.write()
with cd(project.root):
pdm(["run", "-v", "test_script", *args], obj=project)
out, _ = capfd.readouterr()
assert out.strip().splitlines()[1:] == expected
def test_run_shell_script_with_pdm_placeholder(project, pdm):
project.pyproject.settings["scripts"] = {
"test_script": {
"shell": "{pdm} -V > output.txt",
"help": "test it won't fail",
}
}
project.pyproject.write()
with cd(project.root):
result = pdm(["run", "test_script"], obj=project)
assert result.exit_code == 0
assert (project.root / "output.txt").read_text().strip().startswith("PDM, version")
def test_run_expand_env_vars(project, pdm, capfd, monkeypatch):
(project.root / "test_script.py").write_text("import os; print(os.getenv('FOO'))")
project.pyproject.settings["scripts"] = {
"test_cmd": 'python -c "foo, bar = 0, 1;print(${FOO})"',
"test_cmd_no_expand": "python -c 'print(${FOO})'",
"test_script": "python test_script.py",
"test_cmd_array": ["python", "test_script.py"],
"test_shell": {"shell": "echo %FOO%" if os.name == "nt" else "echo $FOO"},
}
project.pyproject.write()
capfd.readouterr()
with cd(project.root):
monkeypatch.setenv("FOO", "bar")
pdm(["run", "test_cmd"], obj=project)
assert capfd.readouterr()[0].strip() == "1"
result = pdm(["run", "test_cmd_no_expand"], obj=project)
assert result.exit_code == 1
pdm(["run", "test_script"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
pdm(["run", "test_cmd_array"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
pdm(["run", "test_shell"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
def test_run_expand_env_vars_from_config(project, pdm, capfd):
(project.root / "test_script.py").write_text("import os; print(os.getenv('FOO'))")
project.pyproject.settings["scripts"] = {
"test_cmd": 'python -c "foo, bar = 0, 1;print(${FOO})"',
"test_cmd_no_expand": "python -c 'print(${FOO})'",
"test_script": "python test_script.py",
"test_cmd_array": ["python", "test_script.py"],
"test_shell": {"shell": "echo %FOO%" if os.name == "nt" else "echo $FOO"},
"_": {"env": {"FOO": "bar"}},
}
project.pyproject.write()
capfd.readouterr()
with cd(project.root):
pdm(["run", "test_cmd"], obj=project)
assert capfd.readouterr()[0].strip() == "1"
result = pdm(["run", "test_cmd_no_expand"], obj=project)
assert result.exit_code == 1
pdm(["run", "test_script"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
pdm(["run", "test_cmd_array"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
pdm(["run", "test_shell"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
def test_run_script_with_env_defined(project, pdm, capfd):
(project.root / "test_script.py").write_text("import os; print(os.getenv('FOO'))")
project.pyproject.settings["scripts"] = {"test_script": {"cmd": "python test_script.py", "env": {"FOO": "bar"}}}
project.pyproject.write()
capfd.readouterr()
with cd(project.root):
pdm(["run", "test_script"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
def test_run_script_with_dotenv_file(project, pdm, capfd, monkeypatch):
(project.root / "test_script.py").write_text("import os; print(os.getenv('FOO'), os.getenv('BAR'))")
project.pyproject.settings["scripts"] = {
"test_override": {
"cmd": "python test_script.py",
"env_file": {"override": ".env"},
},
"test_default": {"cmd": "python test_script.py", "env_file": ".env"},
}
project.pyproject.write()
monkeypatch.setenv("BAR", "foo")
(project.root / ".env").write_text("FOO=bar\nBAR=override")
capfd.readouterr()
with cd(project.root):
pdm(["run", "test_default"], obj=project)
assert capfd.readouterr()[0].strip() == "bar foo"
pdm(["run", "test_override"], obj=project)
assert capfd.readouterr()[0].strip() == "bar override"
def test_run_script_override_global_env(project, pdm, capfd):
(project.root / "test_script.py").write_text("import os; print(os.getenv('FOO'))")
project.pyproject.settings["scripts"] = {
"_": {"env": {"FOO": "bar"}},
"test_env": {"cmd": "python test_script.py"},
"test_env_override": {"cmd": "python test_script.py", "env": {"FOO": "foobar"}},
}
project.pyproject.write()
capfd.readouterr()
with cd(project.root):
pdm(["run", "test_env"], obj=project)
assert capfd.readouterr()[0].strip() == "bar"
pdm(["run", "test_env_override"], obj=project)
assert capfd.readouterr()[0].strip() == "foobar"
def test_run_show_list_of_scripts(project, pdm):
project.pyproject.settings["scripts"] = {
"test_composite": {"composite": ["test_cmd", "test_script", "test_shell"]},
"test_cmd": "flask db upgrade",
"test_multi": """\
I am a multilines
command
""",
"test_script": {"call": "test_script:main", "help": "call a python function"},
"test_shell": {"shell": "echo $FOO", "help": "shell command"},
}
project.pyproject.write()
result = pdm(["run", "--list"], obj=project)
result_lines = result.output.splitlines()[3:]
assert result_lines[0][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade"
sep = termui.Emoji.ARROW_SEPARATOR
assert result_lines[1][1:-1].strip() == f"test_composite │ composite │ test_cmd {sep} test_script {sep} test_shell"
assert result_lines[2][1:-1].strip() == f"test_multi │ cmd │ I am a multilines{termui.Emoji.ELLIPSIS}"
assert result_lines[3][1:-1].strip() == "test_script │ call │ call a python function"
assert result_lines[4][1:-1].strip() == "test_shell │ shell │ shell command"
def test_run_show_list_of_scripts_hide_internals(project, pdm):
project.pyproject.settings["scripts"] = {
"public": "true",
"_internal": "true",
}
project.pyproject.write()
result = pdm(["run", "--list"], obj=project)
assert "public" in result.output
assert "_internal" not in result.output
def test_run_json_list_of_scripts(project, pdm):
project.pyproject.settings["scripts"] = {
"_": {"env_file": ".env"},
"test_composite": {"composite": ["test_cmd", "test_script", "test_shell"]},
"test_cmd": "flask db upgrade",
"test_multi": """\
I am a multilines
command
""",
"test_script": {"call": "test_script:main", "help": "call a python function"},
"test_shell": {"shell": "echo $FOO", "help": "shell command"},
"test_env": {"cmd": "true", "env": {"TEST": "value"}},
"test_env_file": {"cmd": "true", "env_file": ".env"},
"test_override": {"cmd": "true", "env_file": {"override": ".env"}},
"test_site_packages": {"cmd": "true", "site_packages": True},
"_private": "true",
}
project.pyproject.write()
result = pdm(["run", "--json"], obj=project, strict=True)
sep = termui.Emoji.ARROW_SEPARATOR
assert json.loads(result.outputs) == {
"_": {"name": "_", "help": "Shared options", "kind": "shared", "env_file": ".env"},
"test_cmd": {"name": "test_cmd", "help": "flask db upgrade", "kind": "cmd", "args": "flask db upgrade"},
"test_composite": {
"name": "test_composite",
"help": f"test_cmd {sep} test_script {sep} test_shell",
"kind": "composite",
"args": ["test_cmd", "test_script", "test_shell"],
},
"test_multi": {
"name": "test_multi",
"help": f"I am a multilines{termui.Emoji.ELLIPSIS}",
"kind": "cmd",
"args": " I am a multilines\n command\n ",
},
"test_script": {
"name": "test_script",
"help": "call a python function",
"kind": "call",
"args": "test_script:main",
},
"test_shell": {"name": "test_shell", "help": "shell command", "kind": "shell", "args": "echo $FOO"},
"test_env": {"name": "test_env", "help": "true", "kind": "cmd", "args": "true", "env": {"TEST": "value"}},
"test_env_file": {"name": "test_env_file", "help": "true", "kind": "cmd", "args": "true", "env_file": ".env"},
"test_override": {
"name": "test_override",
"help": "true",
"kind": "cmd",
"args": "true",
"env_file.override": ".env",
},
"test_site_packages": {
"name": "test_site_packages",
"help": "true",
"kind": "cmd",
"args": "true",
"site_packages": True,
},
"_private": {
"name": "_private",
"help": "true",
"kind": "cmd",
"args": "true",
},
}
@pytest.mark.usefixtures("local_finder")
@pytest.mark.parametrize("explicit_python", [True, False])
def test_run_with_another_project_root(project, pdm, capfd, explicit_python):
project.pyproject.metadata["requires-python"] = ">=3.6"
project.pyproject.write()
pdm(["add", "first"], obj=project)
with TemporaryDirectory(prefix="pytest-run-") as tmp_dir:
Path(tmp_dir).joinpath("main.py").write_text("import first;print(first.first([0, False, 1, 2]))\n")
capfd.readouterr()
with cd(tmp_dir):
args = ["run", "-p", str(project.root), "main.py"]
if explicit_python:
args.insert(len(args) - 1, "python")
ret = pdm(args)
out, err = capfd.readouterr()
assert ret.exit_code == 0, err
assert out.strip() == "1"
def test_import_another_sitecustomize(project, pdm, capfd):
project.pyproject.metadata["requires-python"] = ">=2.7"
project.pyproject.write()
# a script for checking another sitecustomize is imported
project.root.joinpath("foo.py").write_text("import os;print(os.getenv('FOO'))")
# ensure there have at least one sitecustomize can be imported
# there may have more than one sitecustomize.py in sys.path
project.root.joinpath("sitecustomize.py").write_text("import os;os.environ['FOO'] = 'foo'")
env = os.environ.copy()
paths = env.get("PYTHONPATH")
this_path = str(project.root)
new_paths = [this_path] if not paths else [this_path, paths]
env["PYTHONPATH"] = os.pathsep.join(new_paths)
project._environment = None
capfd.readouterr()
with cd(project.root):
result = pdm(["run", "python", "foo.py"], env=env)
assert result.exit_code == 0, result.stderr
out, _ = capfd.readouterr()
assert out.strip() == "foo"
def test_run_with_patched_sysconfig(project, pdm, capfd):
project.root.joinpath("script.py").write_text(
"""\
import sysconfig
import json
print(json.dumps(sysconfig.get_paths()))
"""
)
capfd.readouterr()
with cd(project.root):
result = pdm(["run", "python", "script.py"], obj=project)
assert result.exit_code == 0
out = json.loads(capfd.readouterr()[0])
assert "__pypackages__" in out["purelib"]
def test_run_composite(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"first": "python echo.py First",
"second": "python echo.py Second",
"test": {"composite": ["first", "second"]},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "First CALLED" in out
assert "Second CALLED" in out
def test_composite_stops_on_first_failure(project, pdm, capfd):
project.pyproject.settings["scripts"] = {
"first": {"cmd": ["python", "-c", "print('First CALLED')"]},
"fail": "python -c 'raise Exception'",
"second": "echo 'Second CALLED'",
"test": {"composite": ["first", "fail", "second"]},
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["run", "test"], obj=project)
assert result.exit_code == 1
out, _ = capfd.readouterr()
assert "First CALLED" in out
assert "Second CALLED" not in out
def test_composite_keep_going_on_failure(project, pdm, capfd):
project.pyproject.settings["scripts"] = {
"first": {"cmd": ["python", "-c", "print('First CALLED')"]},
"fail": "python -c 'raise Exception'",
"second": "echo 'Second CALLED'",
"test": {"composite": ["first", "fail", "second"], "keep_going": True},
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["run", "test"], obj=project)
assert result.exit_code == 1
out, err = capfd.readouterr()
assert "First CALLED" in out
assert "Second CALLED" in out
def test_composite_inherit_env(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"first": {
"cmd": "python echo.py First VAR",
"env": {"VAR": "42"},
},
"second": {
"cmd": "python echo.py Second VAR",
"env": {"VAR": "42"},
},
"nested": {
"composite": ["third"],
"env": {"VAR": "42"},
},
"third": {
"cmd": "python echo.py Third VAR",
"env": {"VAR": "42"},
},
"test": {"composite": ["first", "second", "nested"], "env": {"VAR": "overriden"}},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "First CALLED with VAR=overriden" in out
assert "Second CALLED with VAR=overriden" in out
assert "Third CALLED with VAR=overriden" in out
def test_composite_fail_on_first_missing_task(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"first": "python echo.py First",
"second": "python echo.py Second",
"test": {"composite": ["first", "fail", "second"]},
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["run", "test"], obj=project)
assert result.exit_code == 1
out, _ = capfd.readouterr()
assert "First CALLED" in out
assert "Second CALLED" not in out
def test_composite_fails_on_recursive_script(project, pdm):
project.pyproject.settings["scripts"] = {
"first": {"composite": ["first"]},
"second": {"composite": ["third"]},
"third": {"composite": ["second"]},
"fourth": {"composite": ["python -V", "python -V"]},
"fifth": {"composite": ["fourth", "fourth"]},
}
project.pyproject.write()
result = pdm(["run", "first"], obj=project)
assert result.exit_code == 1
assert "Script first is recursive" in result.stderr
result = pdm(["run", "second"], obj=project)
assert result.exit_code == 1
assert "Script second is recursive" in result.stderr
result = pdm(["run", "fourth"], obj=project)
assert result.exit_code == 0
result = pdm(["run", "fifth"], obj=project)
assert result.exit_code == 0
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",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Test CALLED" in out
assert "Pre-First CALLED" in out
assert "First CALLED" in out
assert "Second CALLED" in out
assert "Post-Second CALLED" in out
assert "Post-Test CALLED" in out
def test_composite_pass_parameters_to_subtasks(project, pdm, capfd, _args):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second"]},
"pre_test": "python args.py Pre-Test",
"post_test": "python args.py Post-Test",
"first": "python args.py First",
"pre_first": "python args.py Pre-First",
"second": "python args.py Second",
"post_second": "python args.py Post-Second",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test", "param=value"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Test CALLED" in out
assert "Pre-First CALLED" in out
assert "First CALLED with param=value" in out
assert "Second CALLED with param=value" in out
assert "Post-Second CALLED" in out
assert "Post-Test CALLED" in out
def test_composite_can_pass_parameters(project, pdm, capfd, _args):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first param=first", "second param=second"]},
"pre_test": "python args.py Pre-Test",
"post_test": "python args.py Post-Test",
"first": "python args.py First",
"pre_first": "python args.py Pre-First",
"second": "python args.py Second",
"post_second": "python args.py Post-Second",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Test CALLED" in out
assert "Pre-First CALLED" in out
assert "First CALLED with param=first" in out
assert "Second CALLED with param=second" in out
assert "Post-Second CALLED" in out
assert "Post-Test CALLED" in out
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["-a"], "-a, ", id="with-args"),
pytest.param([], "", id="without-args"),
),
)
def test_composite_only_pass_parameters_to_subtasks_with_args(project, pdm, capfd, _args, args, expected):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second {args} key=value"]},
"first": "python args.py First",
"second": "python args.py Second",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "-v", "test", *args], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "First CALLED" in out
assert f"Second CALLED with {expected}key=value" in out
@pytest.mark.parametrize(
"args,expected",
(
pytest.param(["-a"], "-a", id="with-args"),
pytest.param([], "--default", id="default"),
),
)
def test_composite_only_pass_parameters_to_subtasks_with_args_with_default(project, pdm, capfd, _args, args, expected):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second {args:--default} key=value"]},
"first": "python args.py First",
"second": "python args.py Second",
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "-v", "test", *args], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "First CALLED" in out
assert f"Second CALLED with {expected}, key=value" in out
def test_composite_hooks_inherit_env(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"pre_task": {"cmd": "python echo.py Pre-Task VAR", "env": {"VAR": "42"}},
"task": "python echo.py Task",
"post_task": {"cmd": "python echo.py Post-Task VAR", "env": {"VAR": "42"}},
"test": {"composite": ["task"], "env": {"VAR": "overriden"}},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Task CALLED with VAR=overriden" in out
assert "Task CALLED" in out
assert "Post-Task CALLED with VAR=overriden" in out
def test_composite_inherit_env_in_cascade(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"_": {"env": {"FOO": "BAR", "TIK": "TOK"}},
"pre_task": {
"cmd": "python echo.py Pre-Task VAR FOO TIK",
"env": {"VAR": "42", "FOO": "foobar"},
},
"task": {
"cmd": "python echo.py Task VAR FOO TIK",
"env": {"VAR": "42", "FOO": "foobar"},
},
"post_task": {
"cmd": "python echo.py Post-Task VAR FOO TIK",
"env": {"VAR": "42", "FOO": "foobar"},
},
"test": {"composite": ["task"], "env": {"VAR": "overriden"}},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Task CALLED with VAR=overriden FOO=foobar TIK=TOK" in out
assert "Task CALLED with VAR=overriden FOO=foobar TIK=TOK" in out
assert "Post-Task CALLED with VAR=overriden FOO=foobar TIK=TOK" in out
def test_composite_inherit_dotfile(project, pdm, capfd, _echo):
(project.root / ".env").write_text("VAR=42")
(project.root / "override.env").write_text("VAR=overriden")
project.pyproject.settings["scripts"] = {
"pre_task": {"cmd": "python echo.py Pre-Task VAR", "env_file": ".env"},
"task": {"cmd": "python echo.py Task VAR", "env_file": ".env"},
"post_task": {"cmd": "python echo.py Post-Task VAR", "env_file": ".env"},
"test": {"composite": ["task"], "env_file": "override.env"},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Pre-Task CALLED with VAR=overriden" in out
assert "Task CALLED with VAR=overriden" in out
assert "Post-Task CALLED with VAR=overriden" in out
def test_resolve_env_vars_in_dotfile(project, pdm, capfd, _echo):
(project.root / ".env").write_text("VAR=42\nFOO=${OUT}/${VAR}")
project.pyproject.settings["scripts"] = {
"_": {"env_file": ".env"},
"test": {"cmd": "python echo.py Task FOO BAR", "env": {"BAR": "${FOO}/bar"}},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test"], strict=True, obj=project, env={"OUT": "hello"})
out, _ = capfd.readouterr()
assert "Task CALLED with FOO=hello/42 BAR=hello/42/bar" in out
def test_composite_can_have_commands(project, pdm, capfd):
project.pyproject.settings["scripts"] = {
"task": {"cmd": ["python", "-c", 'print("Task CALLED")']},
"test": {"composite": ["task", "python -c 'print(\"Command CALLED\")'"]},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "-v", "test"], strict=True, obj=project)
out, _ = capfd.readouterr()
assert "Task CALLED" in out
assert "Command CALLED" in out
def test_run_shortcut(project, pdm, capfd):
project.pyproject.settings["scripts"] = {
"test": "echo 'Everything is fine'",
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["test"], obj=project, strict=True)
assert result.exit_code == 0
out, _ = capfd.readouterr()
assert "Everything is fine" in out
def test_run_shortcuts_dont_override_commands(project, pdm, capfd, mocker):
do_lock = mocker.patch.object(actions, "do_lock")
do_sync = mocker.patch.object(actions, "do_sync")
project.pyproject.settings["scripts"] = {
"install": "echo 'Should not run'",
}
project.pyproject.write()
capfd.readouterr()
result = pdm(["install"], obj=project, strict=True)
assert result.exit_code == 0
out, _ = capfd.readouterr()
assert "Should not run" not in out
do_lock.assert_called_once()
do_sync.assert_called_once()
def test_run_shortcut_fail_with_usage_if_script_not_found(project, pdm):
result = pdm(["whatever"], obj=project)
assert result.exit_code != 0
assert "Script unknown: whatever" in result.stderr
assert "Usage" in result.stderr
@pytest.mark.parametrize(
"args",
[
pytest.param(["-ko"], id="unknown param"),
pytest.param(["pip", "--version"], id="not an user script"),
],
)
def test_empty_positionnal_args_still_display_usage(project, pdm, args):
result = pdm(args, obj=project)
assert result.exit_code != 0
assert "Usage" in result.stderr
def test_empty_positional_args_display_help(project, pdm):
result = pdm([], obj=project)
assert result.exit_code == 0
assert "Usage:" in result.output
assert "Commands:" in result.output
assert "Options:" in result.output
def test_run_script_changing_working_dir(project, pdm, capfd):
project.root.joinpath("subdir").mkdir()
project.root.joinpath("subdir", "file.text").write_text("Hello world\n")
project.pyproject.settings["scripts"] = {
"test_script": {"working_dir": "subdir", "cmd": "cat file.text"},
}
project.pyproject.write()
capfd.readouterr()
pdm(["run", "test_script"], obj=project, strict=True)
assert capfd.readouterr()[0].strip() == "Hello world"
def test_run_script_with_inline_metadata(project, pdm, local_finder, local_finder_artifacts):
with cd(project.root):
project.root.joinpath("test_script.py").write_text(
textwrap.dedent("""\
from first import first
assert first([0, False, 1, 2]) == 1
""")
)
result = pdm(["run", "test_script.py"], obj=project)
assert result.exit_code != 0
local_artifacts_url = path_to_url(str(local_finder_artifacts))
project.root.joinpath("test_script.py").write_text(
textwrap.dedent(f"""\
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "first",
# ]
#
# [[tool.pdm.source]]
# name = "pypi"
# url = "{local_artifacts_url}"
# type = "find_links"
# ///
from first import first
assert first([0, False, 1, 2]) == 1
""")
)
with cd(project.root):
result = pdm(["run", "test_script.py"], obj=project)
assert result.exit_code == 0
|