Skip to content

#175 Do not allow requires.add (KB-H044) #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ jobs:
env: TOXENV=py38-conancurrent
dist: xenial

- stage: Linux - Conan 1.21
- stage: Linux - Conan 1.23
name: Python 3.5
python: 3.5
env: TOXENV=py35-conan121
env: TOXENV=py35-conan123
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conan121
env: TOXENV=py38-conan123
dist: xenial

- stage: Linux - Conan 1.20
- stage: Linux - Conan 1.22
name: Python 3.5
python: 3.5
env: TOXENV=py35-conan120
env: TOXENV=py35-conan122
dist: xenial
- name: Python 3.8
python: 3.8
env: TOXENV=py38-conan120
env: TOXENV=py38-conan122
dist: xenial

# Macos is slow, only if everything has passed
Expand Down
8 changes: 4 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ environment:
- PYVER: py38
TOXENV: py38-conancurrent

# Conan 1.21.x
# Conan 1.23.x
- PYVER: py38
TOXENV: py38-conan121
TOXENV: py38-conan123

# Conan 1.20.x
# Conan 1.22.x
- PYVER: py38
TOXENV: py38-conan120
TOXENV: py38-conan122

install:
- set PATH=%PATH%;%PYTHON%/Scripts/
Expand Down
9 changes: 9 additions & 0 deletions hooks/conan-center.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"KB-H034": "TEST PACKAGE - NO IMPORTS()",
"KB-H037": "NO AUTHOR",
"KB-H040": "NO TARGET NAME",
"KB-H044": "NO REQUIRES.ADD()",
}


Expand Down Expand Up @@ -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-H044", output)
def test(out):
for forbidden in ["self.requires.add", "self.build_requires.add"]:
if forbidden in conanfile_content:
out.error("The method '{}()' is not allowed. Use '{}()' instead."
.format(forbidden, forbidden.replace(".add", "")))


@raise_if_error_output
def post_export(output, conanfile, conanfile_path, reference, **kwargs):
export_folder_path = os.path.dirname(conanfile_path)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_hooks/conan-center/test_conan-center.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,54 @@ def test(self):
self.assertIn("ERROR: [TEST PACKAGE - NO IMPORTS() (KB-H034)] The method `imports` is not " \
"allowed in test_package/conanfile.py", output)

def test_requirements_add(self):
conanfile = textwrap.dedent("""\
from conans import ConanFile
class AConan(ConanFile):
pass
""")
tools.save('conanfile.py', content=conanfile)
output = self.conan(['create', '.', 'name/version@user/test'])
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)

conanfile = textwrap.dedent("""\
from conans import ConanFile
class AConan(ConanFile):
def requirements(self):
{}
""")

tools.save('conanfile.py',
content=conanfile.replace("{}", 'self.requires("name/version@user/test")'))
output = self.conan(['create', '.', 'foo/version@user/test'])
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)

tools.save('conanfile.py',
content=conanfile.replace("{}", 'self.requires.add("name/version@user/test")'))
output = self.conan(['create', '.', 'foo/version@user/test'])
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] The method 'self.requires.add()' is not " \
"allowed. Use 'self.requires()' instead.", output)

conanfile = textwrap.dedent("""\
from conans import ConanFile
class AConan(ConanFile):
def build_requirements(self):
{}
""")

tools.save('conanfile.py',
content=conanfile.replace("{}", 'self.build_requires("name/version@user/test")'))
output = self.conan(['create', '.', 'foo/version@user/test'])
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)

# Conan >= 1.23 requires "context" parameter for build_requires.add()
if Version(conan_version) < "1.23":
tools.save('conanfile.py',
content=conanfile.replace("{}", 'self.build_requires.add("name/version@user/test")'))
output = self.conan(['create', '.', 'foo/version@user/test'])
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] The method 'self.build_requires.add()' is not " \
"allowed. Use 'self.build_requires()' instead.", output)

def test_no_author(self):
conanfile = textwrap.dedent("""\
from conans import ConanFile
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[tox]
skipsdist=True
envlist =
py{27,35,37,38}-conan{dev,latest,120,121}
py{27,35,37,38}-conan{dev,latest,122,123}

[testenv]
deps =
conandev: https://github.com/conan-io/conan/archive/develop.tar.gz
conancurrent: conan
conan121: conan>=1.21,<1.22
conan120: conan>=1.20,<1.21
conan123: conan>=1.23,<1.24
conan122: conan>=1.22,<1.23
coverage: coverage-enable-subprocess
-r {toxinidir}/tests/requirements_test.txt

Expand Down