From d819621f337eb2eb436ea64115271c7d28aa4d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Thu, 19 Dec 2024 15:41:24 +0100 Subject: [PATCH] Guard against unknown languages attribute values (#17512) Guard against languages misstype --- conans/model/conan_file.py | 2 ++ .../conanfile/conanfile_errors_test.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/conans/model/conan_file.py b/conans/model/conan_file.py index 27001f20fd1..ac84185159a 100644 --- a/conans/model/conan_file.py +++ b/conans/model/conan_file.py @@ -95,6 +95,8 @@ def __init__(self, display_name=""): self.generators = [self.generators] if isinstance(self.languages, str): self.languages = [self.languages] + if not all(lang == "C" or lang == "C++" for lang in self.languages): + raise ConanException("Only 'C' and 'C++' languages are allowed in 'languages' attribute") if isinstance(self.settings, str): self.settings = [self.settings] self.requires = Requirements(self.requires, self.build_requires, self.test_requires, diff --git a/test/integration/conanfile/conanfile_errors_test.py b/test/integration/conanfile/conanfile_errors_test.py index 5c59e782d66..057589595de 100644 --- a/test/integration/conanfile/conanfile_errors_test.py +++ b/test/integration/conanfile/conanfile_errors_test.py @@ -240,3 +240,22 @@ def package_info(self): tc.run("create cmake") tc.run("create . -b=missing -pr=profile") assert "cmake/1.0 -> True" not in tc.out + + +@pytest.mark.parametrize("languages", [ + "['C', 'CXX']", + "['CXX']", + "'CXX'", +]) +def test_language_unexpected(languages): + tc = TestClient(light=True) + tc.save({"conanfile.py": GenConanfile().with_class_attribute(f"languages = {languages}")}) + tc.run("inspect .", assert_error=True) + assert "Only 'C' and 'C++' languages are allowed in 'languages' attribute" in tc.out + + +def test_empty_languages(): + tc = TestClient(light=True) + tc.save({"conanfile.py": GenConanfile().with_class_attribute("languages = []")}) + tc.run("inspect .") + assert "Only 'C' and 'C++' languages are allowed in 'languages' attribute" not in tc.out