Skip to content

Commit

Permalink
Add in operator support for ConanFile's self.dependencies (#15221)
Browse files Browse the repository at this point in the history
* Add in operator support for ConanFile self.dependencies

* Ensure tool_requires also work
  • Loading branch information
AbrilRBS authored Dec 7, 2023
1 parent a6f42bf commit e6aa291
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions conans/model/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
21 changes: 21 additions & 0 deletions conans/test/integration/graph/test_dependencies_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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()
Expand Down Expand Up @@ -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"),
]


Expand Down

0 comments on commit e6aa291

Please sign in to comment.