Skip to content

Commit 27b28cc

Browse files
committed
conan-io#182 Validate check_min_cppstd
Signed-off-by: Uilian Ries <[email protected]>
1 parent 6a115ad commit 27b28cc

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

hooks/conan-center.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,25 @@ def test(out):
387387

388388
@run_test("KB-H046", output)
389389
def test(out):
390-
if "check_min_cppstd" in conanfile_content and \
391-
(not 'settings.get_safe("compiler.cppstd")' in conanfile_content and \
392-
not "settings.get_safe('compiler.cppstd')" in conanfile_content):
393-
out.error("'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first."
394-
" Check if 'cppstd' is configured before 'check_min_cppstd'.")
390+
def _which_line_is(content, lines):
391+
n = 0
392+
for line in lines:
393+
n += 1
394+
if content in line:
395+
return n
396+
return 0
397+
398+
lines = conanfile_content.splitlines()
399+
check_line = _which_line_is("check_min_cppstd", lines)
400+
if check_line:
401+
if_1_line = _which_line_is('settings.get_safe("compiler.cppstd")', lines)
402+
if_2_line = _which_line_is("settings.get_safe('compiler.cppstd')", lines)
403+
if (not if_1_line and not if_2_line) or \
404+
(if_1_line and if_1_line > check_line) or \
405+
(if_2_line and if_2_line > check_line):
406+
out.error("'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first."
407+
" Check if 'cppstd' is configured before 'check_min_cppstd'.")
408+
395409

396410
@raise_if_error_output
397411
def post_export(output, conanfile, conanfile_path, reference, **kwargs):

tests/test_hooks/conan-center/test_conan-center.py

+6
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,12 @@ def configure(self):
611611
output = self.conan(['create', '.', 'name/version@user/test'])
612612
self.assertIn("[VALIDATE CHECK_MIN_CPPSTD (KB-H046)] OK", output)
613613

614+
tools.save('conanfile.py', content=conanfile.replace("{0}", 'tools.check_min_cppstd(self, "11")')
615+
.replace("{1}", "if self.settings.get_safe(\"compiler.cppstd\"):\n pass"))
616+
output = self.conan(['create', '.', 'name/version@user/test'])
617+
self.assertIn("ERROR: [VALIDATE CHECK_MIN_CPPSTD (KB-H046)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first."
618+
" Check if 'cppstd' is configured before 'check_min_cppstd'.", output)
619+
614620
tools.save('conanfile.py', content=conanfile.replace("{0}", 'tools.check_min_cppstd(self, "11")').replace("{1}", ""))
615621
output = self.conan(['create', '.', 'name/version@user/test'])
616622
self.assertIn("ERROR: [VALIDATE CHECK_MIN_CPPSTD (KB-H046)] 'tools.check_min_cppstd requires 'if self.settings.get_safe(\"compiler.cppstd\")' first."

0 commit comments

Comments
 (0)