From e6aa291901511b14dbb5a42d9e39f05c4bdd8cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Thu, 7 Dec 2023 15:04:23 +0100 Subject: [PATCH] Add `in` operator support for ConanFile's `self.dependencies` (#15221) * Add in operator support for ConanFile self.dependencies * Ensure tool_requires also work --- conans/model/dependencies.py | 11 ++++++++++ .../graph/test_dependencies_visit.py | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/conans/model/dependencies.py b/conans/model/dependencies.py index c65d4648b4c..01f4afbcda1 100644 --- a/conans/model/dependencies.py +++ b/conans/model/dependencies.py @@ -73,6 +73,17 @@ def items(self): def values(self): return self._data.values() + def __contains__(self, item): + try: + self.get(item) + return True + except KeyError: + return False + except ConanException: + # ConanException is raised when there are more than one matching the filters + # so it's definitely in the dict + return True + class ConanFileDependencies(UserRequirementsDict): diff --git a/conans/test/integration/graph/test_dependencies_visit.py b/conans/test/integration/graph/test_dependencies_visit.py index 75c9fda1402..3bb77dc92d4 100644 --- a/conans/test/integration/graph/test_dependencies_visit.py +++ b/conans/test/integration/graph/test_dependencies_visit.py @@ -29,6 +29,18 @@ def generate(self): self.output.info("DefPRefBuild: {}!!!".format(dep.pref.repr_notime())) for r, d in self.dependencies.build.items(): self.output.info("DIRECTBUILD {}: {}".format(r.direct, d)) + + if "openssl" in self.dependencies: + self.output.info("OpenSSL found in deps") + + if "cmake" in self.dependencies: + self.output.info("cmake found in default deps") + + if "cmake" in self.dependencies.build: + self.output.info("cmake found in deps.build") + + if "badlib" in self.dependencies: + self.output.info("badlib found in deps") """) client.save({"conanfile.py": conanfile}) @@ -48,6 +60,12 @@ def generate(self): assert "conanfile.py: DIRECTBUILD True: cmake/0.1" in client.out assert "conanfile.py: DIRECTBUILD False: openssl/0.2" in client.out + assert "OpenSSL found in deps" in client.out + assert "badlib found in deps" not in client.out + + assert "cmake found in default deps" not in client.out + assert "cmake found in deps.build" in client.out + def test_dependencies_visit_settings_options(): client = TestClient() @@ -89,6 +107,9 @@ def generate(self): '- cmake/0.2, Traits: build=True, headers=False, libs=False, run=False, visible=False' ), ('self.dependencies["missing"]', True, "'missing' not found in the dependency set"), + ('self.output.info("Missing in deps: " + str("missing" in self.dependencies))', False, "Missing in deps: False"), + ('self.output.info("Zlib in deps: " + str("zlib" in self.dependencies))', False, "Zlib in deps: True"), + ('self.output.info("Zlib in deps.build: " + str("zlib" in self.dependencies.build))', False, "Zlib in deps.build: True"), ]