Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new CMake.ctest() method #15282

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this assertion can never fail, as it should end with in c.out.

Thanks for adding this feature though 👍



@pytest.mark.tool("cmake")
Expand Down