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
|
"""test_setup
----------------------------------
Tests for `skbuild.setup` function.
"""
from __future__ import annotations
import os
import pprint
import sys
import textwrap
from collections.abc import Sequence
from unittest.mock import patch
import py.path
import pytest
from setuptools import Distribution as setuptool_Distribution
from distutils.core import Distribution as distutils_Distribution
from skbuild import setup as skbuild_setup
from skbuild.constants import CMAKE_INSTALL_DIR, SKBUILD_DIR
from skbuild.exceptions import SKBuildError
from skbuild.platform_specifics import get_platform
from skbuild.setuptools_wrap import strip_package
from skbuild.utils import push_dir, to_platform_path
from . import (
_tmpdir,
execute_setup_py,
initialize_git_repo_and_commit,
is_site_reachable,
push_argv,
)
@pytest.mark.parametrize("distribution_type", ["unknown", "py_modules", "packages", "skbuild"])
def test_distribution_is_pure(distribution_type, tmpdir):
skbuild_setup_kwargs = {}
if distribution_type == "unknown":
is_pure = False
elif distribution_type == "py_modules":
is_pure = True
hello_py = tmpdir.join("hello.py")
hello_py.write("")
skbuild_setup_kwargs["py_modules"] = ["hello"]
elif distribution_type == "packages":
is_pure = True
init_py = tmpdir.mkdir("hello").join("__init__.py")
init_py.write("")
skbuild_setup_kwargs["packages"] = ["hello"]
elif distribution_type == "skbuild":
is_pure = False
cmakelists_txt = tmpdir.join("CMakeLists.txt")
cmakelists_txt.write(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
else:
msg = f"Unknown distribution_type: {distribution_type}"
raise Exception(msg)
platform = get_platform()
original_write_test_cmakelist = platform.write_test_cmakelist
def write_test_cmakelist_no_languages(_self, _languages):
original_write_test_cmakelist([])
with patch.object(type(platform), "write_test_cmakelist", new=write_test_cmakelist_no_languages):
with push_dir(str(tmpdir)), push_argv(["setup.py", "build"]):
distribution = skbuild_setup(
name="test",
version="0.0.1",
description="test object returned by setup function",
author="The scikit-build team",
license="MIT",
**skbuild_setup_kwargs, # type: ignore[arg-type]
)
assert issubclass(distribution.__class__, (distutils_Distribution, setuptool_Distribution))
assert is_pure == distribution.is_pure()
@pytest.mark.parametrize("cmake_args", [[], ["--", "-DVAR:STRING=43", "-DVAR_WITH_SPACE:STRING=Ciao Mondo"]])
def test_cmake_args_keyword(cmake_args, capfd):
tmp_dir = _tmpdir("cmake_args_keyword")
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
setup(
name="test_cmake_args_keyword",
version="1.2.3",
description="a minimal example package",
author='The scikit-build team',
license="MIT",
cmake_args=[
"-DVAR:STRING=42",
"-DVAR_WITH_SPACE:STRING=Hello World"
]
)
"""
)
)
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
message(STATUS "VAR[${VAR}]")
message(STATUS "VAR_WITH_SPACE[${VAR_WITH_SPACE}]")
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
)
with execute_setup_py(tmp_dir, ["build", *cmake_args], disable_languages_test=True):
pass
out, _ = capfd.readouterr()
if not cmake_args:
assert "VAR[42]" in out
assert "VAR_WITH_SPACE[Hello World]" in out
else:
assert "VAR[43]" in out
assert "VAR_WITH_SPACE[Ciao Mondo]" in out
@pytest.mark.parametrize(
("cmake_install_dir", "expected_failed", "error_code_type"),
[
(None, True, str),
("", True, str),
(str(py.path.local.get_temproot().join("scikit-build")), True, SKBuildError),
("banana", False, str),
],
)
def test_cmake_install_dir_keyword(cmake_install_dir, expected_failed, error_code_type, capsys, caplog):
# -------------------------------------------------------------------------
# "SOURCE" tree layout:
#
# ROOT/
#
# CMakeLists.txt
# setup.py
#
# apple/
# __init__.py
#
# -------------------------------------------------------------------------
# "BINARY" distribution layout
#
# ROOT/
#
# apple/
# __init__.py
#
tmp_dir = _tmpdir("cmake_install_dir_keyword")
setup_kwarg = ""
if cmake_install_dir is not None:
setup_kwarg = f"cmake_install_dir={str(cmake_install_dir)!r}"
tmp_dir.join("setup.py").write(
textwrap.dedent(
f"""
from skbuild import setup
setup(
name="test_cmake_install_dir",
version="1.2.3",
description="a package testing use of cmake_install_dir",
author='The scikit-build team',
license="MIT",
packages=['apple', 'banana'],
{setup_kwarg}
)
"""
)
)
# Install location purposely set to "." so that we can test
# usage of "cmake_install_dir" skbuild.setup keyword.
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(banana NONE)
file(WRITE "${CMAKE_BINARY_DIR}/__init__.py" "")
install(FILES "${CMAKE_BINARY_DIR}/__init__.py" DESTINATION ".")
"""
)
)
tmp_dir.ensure("apple", "__init__.py")
failed = False
message = ""
try:
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
except SystemExit as e:
# Error is not of type SKBuildError, it is expected to be
# raised by distutils.core.setup
failed = isinstance(e.code, error_code_type)
message = str(e)
out, _ = capsys.readouterr()
out += caplog.text
assert failed == expected_failed
if failed:
if error_code_type is str:
assert message == "error: package directory '{}' does not exist".format(
os.path.join(CMAKE_INSTALL_DIR(), "banana")
)
else:
assert message.strip().startswith("setup parameter 'cmake_install_dir' is set to an absolute path.")
else:
init_py = to_platform_path(f"{CMAKE_INSTALL_DIR()}/banana/__init__.py")
assert f"copying {init_py}" in out
@pytest.mark.parametrize("cmake_with_sdist", [True, False])
def test_cmake_with_sdist_keyword(cmake_with_sdist, capfd):
tmp_dir = _tmpdir("cmake_with_sdist")
tmp_dir.join("setup.py").write(
textwrap.dedent(
f"""
from skbuild import setup
setup(
name="cmake_with_sdist_keyword",
version="1.2.3",
description="a minimal example package",
author='The scikit-build team',
license="MIT",
cmake_with_sdist={cmake_with_sdist}
)
"""
)
)
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
)
initialize_git_repo_and_commit(tmp_dir)
with execute_setup_py(tmp_dir, ["sdist"], disable_languages_test=True):
pass
out, _ = capfd.readouterr()
if cmake_with_sdist:
assert "Generating done" in out
else:
assert "Generating done" not in out
def test_cmake_minimum_required_version_keyword():
tmp_dir = _tmpdir("cmake_minimum_required_version")
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
setup(
name="cmake_with_sdist_keyword",
version="1.2.3",
description="a minimal example package",
author='The scikit-build team',
license="MIT",
cmake_minimum_required_version='99.98.97'
)
"""
)
)
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
)
try:
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
except SystemExit as e:
# Error is not of type SKBuildError, it is expected to be
# raised by distutils.core.setup
failed = isinstance(e.code, SKBuildError)
message = str(e)
assert failed
assert "CMake version 99.98.97 or higher is required." in message
@pytest.mark.deprecated()
@pytest.mark.filterwarnings("ignore:setuptools.installer is deprecated:Warning")
@pytest.mark.skipif(
os.environ.get("CONDA_BUILD", "0") == "1",
reason="running tests expecting network connection in Conda is not possible. "
"See https://github.com/conda/conda/issues/508",
)
@pytest.mark.skipif(not is_site_reachable("https://pypi.org/simple/cmake/"), reason="pypi.org website not reachable")
@pytest.mark.xfail(
sys.platform.startswith("cygwin"), strict=False, reason="Cygwin needs a release of scikit-build first"
)
def test_setup_requires_keyword_include_cmake(mocker, capsys):
mock_setup = mocker.patch("skbuild.setuptools_wrap.setuptools.setup")
tmp_dir = _tmpdir("setup_requires_keyword_include_cmake")
setup_requires = ["cmake>=3.10"]
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
setup(
name="cmake_with_sdist_keyword",
version="1.2.3",
description="a minimal example package",
author='The scikit-build team',
license="MIT",
setup_requires=[{setup_requires}]
)
""".format(setup_requires=",".join([f"'{package}'" for package in setup_requires]))
)
)
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
)
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
assert mock_setup.call_count == 1
setup_kw = mock_setup.call_args[1]
assert setup_kw["setup_requires"] == setup_requires
import cmake
out, _ = capsys.readouterr()
if "Searching for cmake>=3.10" in out:
assert cmake.__file__.lower().startswith(str(tmp_dir).lower())
@pytest.mark.parametrize("distribution_type", ["pure", "skbuild"])
def test_script_keyword(distribution_type, capsys, caplog):
# -------------------------------------------------------------------------
#
# "SOURCE" tree layout for "pure" distribution:
#
# ROOT/
# setup.py
# foo.py
# bar.py
#
# "SOURCE" tree layout for "pure" distribution:
#
# ROOT/
# setup.py
# CMakeLists.txt
#
# -------------------------------------------------------------------------
# "BINARY" distribution layout is identical for both
#
# ROOT/
# foo.py
# bar.py
#
tmp_dir = _tmpdir("script_keyword")
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
setup(
name="test_script_keyword",
version="1.2.3",
description="a package testing use of script keyword",
author='The scikit-build team',
license="MIT",
scripts=['foo.py', 'bar.py'],
packages=[],
)
"""
)
)
if distribution_type == "skbuild":
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(foo NONE)
file(WRITE "${CMAKE_BINARY_DIR}/foo.py" "# foo.py")
file(WRITE "${CMAKE_BINARY_DIR}/bar.py" "# bar.py")
install(
FILES
"${CMAKE_BINARY_DIR}/foo.py"
"${CMAKE_BINARY_DIR}/bar.py"
DESTINATION "."
)
"""
)
)
messages = [
f"copying {CMAKE_INSTALL_DIR()}/{module}.py -> {SKBUILD_DIR()}/setuptools/scripts-"
for module in ["foo", "bar"]
]
elif distribution_type == "pure":
tmp_dir.join("foo.py").write("# foo.py")
tmp_dir.join("bar.py").write("# bar.py")
messages = [f"copying {module}.py -> {SKBUILD_DIR()}/setuptools/scripts-" for module in ["foo", "bar"]]
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
out, _ = capsys.readouterr()
out += caplog.text
for message in messages:
assert to_platform_path(message) in out
@pytest.mark.parametrize("distribution_type", ["pure", "skbuild"])
def test_py_modules_keyword(distribution_type, capsys, caplog):
# -------------------------------------------------------------------------
#
# "SOURCE" tree layout for "pure" distribution:
#
# ROOT/
# setup.py
# foo.py
# bar.py
#
# "SOURCE" tree layout for "skbuild" distribution:
#
# ROOT/
# setup.py
# CMakeLists.txt
#
# -------------------------------------------------------------------------
# "BINARY" distribution layout is identical for both
#
# ROOT/
# foo.py
# bar.py
#
tmp_dir = _tmpdir("py_modules_keyword")
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
setup(
name="test_py_modules_keyword",
version="1.2.3",
description="a package testing use of py_modules keyword",
author='The scikit-build team',
license="MIT",
py_modules=['foo', 'bar']
)
"""
)
)
if distribution_type == "skbuild":
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(foobar NONE)
file(WRITE "${CMAKE_BINARY_DIR}/foo.py" "# foo.py")
file(WRITE "${CMAKE_BINARY_DIR}/bar.py" "# bar.py")
install(
FILES
"${CMAKE_BINARY_DIR}/foo.py"
"${CMAKE_BINARY_DIR}/bar.py"
DESTINATION "."
)
"""
)
)
messages = [
f"copying {CMAKE_INSTALL_DIR()}/{module}.py -> {SKBUILD_DIR()}/setuptools/lib" for module in ["foo", "bar"]
]
elif distribution_type == "pure":
tmp_dir.join("foo.py").write("# foo.py")
tmp_dir.join("bar.py").write("# bar.py")
messages = [f"copying {module}.py -> {SKBUILD_DIR()}/setuptools/lib" for module in ["foo", "bar"]]
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
out, _ = capsys.readouterr()
out += caplog.text
for message in messages:
assert to_platform_path(message) in out
@pytest.mark.parametrize(
("package_parts", "module_file", "expected"),
[
([], "", ""),
([""], "file.py", "file.py"),
([], "foo/file.py", "foo/file.py"),
(["foo"], "", ""),
(["foo"], "foo/file.py", "file.py"),
(["foo"], "foo\\file.py", "file.py"),
(["foo", "bar"], "foo/file.py", "foo/file.py"),
(["foo", "bar"], "foo/bar/file.py", "file.py"),
(["foo", "bar"], "foo/bar/baz/file.py", "baz/file.py"),
(["foo"], "/foo/file.py", "/foo/file.py"),
],
)
def test_strip_package(package_parts, module_file, expected):
assert strip_package(package_parts, module_file) == expected
@pytest.mark.parametrize("has_cmake_package", [0, 1])
@pytest.mark.parametrize("has_cmake_module", [0, 1])
@pytest.mark.parametrize("has_hybrid_package", [0, 1])
@pytest.mark.parametrize("has_pure_package", [0, 1])
@pytest.mark.parametrize("has_pure_module", [0, 1])
@pytest.mark.parametrize("with_package_base", [0, 1])
def test_setup_inputs(
has_cmake_package,
has_cmake_module,
has_hybrid_package,
has_pure_package,
has_pure_module,
with_package_base,
mocker,
):
"""This test that a project can have a package with some modules
installed using setup.py and some other modules installed using CMake.
"""
tmp_dir = _tmpdir("test_setup_inputs")
package_base = "to/the/base" if with_package_base else ""
package_base_dir = package_base + "/" if package_base else ""
cmake_source_dir = package_base
if cmake_source_dir and (has_cmake_package or has_cmake_module):
pytest.skip(
"unsupported configuration: "
"python package fully generated by CMake does *NOT* work. "
"At least __init__.py should be in the project source tree"
)
# -------------------------------------------------------------------------
# Here is the "SOURCE" tree layout:
#
# ROOT/
#
# setup.py
#
# [<base>/]
#
# pureModule.py
#
# pure/
# __init__.py
# pure.py
#
# data/
# pure.dat
#
# [<cmake_src_dir>/]
#
# hybrid/
# CMakeLists.txt
# __init__.py
# hybrid_pure.dat
# hybrid_pure.py
#
# data/
# hybrid_data_pure.dat
#
# hybrid_2/
# __init__.py
# hybrid_2_pure.py
#
# hybrid_2_pure/
# __init__.py
# hybrid_2_pure_1.py
# hybrid_2_pure_2.py
#
#
# -------------------------------------------------------------------------
# and here is the "BINARY" distribution layout:
#
# The comment "CMake" or "Setuptools" indicates which tool is responsible
# for placing the file in the tree used to create the binary distribution.
#
# ROOT/
#
# cmakeModule.py # CMake
#
# cmake/
# __init__.py # CMake
# cmake.py # CMake
#
# hybrid/
# hybrid_cmake.dat # CMake
# hybrid_cmake.py # CMake
# hybrid_pure.dat # Setuptools
# hybrid_pure.py # Setuptools
#
# data/
# hybrid_data_pure.dat # CMake or Setuptools
# hybrid_data_cmake.dat # CMake *NO TEST*
#
# hybrid_2/
# __init__.py # CMake or Setuptools
# hybrid_2_pure.py # CMake or Setuptools
# hybrid_2_cmake.py # CMake
#
# hybrid_2_pure/
# __init__.py # CMake or Setuptools
# hybrid_2_pure_1.py # CMake or Setuptools
# hybrid_2_pure_2.py # CMake or Setuptools
#
# pureModule.py # Setuptools
#
# pure/
# __init__.py # Setuptools
# pure.py # Setuptools
#
# data/
# pure.dat # Setuptools
tmp_dir.join("setup.py").write(
textwrap.dedent(
"""
from skbuild import setup
#from setuptools import setup
setup(
name="test_hybrid_project",
version="1.2.3",
description=("an hybrid package mixing files installed by both "
"CMake and setuptools"),
author='The scikit-build team',
license="MIT",
cmake_source_dir='{cmake_source_dir}',
cmake_install_dir='{cmake_install_dir}',
# Arbitrary order of packages
packages=[
{p_off} 'pure',
{h_off} 'hybrid.hybrid_2',
{h_off} 'hybrid',
{c_off} 'cmake',
{p_off} 'hybrid.hybrid_2_pure',
],
py_modules=[
{pm_off} '{package_base}pureModule',
{cm_off} '{package_base}cmakeModule',
],
package_data={{
{p_off} 'pure': ['data/pure.dat'],
{h_off} 'hybrid': ['hybrid_pure.dat', 'data/hybrid_data_pure.dat'],
}},
# Arbitrary order of package_dir
package_dir = {{
{p_off} 'hybrid.hybrid_2_pure': '{package_base}hybrid/hybrid_2_pure',
{p_off} 'pure': '{package_base}pure',
{h_off} 'hybrid': '{package_base}hybrid',
{h_off} 'hybrid.hybrid_2': '{package_base}hybrid/hybrid_2',
{c_off} 'cmake': '{package_base}cmake',
}}
)
""".format(
cmake_source_dir=cmake_source_dir,
cmake_install_dir=package_base,
package_base=package_base_dir,
c_off="" if has_cmake_package else "#",
cm_off="" if has_cmake_module else "#",
h_off="" if has_hybrid_package else "#",
p_off="" if has_pure_package else "#",
pm_off="" if has_pure_module else "#",
)
)
)
src_dir = tmp_dir.ensure(package_base, dir=1)
src_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(hybrid NONE)
set(build_dir ${{CMAKE_BINARY_DIR}})
{c_off} file(WRITE ${{build_dir}}/__init__.py "")
{c_off} file(WRITE ${{build_dir}}/cmake.py "")
{c_off} install(
{c_off} FILES
{c_off} ${{build_dir}}/__init__.py
{c_off} ${{build_dir}}/cmake.py
{c_off} DESTINATION cmake
{c_off} )
{cm_off} file(WRITE ${{build_dir}}/cmakeModule.py "")
{cm_off} install(
{cm_off} FILES ${{build_dir}}/cmakeModule.py
{cm_off} DESTINATION .)
{h_off} file(WRITE ${{build_dir}}/hybrid_cmake.dat "")
{h_off} install(
{h_off} FILES ${{build_dir}}/hybrid_cmake.dat
{h_off} DESTINATION hybrid)
{h_off} file(WRITE ${{build_dir}}/hybrid_cmake.py "")
{h_off} install(
{h_off} FILES ${{build_dir}}/hybrid_cmake.py
{h_off} DESTINATION hybrid)
{h_off} file(WRITE ${{build_dir}}/hybrid_data_cmake.dat "")
{h_off} install(
{h_off} FILES ${{build_dir}}/hybrid_data_cmake.dat
{h_off} DESTINATION hybrid/data)
{h_off} file(WRITE ${{build_dir}}/hybrid_2_cmake.py "")
{h_off} install(
{h_off} FILES ${{build_dir}}/hybrid_2_cmake.py
{h_off} DESTINATION hybrid/hybrid_2)
install(CODE "message(STATUS \\\"Installation complete\\\")")
""".format(
c_off="" if has_cmake_package else "#",
cm_off="" if has_cmake_module else "#",
h_off="" if has_hybrid_package else "#",
)
)
)
# List path types: 'c', 'cm', 'h', 'p' or 'pm'
path_types: Sequence[str]
try:
path_types = next(
iter(
zip(
*filter(
lambda i: i[1],
[
("c", has_cmake_package),
("cm", has_cmake_module),
("h", has_hybrid_package),
("p", has_pure_package),
("pm", has_pure_module),
],
)
)
)
)
except StopIteration:
path_types = []
def select_paths(annotated_paths):
"""Return a filtered list paths considering ``path_types``.
`annotated_paths`` is list of tuple ``(type, path)`` where type
is either `c`, 'cm', `h`, `p` or 'pm'.
"""
return filter(lambda i: i[0] in path_types, annotated_paths)
# Commented paths are the one expected to be installed by CMake. For
# this reason, corresponding files should NOT be created in the source
# tree.
for _type, path in select_paths(
[
# ('c', 'cmake/__init__.py'),
# ('c', 'cmake/cmake.py'),
# ('cm', 'cmakeModule.py'),
("h", "hybrid/__init__.py"),
# ('h', 'hybrid/hybrid_cmake.dat'),
# ('h', 'hybrid/hybrid_cmake.py'),
("h", "hybrid/hybrid_pure.dat"),
("h", "hybrid/hybrid_pure.py"),
# ('h', 'hybrid/data/hybrid_data_cmake.dat'),
("h", "hybrid/data/hybrid_data_pure.dat"),
("h", "hybrid/hybrid_2/__init__.py"),
# ('h', 'hybrid/hybrid_2/hybrid_2_cmake.py'),
("h", "hybrid/hybrid_2/hybrid_2_pure.py"),
("p", "hybrid/hybrid_2_pure/__init__.py"),
("p", "hybrid/hybrid_2_pure/hybrid_2_pure_1.py"),
("p", "hybrid/hybrid_2_pure/hybrid_2_pure_2.py"),
("pm", "pureModule.py"),
("p", "pure/__init__.py"),
("p", "pure/pure.py"),
("p", "pure/data/pure.dat"),
]
):
assert _type in {"p", "pm", "h"}
root = package_base if _type in {"p", "pm"} else cmake_source_dir
tmp_dir.ensure(os.path.join(root, path))
# Do not call the real setup function. Instead, replace it with
# a MagicMock allowing to check with which arguments it was invoked.
mock_setup = mocker.patch("skbuild.setuptools_wrap.setuptools.setup")
# Convenience print function
def _pprint(desc, value=None):
print(
"-----------------\n"
f"{desc}:\n"
"\n"
f"{pprint.pformat(setup_kw.get(desc, {}) if value is None else value, indent=2)}\n"
)
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
assert mock_setup.call_count == 1
setup_kw = mock_setup.call_args[1]
# packages
expected_packages = []
if has_cmake_package:
expected_packages += ["cmake"]
if has_hybrid_package:
expected_packages += ["hybrid", "hybrid.hybrid_2"]
if has_pure_package:
expected_packages += ["hybrid.hybrid_2_pure", "pure"]
_pprint("expected_packages", expected_packages)
_pprint("packages")
# package dir
expected_package_dir = {
package: (os.path.join(CMAKE_INSTALL_DIR(), package_base, package.replace(".", "/")))
for package in expected_packages
}
_pprint("expected_package_dir", expected_package_dir)
_pprint("package_dir")
# package data
expected_package_data = {}
if has_cmake_package:
expected_package_data["cmake"] = ["__init__.py", "cmake.py"]
if has_hybrid_package:
expected_package_data["hybrid"] = [
"__init__.py",
"hybrid_cmake.dat",
"hybrid_cmake.py",
"hybrid_pure.dat",
"hybrid_pure.py",
"data/hybrid_data_cmake.dat",
"data/hybrid_data_pure.dat",
]
expected_package_data["hybrid.hybrid_2"] = ["__init__.py", "hybrid_2_cmake.py", "hybrid_2_pure.py"]
if has_pure_package:
expected_package_data["hybrid.hybrid_2_pure"] = ["__init__.py", "hybrid_2_pure_1.py", "hybrid_2_pure_2.py"]
expected_package_data["pure"] = [
"__init__.py",
"pure.py",
"data/pure.dat",
]
if has_cmake_module or has_pure_module:
expected_modules = []
if has_cmake_module:
expected_modules.append(package_base_dir + "cmakeModule.py")
if has_pure_module:
expected_modules.append(package_base_dir + "pureModule.py")
expected_package_data[""] = expected_modules
_pprint("expected_package_data", expected_package_data)
package_data = {p: sorted(files) for p, files in setup_kw["package_data"].items()}
_pprint("package_data", package_data)
# py_modules (corresponds to files associated with empty package)
expected_py_modules = []
if "" in expected_package_data:
expected_py_modules = [os.path.splitext(module_file)[0] for module_file in expected_package_data[""]]
_pprint("expected_py_modules", expected_py_modules)
_pprint("py_modules")
# scripts
_pprint("expected_scripts", [])
_pprint("scripts")
# data_files
_pprint("expected_data_files", [])
_pprint("data_files")
assert sorted(setup_kw["packages"]) == sorted(expected_packages)
assert sorted(setup_kw["package_dir"]) == sorted(expected_package_dir)
assert package_data == {p: sorted(files) for p, files in expected_package_data.items()}
assert sorted(setup_kw["py_modules"]) == sorted(expected_py_modules)
assert sorted(setup_kw["scripts"]) == sorted([])
assert sorted(setup_kw["data_files"]) == sorted([])
@pytest.mark.parametrize("with_cmake_source_dir", [0, 1])
def test_cmake_install_into_pure_package(with_cmake_source_dir, capsys, caplog):
# -------------------------------------------------------------------------
# "SOURCE" tree layout:
#
# (1) with_cmake_source_dir == 0
#
# ROOT/
#
# CMakeLists.txt
# setup.py
#
# fruits/
# __init__.py
#
#
# (2) with_cmake_source_dir == 1
#
# ROOT/
#
# setup.py
#
# fruits/
# __init__.py
#
# src/
#
# CMakeLists.txt
#
# -------------------------------------------------------------------------
# "BINARY" distribution layout:
#
# ROOT/
#
# fruits/
#
# __init__.py
# apple.py
# banana.py
#
# data/
#
# apple.dat
# banana.dat
#
tmp_dir = _tmpdir("cmake_install_into_pure_package")
cmake_source_dir = "src" if with_cmake_source_dir else ""
tmp_dir.join("setup.py").write(
textwrap.dedent(
f"""
from skbuild import setup
setup(
name="test_py_modules_keyword",
version="1.2.3",
description="a package testing use of py_modules keyword",
author='The scikit-build team',
license="MIT",
packages=['fruits'],
cmake_install_dir='fruits',
cmake_source_dir='{cmake_source_dir}',
)
"""
)
)
cmake_src_dir = tmp_dir.ensure(cmake_source_dir, dir=1)
cmake_src_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
file(WRITE "${CMAKE_BINARY_DIR}/apple.py" "# apple.py")
file(WRITE "${CMAKE_BINARY_DIR}/banana.py" "# banana.py")
install(
FILES
"${CMAKE_BINARY_DIR}/apple.py"
"${CMAKE_BINARY_DIR}/banana.py"
DESTINATION "."
)
file(WRITE "${CMAKE_BINARY_DIR}/apple.dat" "# apple.dat")
file(WRITE "${CMAKE_BINARY_DIR}/banana.dat" "# banana.dat")
install(
FILES
"${CMAKE_BINARY_DIR}/apple.dat"
"${CMAKE_BINARY_DIR}/banana.dat"
DESTINATION "data"
)
"""
)
)
tmp_dir.ensure("fruits/__init__.py")
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
messages = [
f"copying {CMAKE_INSTALL_DIR()}/{module} -> {SKBUILD_DIR()}/setuptools/lib"
for module in [
"fruits/__init__.py",
"fruits/apple.py",
"fruits/banana.py",
"fruits/data/apple.dat",
"fruits/data/banana.dat",
]
]
out, _ = capsys.readouterr()
out += caplog.text
for message in messages:
assert to_platform_path(message) in out
@pytest.mark.parametrize("zip_safe", [None, False, True])
def test_zip_safe_default(zip_safe, mocker):
mock_setup = mocker.patch("skbuild.setuptools_wrap.setuptools.setup")
tmp_dir = _tmpdir("zip_safe_default")
setup_kwarg = ""
if zip_safe is not None:
setup_kwarg = f"zip_safe={zip_safe}"
tmp_dir.join("setup.py").write(
textwrap.dedent(
f"""
from skbuild import setup
setup(
name="zip_safe_default",
version="1.2.3",
description="a minimal example package",
author='The scikit-build team',
license="MIT",
{setup_kwarg}
)
"""
)
)
tmp_dir.join("CMakeLists.txt").write(
textwrap.dedent(
"""
cmake_minimum_required(VERSION 3.5.0)
project(test NONE)
install(CODE "execute_process(
COMMAND \\${CMAKE_COMMAND} -E sleep 0)")
"""
)
)
with execute_setup_py(tmp_dir, ["build"], disable_languages_test=True):
pass
assert mock_setup.call_count == 1
setup_kw = mock_setup.call_args[1]
assert "zip_safe" in setup_kw
if zip_safe is None:
assert not setup_kw["zip_safe"]
elif zip_safe:
assert setup_kw["zip_safe"]
else: # zip_safe is False
assert not setup_kw["zip_safe"]
|