From b9d14c3bafebbe6470c583bda598ac18a868977e Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 16 Nov 2021 19:17:23 +0100 Subject: [PATCH] adding test_requires() for 2.0 migration of force_host_context --- conans/client/graph/graph_manager.py | 9 ++++++ .../graph/core/test_build_requires.py | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/conans/client/graph/graph_manager.py b/conans/client/graph/graph_manager.py index 68159a59554..8ef2708ffbe 100644 --- a/conans/client/graph/graph_manager.py +++ b/conans/client/graph/graph_manager.py @@ -298,6 +298,15 @@ def _resolve_graph(self, root_node, profile_host, profile_build, graph_lock, bui @staticmethod def _get_recipe_build_requires(conanfile, default_context): conanfile.build_requires = _RecipeBuildRequires(conanfile, default_context) + + class TestRequirements: + def __init__(self, build_requires): + self._build_requires = build_requires + + def __call__(self, ref): + self._build_requires(ref, force_host_context=True) + + conanfile.test_requires = TestRequirements(conanfile.build_requires) if hasattr(conanfile, "build_requirements"): with get_env_context_manager(conanfile): with conanfile_exception_formatter(str(conanfile), "build_requirements"): diff --git a/conans/test/integration/graph/core/test_build_requires.py b/conans/test/integration/graph/core/test_build_requires.py index b6930e2912c..56348f9dd22 100644 --- a/conans/test/integration/graph/core/test_build_requires.py +++ b/conans/test/integration/graph/core/test_build_requires.py @@ -1,3 +1,5 @@ +import textwrap + import six from parameterized import parameterized @@ -399,3 +401,29 @@ def test_build_require_link_order(self): dependents=[], closure=[gazelle, grass]) self.assertListEqual(list(cheetah.conanfile.deps_cpp_info.libs), ['mylibgazelle0.1lib', 'mylibgrass0.1lib']) + + def test_test_require(self): + # app -(tr)-> gtest/0.1 + # This test should survive in 2.0 + tool_ref = ConanFileReference.loads("gtest/0.1") + self._cache_recipe(tool_ref, GenConanfile("gtest", "0.1")) + + conanfile = textwrap.dedent(""" + from conans import ConanFile + class Pkg(ConanFile): + name = "app" + version = "0.1" + def build_requirements(self): + self.test_requires("gtest/0.1") + """) + deps_graph = self.build_graph(conanfile) + + # Build requires always apply to the consumer + self.assertEqual(2, len(deps_graph.nodes)) + app = deps_graph.root + tool = app.dependencies[0].dst + + self._check_node(app, "app/0.1@", deps=[], build_deps=[tool], dependents=[], + closure=[tool]) + self._check_node(tool, "gtest/0.1#123", deps=[], build_deps=[], + dependents=[app], closure=[])