From 018441d3a6e87fdc520bda82d77ec336ace85510 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 15 Dec 2023 12:38:10 +0100 Subject: [PATCH] new CMake.ctest() method (#15282) --- conan/tools/cmake/cmake.py | 33 ++++++++++++++++++- .../functional/toolchains/cmake/test_cmake.py | 5 +-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/conan/tools/cmake/cmake.py b/conan/tools/cmake/cmake.py index 31e50ae7fe7..c648bc7a2b4 100644 --- a/conan/tools/cmake/cmake.py +++ b/conan/tools/cmake/cmake.py @@ -245,6 +245,38 @@ def test(self, build_type=None, target=None, cli_args=None, build_tool_args=None self._build(build_type=build_type, target=target, cli_args=cli_args, build_tool_args=build_tool_args, env=env, stdout=stdout, stderr=stderr) + def ctest(self, cli_args=None, env="", stdout=None, stderr=None): + """ + Equivalent to running ctest ... + + :param cli_args: Extra ctest command line arguments + :param env: the environment files to activate, by default conanbuild + conanrun + :param stdout: Use it to redirect stdout to this stream + :param stderr: Use it to redirect stderr to this stream + """ + if self._conanfile.conf.get("tools.build:skip_test", check_type=bool): + return + + arg_list = [] + bt = self._conanfile.settings.get_safe("build_type") + is_multi = is_multi_configuration(self._generator) + if bt and is_multi: + arg_list.append(f"--build-config {bt}") + njobs = build_jobs(self._conanfile) + if njobs: + arg_list.append(f"--parallel {njobs}") + + verbosity = self._conanfile.conf.get("tools.build:verbosity", choices=("quiet", "verbose")) + if verbosity: + arg_list.append(f"--{verbosity}") + + arg_list.extend(cli_args or []) + arg_list = " ".join(filter(None, arg_list)) + command = f"ctest {arg_list}" + + env = ["conanbuild", "conanrun"] if env == "" else env + self._conanfile.run(command, env=env, stdout=stdout, stderr=stderr) + @property def _compilation_verbosity_arg(self): """ @@ -266,4 +298,3 @@ def _cmake_log_levels_args(self): if log_level == "quiet": log_level = "error" return ["--loglevel=" + log_level.upper()] if log_level is not None else [] - diff --git a/conans/test/functional/toolchains/cmake/test_cmake.py b/conans/test/functional/toolchains/cmake/test_cmake.py index 38f7319fd30..40aabeb0c47 100644 --- a/conans/test/functional/toolchains/cmake/test_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_cmake.py @@ -7,7 +7,6 @@ import pytest from parameterized.parameterized import parameterized -from conans.model.recipe_ref import RecipeReference from conans.test.assets.cmake import gen_cmakelists from conans.test.assets.sources import gen_function_cpp, gen_function_h from conans.test.functional.utils import check_vs_runtime, check_exe_run @@ -624,6 +623,7 @@ def build(self): cmake.configure() cmake.build() cmake.test() + cmake.ctest() """) cmakelist = textwrap.dedent(""" @@ -648,7 +648,8 @@ def build(self): # The create flow must work c.run("create . --name=pkg --version=0.1 -pr:b=default -o test*:shared=True") - assert "1/1 Test #1: example .......................... Passed" in c.out + assert str(c.out).count("1/1 Test #1: example .......................... Passed") == 2 + assert "pkg/0.1: RUN: ctest --build-config Release --parallel" @pytest.mark.tool("cmake")