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

Honour check_type parameter to from CMakeDeps & PkgConfigDeps get_property #17052

Merged
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
6 changes: 5 additions & 1 deletion conan/tools/cmake/cmakedeps/cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def get_property(self, prop, dep, comp_name=None, check_type=None):
dep_name) in self.build_context_activated and dep.context == "build" else ""
dep_comp = f"{str(dep_name)}::{comp_name}" if comp_name else f"{str(dep_name)}"
try:
return self._properties[f"{dep_comp}{build_suffix}"][prop]
value = self._properties[f"{dep_comp}{build_suffix}"][prop]
if check_type is not None and not isinstance(value, check_type):
raise ConanException(
f'The expected type for {prop} is "{check_type.__name__}", but "{type(value).__name__}" was found')
return value
except KeyError:
return dep.cpp_info.get_property(prop, check_type=check_type) if not comp_name \
else dep.cpp_info.components[comp_name].get_property(prop, check_type=check_type)
Expand Down
6 changes: 5 additions & 1 deletion conan/tools/gnu/pkgconfigdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ def get_property(self, prop, dep, comp_name=None, check_type=None):
dep_name = dep.ref.name
dep_comp = f"{str(dep_name)}::{comp_name}" if comp_name else f"{str(dep_name)}"
try:
return self._properties[f"{dep_comp}{self._suffix}"][prop]
value = self._properties[f"{dep_comp}{self._suffix}"][prop]
if check_type is not None and not isinstance(value, check_type):
raise ConanException(
f'The expected type for {prop} is "{check_type.__name__}", but "{type(value).__name__}" was found')
return value
except KeyError:
return dep.cpp_info.get_property(prop, check_type=check_type) if not comp_name \
else dep.cpp_info.components[comp_name].get_property(prop, check_type=check_type)
Expand Down
24 changes: 24 additions & 0 deletions test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,3 +819,27 @@ def package_info(self):
# FIXME: Depending on itself, this doesn't look good
assert 'set_property(TARGET dep::dep APPEND PROPERTY ' \
'INTERFACE_LINK_LIBRARIES dep::dep)' in cmake_target


def test_cmakedeps_set_get_property_checktype():
c = TestClient()
app = textwrap.dedent("""\
from conan import ConanFile
from conan.tools.cmake import CMakeDeps
class Pkg(ConanFile):
name = "app"
version = "0.1"
settings = "build_type"
requires = "dep/0.1"
def generate(self):
deps = CMakeDeps(self)
deps.set_property("dep", "foo", 1)
deps.get_property("foo", self.dependencies["dep"], check_type=list)
deps.generate()
""")

c.save({"dep/conanfile.py": GenConanfile("dep", "0.1"),
"app/conanfile.py": app})
c.run("create dep")
c.run("create app", assert_error=True)
assert 'The expected type for foo is "list", but "int" was found' in c.out