diff --git a/hooks/conan-center.py b/hooks/conan-center.py index b0b7b52f..51774338 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -70,6 +70,7 @@ "KB-H064": "INVALID TOPICS", "KB-H065": "NO REQUIRED_CONAN_VERSION", "KB-H066": "SHORT_PATHS USAGE", + "KB-H067": "VALIDATE CHECK_MIN_CPPSTD", } @@ -442,6 +443,27 @@ def test(out): "Conanfile should not contain 'self.cpp_info.names['{0}']'. " " Use 'cmake_find_package' and 'cmake_find_package_multi' instead.".format(generator)) + @run_test("KB-H067", output) + def test(out): + def _which_line_is(content, lines): + n = 0 + for line in lines: + n += 1 + if content in line: + return n + return 0 + + lines = conanfile_content.splitlines() + check_line = _which_line_is("check_min_cppstd", lines) + if check_line: + if_1_line = _which_line_is('settings.get_safe("compiler.cppstd")', lines) + if_2_line = _which_line_is("settings.get_safe('compiler.cppstd')", lines) + if (not if_1_line and not if_2_line) or \ + (if_1_line and if_1_line > check_line) or \ + (if_2_line and if_2_line > check_line): + out.error("'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." + " Check if 'cppstd' is configured before 'check_min_cppstd'.") + @run_test("KB-H041", output) def test(out): checked_fileexts = ".c", ".cc", ".cpp", ".cxx", ".h", ".hxx", ".hpp", \ diff --git a/tests/test_hooks/conan-center/test_conan-center.py b/tests/test_hooks/conan-center/test_conan-center.py index 8f8e7cc2..7e5c635b 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -732,6 +732,30 @@ def package_info(self): output = self.conan(['create', '.', 'name/version@user/test']) self.assertIn("[NO TARGET NAME (KB-H040)] OK", output) + def test_check_min_cppstd(self): + conanfile = textwrap.dedent("""\ + from conans import ConanFile, tools + class AConan(ConanFile): + settings = "compiler" + def configure(self): + {0} + {1} + """) + tools.save('conanfile.py', content=conanfile.replace("{0}", 'if self.settings.get_safe("compiler.cppstd"):').replace("{1}", ' tools.check_min_cppstd(self, "11")')) + output = self.conan(['create', '.', 'name/version@user/test']) + self.assertIn("[VALIDATE CHECK_MIN_CPPSTD (KB-H046)] OK", output) + + tools.save('conanfile.py', content=conanfile.replace("{0}", 'tools.check_min_cppstd(self, "11")') + .replace("{1}", "if self.settings.get_safe(\"compiler.cppstd\"):\n pass")) + output = self.conan(['create', '.', 'name/version@user/test']) + self.assertIn("ERROR: [VALIDATE CHECK_MIN_CPPSTD (KB-H067)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." + " Check if 'cppstd' is configured before 'check_min_cppstd'.", output) + + tools.save('conanfile.py', content=conanfile.replace("{0}", 'tools.check_min_cppstd(self, "11")').replace("{1}", "")) + output = self.conan(['create', '.', 'name/version@user/test']) + self.assertIn("ERROR: [VALIDATE CHECK_MIN_CPPSTD (KB-H067)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." + " Check if 'cppstd' is configured before 'check_min_cppstd'.", output) + def test_cmake_verbose_makefile(self): conanfile = self.conanfile_base.format(placeholder="exports_sources = \"CMakeLists.txt\"")