Skip to content

Commit 935e847

Browse files
committed
conan-io#123 Custom attributes must be protected
Signed-off-by: Uilian Ries <[email protected]>
1 parent 6fbbfde commit 935e847

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

hooks/conan-center.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import yaml
88
from logging import WARNING, ERROR, INFO, DEBUG, NOTSET
99

10-
from conans import tools, Settings
10+
from conans import tools, Settings, ConanFile
1111

1212
kb_errors = {"KB-H001": "DEPRECATED GLOBAL CPPSTD",
1313
"KB-H002": "REFERENCE LOWERCASE",
@@ -37,7 +37,9 @@
3737
"KB-H028": "CMAKE MINIMUM VERSION",
3838
"KB-H029": "TEST PACKAGE - RUN ENVIRONMENT",
3939
"KB-H030": "CONANDATA.YML FORMAT",
40-
"KB-H031": "CONANDATA.YML REDUCE"}
40+
"KB-H031": "CONANDATA.YML REDUCE",
41+
"KB-H034": "CUSTOM ATTRIBUTES",
42+
}
4143

4244

4345
class _HooksOutputErrorCollector(object):
@@ -318,6 +320,19 @@ def _not_allowed_entries(info, allowed_entries):
318320
"conandata.yml" % (entries, version))
319321
return
320322

323+
@run_test("KB-H034", output)
324+
def test(out):
325+
mock = ConanFile(conanfile.output, None)
326+
valid_attrs = [attr for attr in dir(mock) if not callable(attr)] + ['conan_data', 'python_requires']
327+
current_attrs = [attr for attr in dir(conanfile) if not callable(attr)]
328+
invalid_attrs = []
329+
for attr in current_attrs:
330+
if not attr.startswith("_") and attr not in valid_attrs:
331+
invalid_attrs.append(attr)
332+
if invalid_attrs:
333+
out.error("Custom attributes must be declared as protected. " \
334+
"The follow attributes are invalid: '{}'".format("', '".join(invalid_attrs)))
335+
321336

322337
@raise_if_error_output
323338
def post_export(output, conanfile, conanfile_path, reference, **kwargs):

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

+30
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def test_conanfile(self):
8888
self.assertIn("ERROR: [CONAN CENTER INDEX URL (KB-H027)] The attribute 'url' should " \
8989
"point to: https://github.com/conan-io/conan-center-index", output)
9090
self.assertIn("[CMAKE MINIMUM VERSION (KB-H028)] OK", output)
91+
self.assertIn("[CUSTOM ATTRIBUTES (KB-H034)] OK", output)
9192

9293
def test_conanfile_header_only(self):
9394
tools.save('conanfile.py', content=self.conanfile_header_only)
@@ -109,6 +110,7 @@ def test_conanfile_header_only(self):
109110
"recipe", output)
110111
self.assertIn("[META LINES (KB-H025)] OK", output)
111112
self.assertIn("[CMAKE MINIMUM VERSION (KB-H028)] OK", output)
113+
self.assertIn("[CUSTOM ATTRIBUTES (KB-H034)] OK", output)
112114

113115
def test_conanfile_header_only_with_settings(self):
114116
tools.save('conanfile.py', content=self.conanfile_header_only_with_settings)
@@ -129,6 +131,7 @@ def test_conanfile_header_only_with_settings(self):
129131
"recipe", output)
130132
self.assertIn("[META LINES (KB-H025)] OK", output)
131133
self.assertIn("[CMAKE MINIMUM VERSION (KB-H028)] OK", output)
134+
self.assertIn("[CUSTOM ATTRIBUTES (KB-H034)] OK", output)
132135

133136
def test_conanfile_installer(self):
134137
tools.save('conanfile.py', content=self.conanfile_installer)
@@ -397,3 +400,30 @@ def test_cmake_minimum_version(self):
397400
self.assertIn("ERROR: [CMAKE MINIMUM VERSION (KB-H028)] The CMake file '%s' must contain a "
398401
"minimum version declared (e.g. cmake_minimum_required(VERSION 3.1.2))" % path,
399402
output)
403+
404+
def test_invalid_recipe_attributes(self):
405+
conanfile = textwrap.dedent("""\
406+
from conans import ConanFile
407+
408+
class AConan(ConanFile):
409+
url = "fake_url.com"
410+
license = "fake_license"
411+
description = "whatever"
412+
homepage = "homepage.com"
413+
topics = ("fake_topic", "another_fake_topic")
414+
exports_sources = "header.h"
415+
options = {"foo": [True, False], "bar": [True, False]}
416+
default_options = {"foo": False, "bar": True}
417+
_source_subfolder = "source_subfolder"
418+
_build_subfolder = "build_subfolder"
419+
foobar = "package"
420+
package_subfolder = "package"
421+
422+
def package(self):
423+
self.copy("*", dst="include")
424+
""")
425+
tools.save('conanfile.py', content=conanfile)
426+
output = self.conan(['create', '.', 'name/version@user/test'])
427+
self.assertIn("ERROR: [CUSTOM ATTRIBUTES (KB-H034)] Custom attributes must be declared " \
428+
"as protected. The follow attributes are invalid: 'foobar', " \
429+
"'package_subfolder'", output)

0 commit comments

Comments
 (0)