Skip to content

Commit

Permalink
Improve error when accessing cpp_info shorthand methods. (#16847)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErniGH authored Aug 20, 2024
1 parent 9768d04 commit 4fbb384
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions conans/model/build_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,28 @@ def frameworkdirs(self, value):
@property
def bindir(self):
bindirs = self.bindirs
assert bindirs
assert len(bindirs) == 1
if not bindirs or len(bindirs) != 1:
raise ConanException(f"The bindir property is undefined because bindirs "
f"{'is empty' if not bindirs else 'has more than one element'}."
f" Consider using the bindirs property.")
return bindirs[0]

@property
def libdir(self):
libdirs = self.libdirs
assert libdirs
assert len(libdirs) == 1
if not libdirs or len(libdirs) != 1:
raise ConanException(f"The libdir property is undefined because libdirs "
f"{'is empty' if not libdirs else 'has more than one element'}."
f" Consider using the libdirs property.")
return libdirs[0]

@property
def includedir(self):
includedirs = self.includedirs
assert includedirs
assert len(includedirs) == 1
if not includedirs or len(includedirs) != 1:
raise ConanException(f"The includedir property is undefined because includedirs "
f"{'is empty' if not includedirs else 'has more than one element'}."
f" Consider using the includedirs property.")
return includedirs[0]

@property
Expand Down
24 changes: 24 additions & 0 deletions test/integration/conanfile/conanfile_errors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,27 @@ def requirements(self):
c.save({"conanfile.py": conanfile})
c.run("create .", assert_error=True)
assert "ERROR: hello/0.1: Dependencies options were defined incorrectly." in c.out


@pytest.mark.parametrize("property_name", ["libdir", "bindir", "includedir"])
@pytest.mark.parametrize("property_content", [[], ["mydir1", "mydir2"]])
def test_shorthand_bad_interface(property_name, property_content):
c = TestClient(light=True)
conanfile = textwrap.dedent(f"""
from conan import ConanFile
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
def package_info(self):
self.cpp_info.{property_name}s = {property_content}
self.output.info(self.cpp_info.{property_name})
""")
c.save({"conanfile.py": conanfile})
c.run("create .", assert_error=True)
if property_content:
assert f"The {property_name} property is undefined because {property_name}s has more than one element." in c.out
else:
assert f"The {property_name} property is undefined because {property_name}s is empty." in c.out

0 comments on commit 4fbb384

Please sign in to comment.