From 6a115ad56ef53204b9d3b712fd867da1ec6bc87b Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Fri, 24 Apr 2020 14:41:58 -0300 Subject: [PATCH 1/2] #182 Valid check_min_cppstd Signed-off-by: Uilian Ries --- hooks/conan-center.py | 9 +++++++++ .../conan-center/test_conan-center.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 3c283b7c..672726f4 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -43,6 +43,7 @@ "KB-H034": "TEST PACKAGE - NO IMPORTS()", "KB-H037": "NO AUTHOR", "KB-H040": "NO TARGET NAME", + "KB-H046": "VALIDATE CHECK_MIN_CPPSTD", } @@ -384,6 +385,14 @@ 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-H046", output) + def test(out): + if "check_min_cppstd" in conanfile_content and \ + (not 'settings.get_safe("compiler.cppstd")' in conanfile_content and \ + not "settings.get_safe('compiler.cppstd')" in conanfile_content): + out.error("'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." + " Check if 'cppstd' is configured before 'check_min_cppstd'.") + @raise_if_error_output def post_export(output, conanfile, conanfile_path, reference, **kwargs): export_folder_path = os.path.dirname(conanfile_path) diff --git a/tests/test_hooks/conan-center/test_conan-center.py b/tests/test_hooks/conan-center/test_conan-center.py index 7943a424..8c98a7c2 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -597,3 +597,21 @@ def package_info(self): tools.save('conanfile.py', content=conanfile.replace("{}", it)) 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}", "")) + output = self.conan(['create', '.', 'name/version@user/test']) + self.assertIn("ERROR: [VALIDATE CHECK_MIN_CPPSTD (KB-H046)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." + " Check if 'cppstd' is configured before 'check_min_cppstd'.", output) From 27b28cc1b5cd39f9b2c839fec41db8e51ab67740 Mon Sep 17 00:00:00 2001 From: Uilian Ries Date: Fri, 24 Apr 2020 15:14:35 -0300 Subject: [PATCH 2/2] #182 Validate check_min_cppstd Signed-off-by: Uilian Ries --- hooks/conan-center.py | 24 +++++++++++++++---- .../conan-center/test_conan-center.py | 6 +++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/hooks/conan-center.py b/hooks/conan-center.py index 672726f4..e7275f7c 100644 --- a/hooks/conan-center.py +++ b/hooks/conan-center.py @@ -387,11 +387,25 @@ def test(out): @run_test("KB-H046", output) def test(out): - if "check_min_cppstd" in conanfile_content and \ - (not 'settings.get_safe("compiler.cppstd")' in conanfile_content and \ - not "settings.get_safe('compiler.cppstd')" in conanfile_content): - out.error("'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first." - " Check if 'cppstd' is configured before 'check_min_cppstd'.") + 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'.") + @raise_if_error_output def post_export(output, conanfile, conanfile_path, reference, **kwargs): diff --git a/tests/test_hooks/conan-center/test_conan-center.py b/tests/test_hooks/conan-center/test_conan-center.py index 8c98a7c2..6c1441b1 100644 --- a/tests/test_hooks/conan-center/test_conan-center.py +++ b/tests/test_hooks/conan-center/test_conan-center.py @@ -611,6 +611,12 @@ def configure(self): 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-H046)] '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-H046)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first."