From 4fbb384efe910a5c268cd944d39d02e942c9cdcc Mon Sep 17 00:00:00 2001 From: Ernesto de Gracia Herranz Date: Tue, 20 Aug 2024 16:56:56 +0200 Subject: [PATCH] Improve error when accessing cpp_info shorthand methods. (#16847) --- conans/model/build_info.py | 18 +++++++++----- .../conanfile/conanfile_errors_test.py | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/conans/model/build_info.py b/conans/model/build_info.py index fa50ed17056..34a9d5c0542 100644 --- a/conans/model/build_info.py +++ b/conans/model/build_info.py @@ -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 diff --git a/test/integration/conanfile/conanfile_errors_test.py b/test/integration/conanfile/conanfile_errors_test.py index 028c974ba91..c10dc1c0bf0 100644 --- a/test/integration/conanfile/conanfile_errors_test.py +++ b/test/integration/conanfile/conanfile_errors_test.py @@ -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 +