Skip to content

Commit

Permalink
new CMake.ctest() method (conan-io#15282)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Dec 15, 2023
1 parent 053f22a commit 018441d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
33 changes: 32 additions & 1 deletion conan/tools/cmake/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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 []

5 changes: 3 additions & 2 deletions conans/test/functional/toolchains/cmake/test_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -624,6 +623,7 @@ def build(self):
cmake.configure()
cmake.build()
cmake.test()
cmake.ctest()
""")

cmakelist = textwrap.dedent("""
Expand All @@ -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")
Expand Down

0 comments on commit 018441d

Please sign in to comment.