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 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
|
from __future__ import annotations
import shutil
from collections import defaultdict
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import cast
import pytest
from packaging.utils import canonicalize_name
from poetry.core.constraints.version import parse_constraint
from poetry.core.factory import Factory
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import MAIN_GROUP
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.vcs_dependency import VCSDependency
from poetry.core.pyproject.tables import BuildSystem
from poetry.core.utils._compat import tomllib
from poetry.core.version.markers import SingleMarker
if TYPE_CHECKING:
from pytest import LogCaptureFixture
fixtures_dir = Path(__file__).parent / "fixtures"
@pytest.fixture
def complete_legacy_warnings() -> list[str]:
return [
"[tool.poetry.name] is deprecated. Use [project.name] instead.",
(
"[tool.poetry.version] is set but 'version' is not in "
"[project.dynamic]. If it is static use [project.version]. If it "
"is dynamic, add 'version' to [project.dynamic].\n"
"If you want to set the version dynamically via `poetry build "
"--local-version` or you are using a plugin, which sets the "
"version dynamically, you should define the version in "
"[tool.poetry] and add 'version' to [project.dynamic]."
),
"[tool.poetry.description] is deprecated. Use [project.description] instead.",
(
"[tool.poetry.readme] is set but 'readme' is not in "
"[project.dynamic]. If it is static use [project.readme]. If it "
"is dynamic, add 'readme' to [project.dynamic].\n"
"If you want to define multiple readmes, you should define them "
"in [tool.poetry] and add 'readme' to [project.dynamic]."
),
"[tool.poetry.license] is deprecated. Use [project.license] instead.",
"[tool.poetry.authors] is deprecated. Use [project.authors] instead.",
"[tool.poetry.maintainers] is deprecated. Use [project.maintainers] instead.",
"[tool.poetry.keywords] is deprecated. Use [project.keywords] instead.",
(
"[tool.poetry.classifiers] is set but 'classifiers' is not in "
"[project.dynamic]. If it is static use [project.classifiers]. If it "
"is dynamic, add 'classifiers' to [project.dynamic].\n"
"ATTENTION: Per default Poetry determines classifiers for "
"supported Python versions and license automatically. If you "
"define classifiers in [project], you disable the automatic "
"enrichment. In other words, you have to define all classifiers "
"manually. If you want to use Poetry's automatic enrichment of "
"classifiers, you should define them in [tool.poetry] and add "
"'classifiers' to [project.dynamic]."
),
"[tool.poetry.homepage] is deprecated. Use [project.urls] instead.",
"[tool.poetry.repository] is deprecated. Use [project.urls] instead.",
"[tool.poetry.documentation] is deprecated. Use [project.urls] instead.",
"[tool.poetry.plugins] is deprecated. Use [project.entry-points] instead.",
(
"[tool.poetry.extras] is deprecated. Use "
"[project.optional-dependencies] instead."
),
(
"Defining console scripts in [tool.poetry.scripts] is deprecated. "
"Use [project.scripts] instead. "
"([tool.poetry.scripts] should only be used for scripts of type 'file')."
),
]
@pytest.fixture
def complete_legacy_duplicate_warnings() -> list[str]:
return [
(
"[project.name] and [tool.poetry.name] are both set. The latter "
"will be ignored."
),
(
"[project.version] and [tool.poetry.version] are both set. The "
"latter will be ignored.\n"
"If you want to set the version dynamically via `poetry build "
"--local-version` or you are using a plugin, which sets the "
"version dynamically, you should define the version in "
"[tool.poetry] and add 'version' to [project.dynamic]."
),
(
"[project.description] and [tool.poetry.description] are both "
"set. The latter will be ignored."
),
(
"[project.readme] and [tool.poetry.readme] are both set. The "
"latter will be ignored.\n"
"If you want to define multiple readmes, you should define them "
"in [tool.poetry] and add 'readme' to [project.dynamic]."
),
(
"[project.license] and [tool.poetry.license] are both set. The "
"latter will be ignored."
),
(
"[project.authors] and [tool.poetry.authors] are both set. The "
"latter will be ignored."
),
(
"[project.maintainers] and [tool.poetry.maintainers] are both "
"set. The latter will be ignored."
),
(
"[project.keywords] and [tool.poetry.keywords] are both set. The "
"latter will be ignored."
),
(
"[project.classifiers] and [tool.poetry.classifiers] are both "
"set. The latter will be ignored.\n"
"ATTENTION: Per default Poetry determines classifiers for "
"supported Python versions and license automatically. If you "
"define classifiers in [project], you disable the automatic "
"enrichment. In other words, you have to define all classifiers "
"manually. If you want to use Poetry's automatic enrichment of "
"classifiers, you should define them in [tool.poetry] and add "
"'classifiers' to [project.dynamic]."
),
(
"[project.urls] and [tool.poetry.homepage] are both set. The "
"latter will be ignored."
),
(
"[project.urls] and [tool.poetry.repository] are both set. The "
"latter will be ignored."
),
(
"[project.urls] and [tool.poetry.documentation] are both set. "
"The latter will be ignored."
),
(
"[project.entry-points] and [tool.poetry.plugins] are both set. The "
"latter will be ignored."
),
(
"[project.optional-dependencies] and [tool.poetry.extras] are "
"both set. The latter will be ignored."
),
(
"[project.scripts] is set and there are console scripts "
"in [tool.poetry.scripts]. The latter will be ignored."
),
]
@pytest.mark.parametrize(
"project", ["sample_project", "sample_project_new", "sample_project_dynamic"]
)
def test_create_poetry(project: str) -> None:
new_format = project == "sample_project_new"
dynamic = project == "sample_project_dynamic"
poetry = Factory().create_poetry(fixtures_dir / project)
assert poetry.is_package_mode
package = poetry.package
assert package.name == "my-package"
assert package.version.text == "1.2.3"
assert package.description == "Some description."
assert package.authors == ["Sébastien Eustace <sebastien@eustace.io>"]
assert package.maintainers == ["Sébastien Eustace <sebastien@eustace.io>"]
if new_format:
assert package.license is None
assert package.license_expression == "MIT"
else:
assert package.license is not None
assert package.license.id == "MIT"
assert package.license_expression is None
assert (
package.readmes[0].relative_to(fixtures_dir).as_posix()
== f"{project}/README.rst"
)
assert package.homepage == "https://python-poetry.org"
assert package.repository_url == "https://github.com/python-poetry/poetry"
assert package.keywords == ["packaging", "dependency", "poetry"]
assert package.python_versions == ">=3.6"
assert str(package.python_constraint) == ">=3.6"
dependencies: dict[str, Dependency] = {}
for dep in package.requires:
dependencies[dep.name] = dep
cleo = dependencies["cleo"]
assert cleo.pretty_constraint == (">=0.6,<1.0" if new_format else "^0.6")
assert not cleo.is_optional()
pendulum = dependencies["pendulum"]
assert pendulum.pretty_constraint == ("rev 2.0" if new_format else "branch 2.0")
assert pendulum.is_vcs()
pendulum = cast("VCSDependency", pendulum)
assert pendulum.vcs == "git"
assert pendulum.rev == "2.0" if new_format else pendulum.branch == "2.0"
assert pendulum.source == "https://github.com/sdispater/pendulum.git"
assert pendulum.allows_prereleases()
assert not pendulum.develop
tomlkit = dependencies["tomlkit"]
assert tomlkit.pretty_constraint == "rev 3bff550"
assert tomlkit.is_vcs()
tomlkit = cast("VCSDependency", tomlkit)
assert tomlkit.vcs == "git"
assert tomlkit.rev == "3bff550"
assert tomlkit.source == "https://github.com/sdispater/tomlkit.git"
assert tomlkit.allows_prereleases()
assert not tomlkit.develop if new_format else tomlkit.develop
tomlkit_for_locking = next(d for d in package.all_requires if d.name == "tomlkit")
assert isinstance(tomlkit_for_locking, VCSDependency)
assert tomlkit_for_locking.develop
requests = dependencies["requests"]
assert requests.pretty_constraint == (
">=2.18,<3.0" if new_format or dynamic else "^2.18"
)
assert not requests.is_vcs()
assert requests.allows_prereleases() is None
assert requests.is_optional()
assert requests.extras == frozenset({"security"})
pathlib2 = dependencies["pathlib2"]
assert pathlib2.pretty_constraint == (">=2.2,<3.0" if new_format else "^2.2")
assert pathlib2.python_versions in {"~2.7", ">=2.7 <2.8"}
assert not pathlib2.is_optional()
demo = dependencies["demo"]
assert demo.is_file()
assert not demo.is_vcs()
assert demo.name == "demo"
assert demo.pretty_constraint == "*"
demo = dependencies["my-package"]
assert not demo.is_file()
assert demo.is_directory()
assert not demo.is_vcs()
assert demo.name == "my-package"
assert demo.pretty_constraint == "*"
simple_project = dependencies["simple-project"]
assert not simple_project.is_file()
assert simple_project.is_directory()
assert not simple_project.is_vcs()
assert simple_project.name == "simple-project"
assert simple_project.pretty_constraint == "*"
functools32 = dependencies["functools32"]
assert functools32.name == "functools32"
assert functools32.pretty_constraint == (
">=3.2.3,<3.3.0" if new_format else "^3.2.3"
)
assert (
str(functools32.marker)
== 'python_version ~= "2.7" and sys_platform == "win32" or python_version in'
' "3.4 3.5"'
)
dataclasses = dependencies["dataclasses"]
assert dataclasses.name == "dataclasses"
assert dataclasses.pretty_constraint == (">=0.7,<1.0" if new_format else "^0.7")
assert dataclasses.python_versions == ">=3.6.1 <3.7"
assert (
str(dataclasses.marker)
== 'python_full_version >= "3.6.1" and python_version < "3.7"'
)
assert "db" in package.extras
classifiers = package.classifiers
assert classifiers == [
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules",
]
if new_format:
assert package.all_classifiers == package.classifiers
else:
assert package.all_classifiers == [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules",
]
def test_create_poetry_with_empty_dependencies() -> None:
project = "sample_project_new_no_deps"
poetry = Factory().create_poetry(fixtures_dir / project)
assert poetry.is_package_mode
package = poetry.package
assert "main" in package._dependency_groups
assert package._dependency_groups[canonicalize_name("main")].dependencies == []
@pytest.mark.parametrize(
"project", ["sample_project_with_groups", "sample_project_with_groups_new"]
)
def test_create_poetry_with_groups(project: str) -> None:
poetry = Factory().create_poetry(fixtures_dir / project)
assert "docs" in poetry.package._dependency_groups
assert "test" in poetry.package._dependency_groups
assert poetry.package._dependency_groups["docs"].is_optional() # type: ignore[index]
assert not poetry.package._dependency_groups["test"].is_optional() # type: ignore[index]
package = poetry.package
expected_dependencies = {
"test": ["pytest", "coverage"],
"dev": ["pre-commit", "pytest", "coverage"],
"docs": ["mkdocs"],
"all": ["pytest", "coverage", "pre-commit", "pytest", "coverage", "mkdocs"],
}
dependencies = defaultdict(list)
for dep in package.all_requires:
assert len(dep.groups) == 1
for group in dep.groups:
if group != MAIN_GROUP:
dependencies[group].append(dep.name)
assert dependencies == expected_dependencies
for dep in package.all_requires:
if dep.name == "mkdocs":
assert isinstance(dep, VCSDependency)
# The "develop" flag of mkdocs from tool.poetry
# must also be set for the all extra!
assert dep.develop is True
assert dep.source_type == "git"
assert dep.source == "https://github.com/mkdocs/mkdocs.git"
def test_create_poetry_with_dependencies_with_subdirectory() -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_dependencies_with_subdirectory"
)
package = poetry.package
dependencies = {str(dep.name): dep for dep in package.requires}
# git dependency
pendulum = dependencies["pendulum"]
assert pendulum.is_vcs()
assert pendulum.pretty_constraint == "branch 2.0"
pendulum = cast("VCSDependency", pendulum)
assert pendulum.source == "https://github.com/sdispater/pendulum.git"
assert pendulum.directory == "sub"
# file dependency
demo = dependencies["demo"]
assert demo.is_file()
assert demo.pretty_constraint == "*"
demo = cast("FileDependency", demo)
assert demo.path == Path("../distributions/demo-0.1.0-in-subdir.zip")
assert demo.directory == "sub"
demo_dependencies = [dep for dep in package.requires if dep.name == "demo"]
assert len(demo_dependencies) == 2
assert demo_dependencies[0] == demo_dependencies[1]
assert {str(dep.marker) for dep in demo_dependencies} == {
'sys_platform == "win32"',
'sys_platform == "linux"',
}
# directory dependency
simple_project = dependencies["simple-project"]
assert simple_project.is_directory()
assert simple_project.pretty_constraint == "*"
simple_project = cast("DirectoryDependency", simple_project)
assert simple_project.path == Path("../simple_project")
with pytest.raises(AttributeError):
_ = simple_project.directory # type: ignore[attr-defined]
# url dependency
foo = dependencies["foo"]
assert foo.is_url()
assert foo.pretty_constraint == "*"
foo = cast("URLDependency", foo)
assert foo.url == "https://example.com/foo.zip"
assert foo.directory == "sub"
def test_create_poetry_with_packages_and_includes() -> None:
poetry = Factory().create_poetry(
fixtures_dir.parent / "masonry" / "builders" / "fixtures" / "with-include"
)
package = poetry.package
assert package.packages == [
{"include": "extra_dir/**/*.py", "format": ["sdist", "wheel"]},
{"include": "extra_dir/**/*.py", "format": ["sdist", "wheel"]},
{"include": "my_module.py", "format": ["sdist", "wheel"]},
{"include": "package_with_include", "format": ["sdist", "wheel"]},
{"include": "tests", "format": ["sdist"]},
{"include": "for_wheel_only", "format": ["wheel"]},
{"include": "src_package", "from": "src", "format": ["sdist", "wheel"]},
{
"include": "from_to",
"from": "etc",
"to": "target_from_to",
"format": ["sdist", "wheel"],
},
{
"include": "my_module_to.py",
"to": "target_module",
"format": ["sdist", "wheel"],
},
]
assert package.include == [
{"path": "extra_dir/vcs_excluded.py", "format": ["sdist", "wheel"]},
{"path": "notes.txt", "format": ["sdist"]},
]
def test_create_poetry_with_multi_constraints_dependency() -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_multi_constraints_dependency"
)
package = poetry.package
assert len(package.requires) == 2
def test_create_poetry_non_package_mode() -> None:
poetry = Factory().create_poetry(fixtures_dir / "non_package_mode")
assert not poetry.is_package_mode
@pytest.mark.parametrize(
"project", ["none", "file", "text", "text_spdx", "str", "str_empty", "str_no_spdx"]
)
@pytest.mark.parametrize("with_license_files", [False, True, "empty"])
def test_create_poetry_with_license_type(
project: str, with_license_files: bool | str, tmp_path: Path
) -> None:
project_dir = fixtures_dir / f"with_license_type_{project}"
expected_license_files: tuple[str, ...] | Path | None = None
if with_license_files:
if with_license_files == "empty":
content = ""
expected_license_files = ()
else:
content = '"LICEN[CS]E*", "AUTHORS*"'
expected_license_files = ("LICEN[CS]E*", "AUTHORS*")
orig_project_dir = project_dir
project_dir = tmp_path / project
shutil.copytree(orig_project_dir, project_dir)
pyproject_file = project_dir / "pyproject.toml"
new_lines = []
for line in pyproject_file.read_text(encoding="utf-8").splitlines():
if line.startswith("keywords = "):
new_lines.append(f"license-files = [{content}]")
new_lines.append(line)
pyproject_file.write_text("\n".join(new_lines), encoding="utf-8")
license_type = project.split("_", 1)[0]
expected_license_id: str | None = None
expected_license_expression: str | None = None
if license_type == "none":
pass
elif license_type == "file":
expected_license_id = (project_dir / "LICENSE").read_text(encoding="utf-8")
expected_license_files = Path("LICENSE")
elif license_type in {"str", "text"}:
with (project_dir / "pyproject.toml").open("rb") as f:
data = tomllib.load(f)
project_license = data["project"]["license"]
if license_type == "text":
expected_license_id = project_license["text"]
elif project == "str_no_spdx":
expected_license_id = project_license
elif project == "str":
expected_license_expression = project_license
else:
raise RuntimeError("unexpected license type")
if with_license_files and license_type in {"file", "text"}:
with pytest.raises(ValueError) as e:
Factory().create_poetry(project_dir)
assert str(e.value) == (
"[project.license] must be of type string"
" if [project.license-files] is defined."
)
else:
poetry = Factory().create_poetry(project_dir)
if expected_license_id is None:
assert poetry.package.license is None
else:
assert poetry.package.license is not None
assert poetry.package.license.id == expected_license_id
assert poetry.package.license_expression == expected_license_expression
assert poetry.package.license_files == expected_license_files
@pytest.mark.parametrize(
("invalid_glob", "expected_message"),
[
(
r"sub\\LICENSE",
(
"Invalid entry in [project.license-files]: 'sub\\LICENSE'"
" (Path delimiters must be forward slashes.)"
),
),
(
"../LICENSE",
(
"Invalid entry in [project.license-files]: '../LICENSE'"
" ('..' must not be used.)"
),
),
(
"./../LICENSE",
(
"Invalid entry in [project.license-files]: './../LICENSE'"
" ('..' must not be used.)"
),
),
(
"sub/../../LICENSE",
(
"Invalid entry in [project.license-files]: 'sub/../../LICENSE'"
" ('..' must not be used.)"
),
),
],
)
def test_create_poetry_with_invalid_license_files_glob(
tmp_path: Path, invalid_glob: str, expected_message: str
) -> None:
project_file = tmp_path / "pyproject.toml"
project_file.write_text(
f"""\
[project]
name = "foo"
version = "1"
license-files = [
"LICENSE",
"{invalid_glob}",
"licenses/**",
]
""",
encoding="utf-8",
)
with pytest.raises(ValueError) as e:
Factory().create_poetry(tmp_path)
assert str(e.value) == expected_message
def test_create_poetry_fails_with_missing_license_file() -> None:
project_dir = fixtures_dir / "missing_license_file"
with pytest.raises(FileNotFoundError) as e:
Factory().create_poetry(project_dir)
assert str((project_dir / "LICENSE").absolute()) in str(e.value)
@pytest.mark.parametrize(
("requires_python", "python", "expected_versions", "expected_constraint"),
[
(">=3.8", None, ">=3.8", ">=3.8"),
(None, "^3.8", "^3.8", ">=3.8,<4.0"),
(">=3.8", "^3.8", "^3.8", ">=3.8,<4.0"),
],
)
def test_create_poetry_python_version(
requires_python: str,
python: str,
expected_versions: str,
expected_constraint: str,
tmp_path: Path,
) -> None:
content = '[project]\nname = "foo"\nversion = "1"\n'
if requires_python:
content += f'requires-python = "{requires_python}"\n'
if python:
content += f'[tool.poetry.dependencies]\npython = "{python}"\n'
(tmp_path / "pyproject.toml").write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(tmp_path)
package = poetry.package
assert package.requires_python == requires_python or python
assert package.python_versions == expected_versions
assert str(package.python_constraint) == expected_constraint
def test_create_poetry_python_version_not_compatible(tmp_path: Path) -> None:
content = """
[project]
name = "foo"
version = "1"
requires-python = ">=3.8"
[tool.poetry.dependencies]
python = ">=3.7"
"""
(tmp_path / "pyproject.toml").write_text(content, encoding="utf-8")
with pytest.raises(ValueError) as e:
Factory().create_poetry(tmp_path)
assert "not a subset" in str(e.value)
@pytest.mark.parametrize(
("content", "expected"),
[
( # static
"""\
[project]
name = "foo"
version = "1"
requires-python = "3.10"
classifiers = ["License :: OSI Approved :: MIT License"]
""",
["License :: OSI Approved :: MIT License"],
),
( # dynamic
"""\
[project]
name = "foo"
version = "1"
requires-python = "3.10"
dynamic = [ "classifiers" ]
[tool.poetry]
classifiers = ["License :: OSI Approved :: MIT License"]
""",
[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
],
),
( # legacy
"""\
[tool.poetry]
name = "foo"
version = "1"
classifiers = ["License :: OSI Approved :: MIT License"]
[tool.poetry.dependencies]
python = "~3.10"
""",
[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
],
),
],
)
def test_create_poetry_classifiers(
content: str, expected: list[str], tmp_path: Path
) -> None:
(tmp_path / "pyproject.toml").write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(tmp_path)
assert poetry.package.all_classifiers == expected
def test_create_poetry_no_readme(tmp_path: Path) -> None:
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
'[tool.poetry]\nname="foo"\nversion="1"\nauthors = []\ndescription = ""\n',
encoding="utf-8",
)
poetry = Factory().create_poetry(tmp_path)
assert not poetry.package.readmes
def test_create_poetry_empty_readme(tmp_path: Path) -> None:
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
'[tool.poetry]\nname="foo"\nversion="1"\nauthors = []\ndescription = ""\n'
'readme = ""\n',
encoding="utf-8",
)
poetry = Factory().create_poetry(tmp_path)
assert not poetry.package.readmes
def test_validate() -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content) == {"errors": [], "warnings": []}
def test_validate_strict_legacy_warnings(complete_legacy_warnings: list[str]) -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {
"errors": [],
"warnings": complete_legacy_warnings,
}
def test_validate_strict_legacy_duplicate_warnings(
complete_legacy_duplicate_warnings: list[str],
) -> None:
complete = fixtures_dir / "complete_duplicates.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {
"errors": [],
"warnings": complete_legacy_duplicate_warnings,
}
def test_validate_strict_new_no_warnings() -> None:
complete = fixtures_dir / "complete_new.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {"errors": [], "warnings": []}
def test_validate_strict_dynamic_warnings() -> None:
# some fields are allowed to be dynamic, but some are not
complete = fixtures_dir / "complete_new_dynamic_invalid.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {
"errors": ["project must contain ['name'] properties"],
"warnings": [
# version, readme and classifiers are allowed to be dynamic!
"[tool.poetry.name] is deprecated. Use [project.name] instead.",
(
"[tool.poetry.description] is deprecated. Use "
"[project.description] instead."
),
"[tool.poetry.license] is deprecated. Use [project.license] instead.",
"[tool.poetry.authors] is deprecated. Use [project.authors] instead.",
(
"[tool.poetry.maintainers] is deprecated. Use "
"[project.maintainers] instead."
),
"[tool.poetry.keywords] is deprecated. Use [project.keywords] instead.",
"[tool.poetry.homepage] is deprecated. Use [project.urls] instead.",
"[tool.poetry.repository] is deprecated. Use [project.urls] instead.",
"[tool.poetry.documentation] is deprecated. Use [project.urls] instead.",
(
"[tool.poetry.extras] is deprecated. Use "
"[project.optional-dependencies] instead."
),
(
"Defining console scripts in [tool.poetry.scripts] is deprecated. "
"Use [project.scripts] instead. "
"([tool.poetry.scripts] should only be used for scripts of type 'file')."
),
],
}
def test_validate_local_version(tmp_path: Path) -> None:
project = tmp_path / "local_version.toml"
project.write_text(
"""[project]\nname = "local-version"\nversion = "0.5.0+LOCAL.123A"\n""",
encoding="utf-8",
)
with project.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content) == {"errors": [], "warnings": []}
def test_validate_fails() -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
content = tomllib.load(f)
content["tool"]["poetry"]["authors"] = "this is not a valid array"
expected = "tool.poetry.authors must be array"
assert Factory.validate(content) == {"errors": [expected], "warnings": []}
def test_validate_without_strict_fails_only_non_strict() -> None:
project_failing_strict_validation = (
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
with project_failing_strict_validation.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content) == {
"errors": [
"Either [project.name] or [tool.poetry.name] is required in package mode.",
(
"Either [project.version] or [tool.poetry.version] is required in "
"package mode."
),
],
"warnings": [],
}
def test_validate_strict_fails_strict_and_non_strict() -> None:
project_failing_strict_validation = (
fixtures_dir / "project_failing_strict_validation" / "pyproject.toml"
)
with project_failing_strict_validation.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {
"errors": [
"Either [project.name] or [tool.poetry.name] is required in package mode.",
(
"Either [project.version] or [tool.poetry.version] is required in "
"package mode."
),
(
'Cannot find dependency "missing_extra" for extra "some-extras" in '
"main dependencies."
),
(
'Cannot find dependency "another_missing_extra" for extra '
'"some-extras" in main dependencies.'
),
(
'The script "a_script_with_unknown_extra" requires extra "foo" which is'
" not defined."
),
(
"Declared README files must be of same type: found text/markdown,"
" text/x-rst"
),
],
"warnings": [
(
"[tool.poetry.readme] is set but 'readme' is not in "
"[project.dynamic]. If it is static use [project.readme]. If it "
"is dynamic, add 'readme' to [project.dynamic].\n"
"If you want to define multiple readmes, you should define them "
"in [tool.poetry] and add 'readme' to [project.dynamic]."
),
(
"[tool.poetry.extras] is deprecated. Use "
"[project.optional-dependencies] instead."
),
(
"Defining console scripts in [tool.poetry.scripts] is deprecated. "
"Use [project.scripts] instead. "
"([tool.poetry.scripts] should only be used for scripts of type 'file')."
),
(
"A wildcard Python dependency is ambiguous. Consider specifying a more"
" explicit one."
),
(
'The "pathlib2" dependency specifies the "allows-prereleases" property,'
' which is deprecated. Use "allow-prereleases" instead.'
),
(
'The script "a_script_with_unknown_extra" depends on an extra. Scripts'
" depending on extras are deprecated and support for them will be"
" removed in a future version of poetry/poetry-core. See"
" https://packaging.python.org/en/latest/specifications/entry-points/#data-model"
" for details."
),
],
}
@pytest.mark.parametrize("with_project_section", [True, False])
def test_validate_dependencies_non_package_mode(with_project_section: bool) -> None:
content: dict[str, Any] = {
"tool": {"poetry": {"package-mode": False, "dependencies": {"foo": "*"}}}
}
expected: dict[str, list[str]] = {"errors": [], "warnings": []}
if with_project_section:
content["project"] = {"name": "my-project"}
expected["warnings"] = [
(
"[tool.poetry.dependencies] is set but [project.dependencies] is "
"not and 'dependencies' is not in [project.dynamic]. You should "
"either migrate [tool.poetry.dependencies] to "
"[project.dependencies] (if you do not need Poetry-specific "
"features) or add [project.dependencies] in addition to "
"[tool.poetry.dependencies] or add 'dependencies' to "
"[project.dynamic]."
)
]
assert Factory.validate(content, strict=True) == expected
@pytest.mark.parametrize("with_project_section", [True, False])
def test_validate_python_non_package_mode(with_project_section: bool) -> None:
content: dict[str, Any] = {
"tool": {"poetry": {"package-mode": False, "dependencies": {"python": ">=3.9"}}}
}
expected: dict[str, list[str]] = {"errors": [], "warnings": []}
if with_project_section:
content["project"] = {"name": "my-project", "dynamic": ["dependencies"]}
expected["warnings"] = [
(
"[tool.poetry.dependencies.python] is set but [project.requires-python]"
" is not set and 'requires-python' is not in [project.dynamic]."
)
]
assert Factory.validate(content, strict=True) == expected
@pytest.mark.parametrize("section", ["project", "poetry"])
@pytest.mark.parametrize("with_license_classifier", [True, False])
def test_validate_deprecated_license_classifiers(
section: str, with_license_classifier: bool
) -> None:
content: dict[str, Any] = {
"project": {"name": "my-project", "version": "1.0", "license": "MIT"},
"tool": {"poetry": {}},
}
classifiers = ["Topic :: Software Development :: Libraries :: Python Modules"]
expected: dict[str, list[str]] = {"errors": [], "warnings": []}
if with_license_classifier:
classifiers.append("License :: OSI Approved :: MIT License")
expected["warnings"].append(
"License classifiers are deprecated. Use [project.license] instead."
)
if section == "project":
content["project"]["classifiers"] = classifiers
elif section == "poetry":
content["tool"]["poetry"]["classifiers"] = classifiers
content["project"]["dynamic"] = ["classifiers"]
else:
raise RuntimeError("unexpected section")
assert Factory.validate(content, strict=True) == expected
@pytest.mark.parametrize(
"project", ["none", "file", "text", "text_spdx", "str", "str_empty", "str_no_spdx"]
)
@pytest.mark.parametrize("with_license_files", [False, True])
def test_validate_with_license_type(
project: str, with_license_files: bool, tmp_path: Path
) -> None:
project_dir = fixtures_dir / f"with_license_type_{project}"
pyproject_file = project_dir / "pyproject.toml"
if with_license_files:
orig_project_dir = project_dir
project_dir = tmp_path / project
shutil.copytree(orig_project_dir, project_dir)
pyproject_file = project_dir / "pyproject.toml"
new_lines = []
for line in pyproject_file.read_text(encoding="utf-8").splitlines():
if line.startswith("keywords = "):
new_lines.append('license-files = ["LICEN[CS]E*", "AUTHORS*"]')
new_lines.append(line)
pyproject_file.write_text("\n".join(new_lines), encoding="utf-8")
expected_warnings = []
if project.split("_", 1)[0] in {"file", "text"}:
expected_warnings.append(
"Defining [project.license] as a table is deprecated."
" [project.license] should be a valid SPDX license expression."
" License files can be referenced in [project.license-files]."
)
elif project in {"str_empty", "str_no_spdx"}:
expected_warnings.append(
"[project.license] is not a valid SPDX expression."
" This is deprecated and will raise an error in the future."
)
with pyproject_file.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {
"errors": [],
"warnings": expected_warnings,
}
def test_strict_validation_success_on_multiple_readme_files() -> None:
with_readme_files = fixtures_dir / "with_readme_files" / "pyproject.toml"
with with_readme_files.open("rb") as f:
content = tomllib.load(f)
assert Factory.validate(content, strict=True) == {"errors": [], "warnings": []}
def test_strict_validation_fails_on_readme_files_with_unmatching_types() -> None:
with_readme_files = fixtures_dir / "with_readme_files" / "pyproject.toml"
with with_readme_files.open("rb") as f:
content = tomllib.load(f)
content["tool"]["poetry"]["readme"][0] = "README.md"
assert Factory.validate(content, strict=True) == {
"errors": [
"Declared README files must be of same type: found text/markdown,"
" text/x-rst"
],
"warnings": [],
}
def test_create_poetry_fails_on_invalid_configuration() -> None:
with pytest.raises(RuntimeError) as e:
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "invalid_pyproject" / "pyproject.toml"
)
expected = """\
The Poetry configuration is invalid:
- Either [project.name] or [tool.poetry.name] is required in package mode.
- Either [project.version] or [tool.poetry.version] is required in package mode.
"""
assert str(e.value) == expected
def test_create_poetry_fails_on_invalid_mode() -> None:
with pytest.raises(RuntimeError) as e:
Factory().create_poetry(
Path(__file__).parent / "fixtures" / "invalid_mode" / "pyproject.toml"
)
expected = """\
The Poetry configuration is invalid:
- tool.poetry.package-mode must be boolean
- Either [project.name] or [tool.poetry.name] is required in package mode.
- Either [project.version] or [tool.poetry.version] is required in package mode.
"""
assert str(e.value) == expected
def test_create_poetry_omits_dev_dependencies_iff_with_dev_is_false() -> None:
poetry = Factory().create_poetry(fixtures_dir / "sample_project", with_groups=False)
assert not any("dev" in r.groups for r in poetry.package.all_requires)
poetry = Factory().create_poetry(fixtures_dir / "sample_project")
assert any("dev" in r.groups for r in poetry.package.all_requires)
def test_create_poetry_with_invalid_dev_dependencies(caplog: LogCaptureFixture) -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_invalid_dev_deps", with_groups=False
)
assert not any("dev" in r.groups for r in poetry.package.all_requires)
assert not caplog.records
poetry = Factory().create_poetry(fixtures_dir / "project_with_invalid_dev_deps")
assert len(caplog.records) == 1
record = caplog.records[0]
assert record.levelname == "WARNING"
assert "does not exist" in record.message
assert any("dev" in r.groups for r in poetry.package.all_requires)
@pytest.mark.parametrize("with_groups", [True, False])
def test_create_poetry_with_invalid_dependency_groups(with_groups: bool) -> None:
with pytest.raises(RuntimeError) as e:
_ = Factory().create_poetry(
fixtures_dir / "project_with_invalid_dependency_groups",
with_groups=with_groups,
)
expected = """\
The Poetry configuration is invalid:
- dependency-groups.testing[1] must be valid exactly by one definition\
(0 matches found)
"""
assert str(e.value) == expected
def test_create_poetry_with_duplicated_dependency_groups() -> None:
with pytest.raises(RuntimeError) as e:
_ = Factory().create_poetry(
fixtures_dir / "project_with_duplicated_dependency_groups",
)
assert (
"Duplicate dependency group name after normalization: test (Test, test)"
in str(e.value)
)
def test_create_poetry_with_dependency_groups_missing_include() -> None:
with pytest.raises(ValueError) as e:
_ = Factory().create_poetry(
fixtures_dir / "project_with_dependency_groups_missing_include",
)
assert (
str(e.value) == "Group 'test' includes group 'coverage' which is not defined."
)
@pytest.mark.parametrize(
("fixture", "expected"),
[
(
"project_with_dependency_groups_simple_cycle",
[
"Cyclic dependency group include in test: coverage -> test",
"Cyclic dependency group include in coverage: test -> coverage",
],
),
(
"project_with_dependency_groups_complex_cycle",
[
"Cyclic dependency group include in test: coverage -> dev -> test",
"Cyclic dependency group include in coverage: dev -> test -> coverage",
"Cyclic dependency group include in dev: test -> coverage -> dev",
],
),
],
)
def test_create_poetry_with_dependency_groups_simple_cycle(
fixture: str, expected: list[str]
) -> None:
with pytest.raises(RuntimeError) as e:
_ = Factory().create_poetry(fixtures_dir / fixture)
assert all(exp in str(e.value) for exp in expected)
def test_create_poetry_with_groups_and_legacy_dev(caplog: LogCaptureFixture) -> None:
assert not caplog.records
poetry = Factory().create_poetry(
fixtures_dir / "project_with_groups_and_legacy_dev"
)
assert len(caplog.records) == 1
record = caplog.records[0]
assert record.levelname == "WARNING"
assert '"poetry.dev-dependencies" section is deprecated' in record.message
package = poetry.package
dependencies = package.all_requires
assert len(dependencies) == 2
assert {dependency.name for dependency in dependencies} == {"pytest", "pre-commit"}
def test_create_poetry_with_groups_and_explicit_main() -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_groups_and_explicit_main"
)
package = poetry.package
dependencies = package.requires
assert len(dependencies) == 1
assert {dependency.name for dependency in dependencies} == {
"aiohttp",
}
def test_create_poetry_with_markers_and_extras() -> None:
poetry = Factory().create_poetry(fixtures_dir / "project_with_markers_and_extras")
package = poetry.package
dependencies = package.requires
extras = package.extras
assert len(dependencies) == 2
assert {dependency.name for dependency in dependencies} == {"orjson"}
assert set(extras[canonicalize_name("all")]) == set(dependencies)
for dependency in dependencies:
assert dependency.in_extras == ["all"]
assert isinstance(dependency, URLDependency)
assert isinstance(dependency.marker, SingleMarker)
assert dependency.marker.name == "sys_platform"
assert dependency.marker.value == (
"darwin" if "macosx" in dependency.url else "linux"
)
@pytest.mark.parametrize(
"constraint, exp_python, exp_marker",
[
({"python": "3.7"}, "~3.7", 'python_version == "3.7"'),
({"platform": "linux"}, "*", 'sys_platform == "linux"'),
({"markers": 'python_version == "3.7"'}, "~3.7", 'python_version == "3.7"'),
(
{"markers": 'platform_machine == "x86_64"'},
"*",
'platform_machine == "x86_64"',
),
(
{"python": "3.7", "markers": 'platform_machine == "x86_64"'},
"~3.7",
'platform_machine == "x86_64" and python_version == "3.7"',
),
(
{"platform": "linux", "markers": 'platform_machine == "x86_64"'},
"*",
'platform_machine == "x86_64" and sys_platform == "linux"',
),
(
{
"python": "3.7",
"platform": "linux",
"markers": 'platform_machine == "x86_64"',
},
"~3.7",
(
'platform_machine == "x86_64" and python_version == "3.7" and'
' sys_platform == "linux"'
),
),
(
{"python": ">=3.7", "markers": 'python_version < "4.0"'},
"<4.0 >=3.7",
'python_version < "4.0" and python_version >= "3.7"',
),
(
{"platform": "linux", "markers": 'sys_platform == "win32"'},
"*",
"<empty>",
),
],
)
def test_create_dependency_marker_variants(
constraint: dict[str, Any], exp_python: str, exp_marker: str
) -> None:
constraint["version"] = "1.0.0"
dep = Factory.create_dependency("foo", constraint)
assert dep.python_versions == exp_python
assert dep.python_constraint == parse_constraint(exp_python)
assert str(dep.marker) == exp_marker
@pytest.mark.parametrize(
("constraint", "expected"),
[
("1", None),
({"version": "1"}, None),
({"version": "1", "allow-prereleases": False}, False),
({"version": "1", "allow-prereleases": True}, True),
],
)
def test_create_dependency_allow_prereleases(
constraint: str | dict[str, str], expected: bool | None
) -> None:
dep = Factory.create_dependency("foo", constraint)
assert dep.allows_prereleases() is expected
def test_all_classifiers_unique_even_if_classifiers_is_duplicated() -> None:
poetry = Factory().create_poetry(
fixtures_dir / "project_with_duplicated_classifiers"
)
package = poetry.package
assert package.all_classifiers == [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Software Development :: Build Tools",
]
@pytest.mark.parametrize(
("project", "expected"),
[
("sample_project", set(BuildSystem().dependencies)),
(
"project_with_build_system_requires",
{
Dependency.create_from_pep_508("poetry-core"),
Dependency.create_from_pep_508("Cython (>=0.29.6,<0.30.0)"),
},
),
],
)
def test_poetry_build_system_dependencies_from_fixtures(
project: str, expected: set[Dependency]
) -> None:
poetry = Factory().create_poetry(fixtures_dir / project)
assert set(poetry.build_system_dependencies) == expected
SAMPLE_PROJECT_DIRECTORY = fixtures_dir / "sample_project"
SIMPLE_PROJECT_WHEEL = (
fixtures_dir
/ "simple_project"
/ "dist"
/ "simple_project-1.2.3-py2.py3-none-any.whl"
)
@pytest.mark.parametrize(
("requires", "expected"),
[
(BuildSystem().requires, set(BuildSystem().dependencies)),
(["poetry-core>=2.0.0"], {Dependency("poetry-core", ">=2.0.0")}),
(["****invalid****"], set()),
(
["hatch", "numpy ; sys_platform == 'win32'"],
{
Dependency("hatch", "*"),
Dependency.create_from_pep_508("numpy ; sys_platform == 'win32'"),
},
),
(
[SAMPLE_PROJECT_DIRECTORY.as_posix()],
{
DirectoryDependency(
SAMPLE_PROJECT_DIRECTORY.name, SAMPLE_PROJECT_DIRECTORY
)
},
),
(
[SIMPLE_PROJECT_WHEEL.as_posix()],
{FileDependency(SIMPLE_PROJECT_WHEEL.name, SIMPLE_PROJECT_WHEEL)},
),
],
)
def test_poetry_build_system_dependencies(
requires: list[str], expected: set[Dependency], temporary_directory: Path
) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
build_system_requires = ", ".join(f'"{require}"' for require in requires)
content = f"""[project]
name = "my-package"
version = "1.2.3"
[build-system]
requires = [{build_system_requires}]
build-backend = "some.api.we.do.not.care.about"
"""
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert set(poetry.build_system_dependencies) == expected
@pytest.mark.parametrize("in_order", [True, False])
@pytest.mark.parametrize(
("group_name", "included_group_name"),
[
("testing", "testing"),
("testing", "TESTING"),
("group_a", "group-a"),
# Examples from the PEP 508 spec
# https://packaging.python.org/en/latest/specifications/name-normalization/#valid-non-normalized-names
("friendly-bard", "friendly-bard"),
("friendly-bard", "Friendly-Bard"),
("friendly-bard", "FRIENDLY-BARD"),
("friendly-bard", "friendly.bard"),
("friendly-bard", "friendly_bard"),
("friendly-bard", "friendly--bard"),
("friendly-bard", "FrIeNdLy-._.-bArD"),
("friendly-Bard", "friendly-bard"),
("FRIENDLY-BARD", "friendly-bard"),
("friendly.bard", "friendly-bard"),
("friendly_bard", "friendly-bard"),
("friendly--bard", "friendly-bard"),
("FrIeNdLy-._.-bArD", "friendly-bard"),
],
)
def test_create_poetry_with_nested_dependency_groups(
group_name: str, included_group_name: str, in_order: bool, temporary_directory: Path
) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
replace_group_name = "%REPLACE_GROUP_NAME%"
replace_included_group_name = "%REPLACE_INCLUDED_GROUP_NAME%"
in_order_content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.%REPLACE_GROUP_NAME%.dependencies]
pytest = "*"
pytest-cov ="*"
[tool.poetry.group.dev]
include-groups = [
"%REPLACE_INCLUDED_GROUP_NAME%",
]
[tool.poetry.group.dev.dependencies]
black = "*"
"""
# The dev group refers to a group that is defined after it.
out_of_order_content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.dev]
include-groups = [
"%REPLACE_INCLUDED_GROUP_NAME%",
]
[tool.poetry.group.dev.dependencies]
black = "*"
[tool.poetry.group.%REPLACE_GROUP_NAME%.dependencies]
pytest = "*"
pytest-cov ="*"
"""
# Generate the content. If `group_name` has a `.` in it, we "escape" it with
# quotes to make it a valid TOML key.
base_content = in_order_content if in_order else out_of_order_content
group_name_to_use = group_name if "." not in group_name else f'"{group_name}"'
content = base_content.replace(replace_group_name, group_name_to_use).replace(
replace_included_group_name, included_group_name
)
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
# Groups are reported internally using their canonical names.
canonical_name = canonicalize_name(group_name)
assert len(poetry.package.all_requires) == 5
assert sorted(
[(dep.name, ",".join(dep.groups)) for dep in poetry.package.all_requires],
key=lambda x: x[0] + x[1],
) == sorted(
[
("black", "dev"),
("pytest-cov", "dev"),
("pytest-cov", canonical_name),
("pytest", "dev"),
("pytest", canonical_name),
],
key=lambda x: x[0] + x[1],
)
def assert_invalid_group_including(
toml_data: str,
expected_error: str,
error_type: type[Exception],
temporary_directory: Path,
) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
pyproject_toml.write_text(toml_data, encoding="utf-8")
with pytest.raises(error_type) as error:
_ = Factory().create_poetry(temporary_directory)
assert str(error.value) == expected_error
@pytest.mark.parametrize(
"include_group_name", ["testing_group", "Testing-Group", "testing-group"]
)
def test_create_poetry_with_self_referenced_dependency_groups(
include_group_name: str,
temporary_directory: Path,
) -> None:
"""testing-group -> testing-group"""
content = f"""\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.testing-group]
include-groups = [
"{include_group_name}",
]
[tool.poetry.group.testing-group.dependencies]
pytest = "*"
pytest-cov ="*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in testing-group: testing-group
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_direct_cyclic_dependency_groups(
temporary_directory: Path,
) -> None:
"""
testing -> dev
dev -> testing
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.testing]
include-groups = [
"dev",
]
[tool.poetry.group.testing.dependencies]
pytest = "*"
pytest-cov ="*"
[tool.poetry.group.dev]
include-groups = [
"testing",
]
[tool.poetry.group.dev.dependencies]
black = "*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in testing: dev -> testing
- Cyclic dependency group include in dev: testing -> dev
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_indirect_full_cyclic_dependency_groups(
temporary_directory: Path,
) -> None:
"""
group-1 -> group-3
group-2 -> group-1
group-3 -> group-2
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.group_1]
include-groups = [
"group_3",
]
[tool.poetry.group.group_1.dependencies]
foo = "*"
[tool.poetry.group.group_2]
include-groups = [
"group_1",
]
[tool.poetry.group.group_2.dependencies]
bar = "*"
[tool.poetry.group.group_3]
include-groups = [
"group_2",
]
[tool.poetry.group.group_3.dependencies]
baz = "*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in group-1: group-3 -> group-2 -> group-1
- Cyclic dependency group include in group-2: group-1 -> group-3 -> group-2
- Cyclic dependency group include in group-3: group-2 -> group-1 -> group-3
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_indirect_partial_cyclic_dependency_groups(
temporary_directory: Path,
) -> None:
"""
group-1 -> group-2
group-2 -> group-1
group-3 -> group-2
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.group_1]
include-groups = [
"group_2",
]
[tool.poetry.group.group_1.dependencies]
foo = "*"
[tool.poetry.group.group_2]
include-groups = [
"group_1",
]
[tool.poetry.group.group_2.dependencies]
bar = "*"
[tool.poetry.group.group_3]
include-groups = [
"group_2",
]
[tool.poetry.group.group_3.dependencies]
baz = "*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in group-1: group-2 -> group-1
- Cyclic dependency group include in group-2: group-1 -> group-2
- Cyclic dependency group include in group-3: group-2 -> group-1 -> group-2
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_shared_dependency_groups(
temporary_directory: Path,
) -> None:
"""
root -> child-1, child-2
child-1 -> shared
child-2 -> shared
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.root]
include-groups = [
"child_1",
"child_2",
]
[tool.poetry.group.root.dependencies]
foo = "*"
[tool.poetry.group.child_1]
include-groups = [
"shared",
]
[tool.poetry.group.child_1.dependencies]
bar = "*"
[tool.poetry.group.child_2]
include-groups = [
"shared",
]
[tool.poetry.group.child_2.dependencies]
baz = "*"
[tool.poetry.group.shared.dependencies]
quux = "*"
"""
pyproject_toml = temporary_directory / "pyproject.toml"
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert len(poetry.package.all_requires) == 10
assert sorted(
[(dep.name, ",".join(dep.groups)) for dep in poetry.package.all_requires],
key=lambda x: x[0] + x[1],
) == [
("bar", "child-1"),
("bar", "root"),
("baz", "child-2"),
("baz", "root"),
("foo", "root"),
("quux", "child-1"),
("quux", "child-2"),
# Duplicates because dependency is included via several groups.
# This is ok because they are merged during dependency resolution.
("quux", "root"),
("quux", "root"),
("quux", "shared"),
]
def test_create_poetry_with_shared_dependency_groups_more_complicated(
temporary_directory: Path,
) -> None:
"""
root -> child-1, child-2
child-1 -> shared
child-2 -> grandchild
grandchild -> shared
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.root]
include-groups = [
"child_1",
"child_2",
]
[tool.poetry.group.root.dependencies]
foo = "*"
[tool.poetry.group.child_1]
include-groups = [
"shared",
]
[tool.poetry.group.child_1.dependencies]
bar = "*"
[tool.poetry.group.child_2]
include-groups = [
"grandchild",
]
[tool.poetry.group.child_2.dependencies]
baz = "*"
[tool.poetry.group.grandchild]
include-groups = [
"shared",
]
[tool.poetry.group.grandchild.dependencies]
bax = "*"
[tool.poetry.group.shared.dependencies]
quux = "*"
"""
pyproject_toml = temporary_directory / "pyproject.toml"
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert len(poetry.package.all_requires) == 14
assert sorted(
[(dep.name, ",".join(dep.groups)) for dep in poetry.package.all_requires],
key=lambda x: x[0] + x[1],
) == [
("bar", "child-1"),
("bar", "root"),
("bax", "child-2"),
("bax", "grandchild"),
("bax", "root"),
("baz", "child-2"),
("baz", "root"),
("foo", "root"),
("quux", "child-1"),
("quux", "child-2"),
("quux", "grandchild"),
# Duplicates because dependency is included via several groups.
# This is ok because they are merged during dependency resolution.
("quux", "root"),
("quux", "root"),
("quux", "shared"),
]
def test_create_poetry_with_complicated_cyclic_diamond_dependency_groups(
temporary_directory: Path,
) -> None:
"""
root -> child-1, child-2
child-1 -> shared
child-2 -> shared
shared -> grandchild
grandchild -> child-2
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.root]
include-groups = [
"child_1",
"child_2",
]
[tool.poetry.group.root.dependencies]
foo = "*"
[tool.poetry.group.child_1]
include-groups = [
"shared",
]
[tool.poetry.group.child_1.dependencies]
bar = "*"
[tool.poetry.group.child_2]
include-groups = [
"shared",
]
[tool.poetry.group.child_2.dependencies]
baz = "*"
[tool.poetry.group.shared]
include-groups = [
"grandchild",
]
[tool.poetry.group.shared.dependencies]
quux = "*"
[tool.poetry.group.grandchild]
include-groups = [
"child_2",
]
[tool.poetry.group.grandchild.dependencies]
bar = "*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in root: child-2 -> shared -> grandchild -> child-2
- Cyclic dependency group include in root: child-1 -> shared -> grandchild -> child-2 -> shared
- Cyclic dependency group include in child-1: shared -> grandchild -> child-2 -> shared
- Cyclic dependency group include in child-2: shared -> grandchild -> child-2
- Cyclic dependency group include in shared: grandchild -> child-2 -> shared
- Cyclic dependency group include in grandchild: child-2 -> shared -> grandchild
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_noncanonical_names_cyclic_dependency_groups(
temporary_directory: Path,
) -> None:
"""
group-1 -> group-2
group-2 -> group-1
group-3 -> group-2
"""
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.GROUP_1]
include-groups = [
"gRoup_2",
]
[tool.poetry.group.GROUP_1.dependencies]
foo = "*"
[tool.poetry.group.group_2]
include-groups = [
"groUp_1",
]
[tool.poetry.group.group_2.dependencies]
bar = "*"
[tool.poetry.group.group_3]
include-groups = [
"group_2",
]
[tool.poetry.group.group_3.dependencies]
baz = "*"
"""
expected = """\
The Poetry configuration is invalid:
- Cyclic dependency group include in group-1: group-2 -> group-1
- Cyclic dependency group include in group-2: group-1 -> group-2
- Cyclic dependency group include in group-3: group-2 -> group-1 -> group-2
"""
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=RuntimeError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_unknown_nested_dependency_groups(
temporary_directory: Path,
) -> None:
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.dev]
include-groups = [
"testing",
]
[tool.poetry.group.dev.dependencies]
black = "*"
"""
expected = "Group 'dev' includes group 'testing' which is not defined."
assert_invalid_group_including(
toml_data=content,
expected_error=expected,
error_type=ValueError,
temporary_directory=temporary_directory,
)
def test_create_poetry_with_included_groups_only(temporary_directory: Path) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.lint.dependencies]
black = "*"
[tool.poetry.group.testing.dependencies]
pytest = "*"
[tool.poetry.group.all]
include-groups = [
"lint",
"testing",
]
"""
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert len(poetry.package.all_requires) == 4
assert [
(dep.name, ",".join(dep.groups)) for dep in poetry.package.all_requires
] == [
("black", "lint"),
("pytest", "testing"),
("black", "all"),
("pytest", "all"),
]
def test_create_poetry_with_nested_similar_dependencies(
temporary_directory: Path,
) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
content = """\
[project]
name = "my-package"
version = "1.2.3"
[tool.poetry.group.parent.dependencies]
foo = "*"
[tool.poetry.group.parent]
include-groups = [
"child",
]
[tool.poetry.group.child.dependencies]
foo = "*"
"""
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert len(poetry.package.all_requires) == 3
assert [
(dep.name, ",".join(dep.groups)) for dep in poetry.package.all_requires
] == [
# Duplicates because dependency is included via several groups.
# This is ok because they are merged during dependency resolution.
("foo", "parent"),
("foo", "parent"),
("foo", "child"),
]
@pytest.mark.parametrize("optional", [False, True])
def test_create_poetry_with_optional_dependency_group(
temporary_directory: Path, optional: bool
) -> None:
pyproject_toml = temporary_directory / "pyproject.toml"
content = f"""\
[project]
name = "my-package"
version = "1.2.3"
[dependency-groups]
dev = [ "foo" ]
# only "optional" in poetry section, no dependencies or include-groups
[tool.poetry.group.dev]
optional = {str(optional).lower()}
"""
pyproject_toml.write_text(content, encoding="utf-8")
poetry = Factory().create_poetry(temporary_directory)
assert len(poetry.package.all_requires) == 1
assert poetry.package.has_dependency_group("dev")
assert poetry.package.dependency_group("dev").is_optional() is optional
|