From 10b9e3a1e45a5a07ff9da561122a094703c529f4 Mon Sep 17 00:00:00 2001 From: "R. Yushaev" <44146334+Naufragous@users.noreply.github.com> Date: Thu, 7 Jul 2022 21:35:50 +0200 Subject: [PATCH 1/3] Fix bug in CMake.test() when using Ninja Multi-Config (#11405) --- conan/tools/cmake/cmake.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conan/tools/cmake/cmake.py b/conan/tools/cmake/cmake.py index b3d6535a56a..0805f70922e 100644 --- a/conan/tools/cmake/cmake.py +++ b/conan/tools/cmake/cmake.py @@ -153,7 +153,8 @@ def test(self, build_type=None, target=None, cli_args=None, build_tool_args=None return if not target: is_multi = is_multi_configuration(self._generator) - target = "RUN_TESTS" if is_multi else "test" + is_ninja = "Ninja" in self._generator + target = "RUN_TESTS" if is_multi and not is_ninja else "test" self._build(build_type=build_type, target=target, cli_args=cli_args, build_tool_args=build_tool_args) From 9cb464da23bed81313799463f8bf8675f44601b9 Mon Sep 17 00:00:00 2001 From: "R. Yushaev" <44146334+Naufragous@users.noreply.github.com> Date: Fri, 8 Jul 2022 19:56:38 +0200 Subject: [PATCH 2/3] Add unit test for the CMake.test() helper --- .../unittests/tools/cmake/test_cmake_test.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 conans/test/unittests/tools/cmake/test_cmake_test.py diff --git a/conans/test/unittests/tools/cmake/test_cmake_test.py b/conans/test/unittests/tools/cmake/test_cmake_test.py new file mode 100644 index 00000000000..897469529e3 --- /dev/null +++ b/conans/test/unittests/tools/cmake/test_cmake_test.py @@ -0,0 +1,42 @@ +import pytest + +from conan.tools.cmake import CMake +from conan.tools.cmake.presets import write_cmake_presets +from conans.client.conf import get_default_settings_yml +from conans.model.conf import Conf +from conans.model.settings import Settings +from conans.test.utils.mocks import ConanFileMock +from conans.test.utils.test_files import temp_folder + + +@pytest.mark.parametrize("generator,target", [ + ("NMake Makefiles", "test"), + ("Ninja Makefiles", "test"), + ("Ninja Multi-Config", "test"), + ("Unix Makefiles", "test"), + ("Visual Studio 14 2015", "RUN_TESTS"), + ("Xcode", "RUN_TESTS"), +]) +def test_run_tests(generator, target): + """ + Testing that the proper test target is picked for different generators, especially multi-config ones. + Issue related: https://github.com/conan-io/conan/issues/11405 + """ + settings = Settings.loads(get_default_settings_yml()) + settings.os = "Windows" + settings.arch = "x86" + settings.build_type = "Release" + settings.compiler = "Visual Studio" + settings.compiler.runtime = "MDd" + settings.compiler.version = "14" + + conanfile = ConanFileMock() + conanfile.conf = Conf() + conanfile.folders.generators = "." + conanfile.folders.set_base_generators(temp_folder()) + conanfile.settings = settings + + write_cmake_presets(conanfile, "toolchain", generator, {}) + cmake = CMake(conanfile) + cmake.test() + assert "'--target' '%s'" % target in conanfile.command From f85809bc67093c19a1229f8cafe31e78fcd832ea Mon Sep 17 00:00:00 2001 From: "R. Yushaev" <44146334+Naufragous@users.noreply.github.com> Date: Sat, 9 Jul 2022 12:52:37 +0200 Subject: [PATCH 3/3] Fix unit test for Windows platform --- conans/test/unittests/tools/cmake/test_cmake_test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/conans/test/unittests/tools/cmake/test_cmake_test.py b/conans/test/unittests/tools/cmake/test_cmake_test.py index 897469529e3..3e852b10d06 100644 --- a/conans/test/unittests/tools/cmake/test_cmake_test.py +++ b/conans/test/unittests/tools/cmake/test_cmake_test.py @@ -1,3 +1,4 @@ +import platform import pytest from conan.tools.cmake import CMake @@ -39,4 +40,6 @@ def test_run_tests(generator, target): write_cmake_presets(conanfile, "toolchain", generator, {}) cmake = CMake(conanfile) cmake.test() - assert "'--target' '%s'" % target in conanfile.command + + search_pattern = "--target {}" if platform.system() == "Windows" else "'--target' '{}'" + assert search_pattern.format(target) in conanfile.command