From ff7b8e6703e8407773968517d68424b9ec59aa30 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 9 Sep 2021 11:05:17 +0200 Subject: [PATCH] testing that cmake_paths functionality is covered by CMakeToolchain (#9538) * testing that cmake_paths functionality is covered by CMakeToolchain * add real helloConfig.cmake test * checking CMakeDeps has priority --- .../test_cmakedeps_custom_configs.py | 0 .../test_cmakedeps_find_module_and_config.py | 9 +- .../{ => cmake}/test_cmake_toolchain.py | 0 .../cmake/test_cmaketoolchain_paths.py | 131 ++++++++++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) rename conans/test/functional/toolchains/cmake/{ => cmakedeps}/test_cmakedeps_custom_configs.py (100%) rename conans/test/functional/toolchains/{ => cmake}/test_cmake_toolchain.py (100%) create mode 100644 conans/test/functional/toolchains/cmake/test_cmaketoolchain_paths.py diff --git a/conans/test/functional/toolchains/cmake/test_cmakedeps_custom_configs.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_custom_configs.py similarity index 100% rename from conans/test/functional/toolchains/cmake/test_cmakedeps_custom_configs.py rename to conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_custom_configs.py diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py index 6afdf5b3d22..f2e71ec6ba5 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_find_module_and_config.py @@ -48,11 +48,14 @@ def package_info(self): self.cpp_info.set_property("cmake_module_file_name", "mi_dependencia") self.cpp_info.set_property("cmake_module_target_name", "mi_dependencia_target") - self.cpp_info.set_property("cmake_module_target_namespace", "mi_dependencia_namespace") + self.cpp_info.set_property("cmake_module_target_namespace", + "mi_dependencia_namespace") self.cpp_info.components["crispin"].libs = ["mydep"] - self.cpp_info.components["crispin"].set_property("cmake_target_name", "MyCrispinTarget") - self.cpp_info.components["crispin"].set_property("cmake_module_target_name", "mi_crispin_target") + self.cpp_info.components["crispin"].set_property("cmake_target_name", + "MyCrispinTarget") + self.cpp_info.components["crispin"].set_property("cmake_module_target_name", + "mi_crispin_target") """) t.save({"conanfile.py": conanfile, diff --git a/conans/test/functional/toolchains/test_cmake_toolchain.py b/conans/test/functional/toolchains/cmake/test_cmake_toolchain.py similarity index 100% rename from conans/test/functional/toolchains/test_cmake_toolchain.py rename to conans/test/functional/toolchains/cmake/test_cmake_toolchain.py diff --git a/conans/test/functional/toolchains/cmake/test_cmaketoolchain_paths.py b/conans/test/functional/toolchains/cmake/test_cmaketoolchain_paths.py new file mode 100644 index 00000000000..c8f6522e20d --- /dev/null +++ b/conans/test/functional/toolchains/cmake/test_cmaketoolchain_paths.py @@ -0,0 +1,131 @@ +import textwrap + +import pytest + +from conans.test.utils.tools import TestClient + + +@pytest.mark.tool_cmake +@pytest.mark.parametrize("package", ["hello", "ZLIB"]) +@pytest.mark.parametrize("find_package", ["module", "config"]) +def test_cmaketoolchain_path_find(package, find_package): + """Test with user "Hello" and also ZLIB one, to check that package ZLIB + has priority over the CMake system one + + Also, that user cmake files in the root are accessible via CMake include() + """ + client = TestClient() + conanfile = textwrap.dedent(""" + from conans import ConanFile + class TestConan(ConanFile): + exports = "*" + def layout(self): + pass + def package(self): + self.copy(pattern="*", keep_path=False) + """) + find = textwrap.dedent(""" + SET({package}_FOUND 1) + MESSAGE("HELLO FROM THE {package} FIND PACKAGE!") + """).format(package=package) + myowncmake = textwrap.dedent(""" + MESSAGE("MYOWNCMAKE FROM {package}!") + """).format(package=package) + + filename = "{}Config.cmake" if find_package == "config" else "Find{}.cmake" + filename = filename.format(package) + client.save({"conanfile.py": conanfile, + "{}".format(filename): find, + "myowncmake.cmake": myowncmake}) + client.run("create . {}/0.1@".format(package)) + + consumer = textwrap.dedent(""" + set(CMAKE_CXX_COMPILER_WORKS 1) + set(CMAKE_CXX_ABI_COMPILED 1) + project(MyHello CXX) + cmake_minimum_required(VERSION 3.15) + + find_package({package} REQUIRED) + include(myowncmake) + """).format(package=package) + + client.save({"CMakeLists.txt": consumer}, clean_first=True) + client.run("install {}/0.1@ -g CMakeToolchain".format(package)) + with client.chdir("build"): + client.run_command("cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake") + assert "Conan: Target declared" not in client.out + assert "HELLO FROM THE {package} FIND PACKAGE!".format(package=package) in client.out + assert "MYOWNCMAKE FROM {package}!".format(package=package) in client.out + + # If using the CMakeDeps generator, the in-package .cmake will be ignored + # But it is still possible to include(owncmake) + client.run("install {}/0.1@ -g CMakeToolchain -g CMakeDeps".format(package)) + with client.chdir("build2"): # A clean folder, not the previous one, CMake cache doesnt affect + client.run_command("cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake") + assert "Conan: Target declared '{package}::{package}'".format(package=package) in client.out + assert "HELLO FROM THE {package} FIND PACKAGE!".format(package=package) not in client.out + assert "MYOWNCMAKE FROM {package}!".format(package=package) in client.out + + +@pytest.mark.tool_cmake +def test_cmaketoolchain_path_find_real_config(): + client = TestClient() + conanfile = textwrap.dedent(""" + from conans import ConanFile + from conan.tools.cmake import CMake + class TestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + exports = "*" + generators = "CMakeToolchain" + + def layout(self): + pass + + def build(self): + cmake = CMake(self) + cmake.configure() + + def package(self): + cmake = CMake(self) + cmake.install() + """) + cmake = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(MyHello NONE) + + add_library(hello INTERFACE) + install(TARGETS hello EXPORT helloConfig) + export(TARGETS hello + NAMESPACE hello:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/helloConfig.cmake" + ) + install(EXPORT helloConfig + DESTINATION "${CMAKE_INSTALL_PREFIX}/hello/cmake" + NAMESPACE hello:: + ) + """) + client.save({"conanfile.py": conanfile, + "CMakeLists.txt": cmake}) + client.run("create . hello/0.1@") + + consumer = textwrap.dedent(""" + project(MyHello NONE) + cmake_minimum_required(VERSION 3.15) + + find_package(hello REQUIRED) + """) + + client.save({"CMakeLists.txt": consumer}, clean_first=True) + client.run("install hello/0.1@ -g CMakeToolchain") + with client.chdir("build"): + client.run_command("cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake") + # If it didn't fail, it found the helloConfig.cmake + assert "Conan: Target declared" not in client.out + + # If using the CMakeDeps generator, the in-package .cmake will be ignored + # But it is still possible to include(owncmake) + client.run("install hello/0.1@ -g CMakeToolchain -g CMakeDeps") + with client.chdir("build2"): # A clean folder, not the previous one, CMake cache doesnt affect + client.run_command("cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake") + assert "Conan: Target declared 'hello::hello'" in client.out +