Skip to content

Commit 5580298

Browse files
authored
Merge pull request #177 from uilianries/feature/requirements-add
#175 Do not allow requires.add (KB-H044)
2 parents c055fb1 + 8cd47a5 commit 5580298

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ jobs:
3333
env: TOXENV=py38-conancurrent
3434
dist: xenial
3535

36-
- stage: Linux - Conan 1.21
36+
- stage: Linux - Conan 1.23
3737
name: Python 3.5
3838
python: 3.5
39-
env: TOXENV=py35-conan121
39+
env: TOXENV=py35-conan123
4040
dist: xenial
4141
- name: Python 3.8
4242
python: 3.8
43-
env: TOXENV=py38-conan121
43+
env: TOXENV=py38-conan123
4444
dist: xenial
4545

46-
- stage: Linux - Conan 1.20
46+
- stage: Linux - Conan 1.22
4747
name: Python 3.5
4848
python: 3.5
49-
env: TOXENV=py35-conan120
49+
env: TOXENV=py35-conan122
5050
dist: xenial
5151
- name: Python 3.8
5252
python: 3.8
53-
env: TOXENV=py38-conan120
53+
env: TOXENV=py38-conan122
5454
dist: xenial
5555

5656
# Macos is slow, only if everything has passed

appveyor.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ environment:
2020
- PYVER: py38
2121
TOXENV: py38-conancurrent
2222

23-
# Conan 1.21.x
23+
# Conan 1.23.x
2424
- PYVER: py38
25-
TOXENV: py38-conan121
25+
TOXENV: py38-conan123
2626

27-
# Conan 1.20.x
27+
# Conan 1.22.x
2828
- PYVER: py38
29-
TOXENV: py38-conan120
29+
TOXENV: py38-conan122
3030

3131
install:
3232
- set PATH=%PATH%;%PYTHON%/Scripts/

hooks/conan-center.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"KB-H034": "TEST PACKAGE - NO IMPORTS()",
4444
"KB-H037": "NO AUTHOR",
4545
"KB-H040": "NO TARGET NAME",
46+
"KB-H044": "NO REQUIRES.ADD()",
4647
}
4748

4849

@@ -385,6 +386,14 @@ def test(out):
385386
"Conanfile should not contain 'self.cpp_info.names['{0}']'. "
386387
" Use 'cmake_find_package' and 'cmake_find_package_multi' instead.".format(generator))
387388

389+
@run_test("KB-H044", output)
390+
def test(out):
391+
for forbidden in ["self.requires.add", "self.build_requires.add"]:
392+
if forbidden in conanfile_content:
393+
out.error("The method '{}()' is not allowed. Use '{}()' instead."
394+
.format(forbidden, forbidden.replace(".add", "")))
395+
396+
388397
@raise_if_error_output
389398
def post_export(output, conanfile, conanfile_path, reference, **kwargs):
390399
export_folder_path = os.path.dirname(conanfile_path)

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,54 @@ def test(self):
564564
self.assertIn("ERROR: [TEST PACKAGE - NO IMPORTS() (KB-H034)] The method `imports` is not " \
565565
"allowed in test_package/conanfile.py", output)
566566

567+
def test_requirements_add(self):
568+
conanfile = textwrap.dedent("""\
569+
from conans import ConanFile
570+
class AConan(ConanFile):
571+
pass
572+
""")
573+
tools.save('conanfile.py', content=conanfile)
574+
output = self.conan(['create', '.', 'name/version@user/test'])
575+
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)
576+
577+
conanfile = textwrap.dedent("""\
578+
from conans import ConanFile
579+
class AConan(ConanFile):
580+
def requirements(self):
581+
{}
582+
""")
583+
584+
tools.save('conanfile.py',
585+
content=conanfile.replace("{}", 'self.requires("name/version@user/test")'))
586+
output = self.conan(['create', '.', 'foo/version@user/test'])
587+
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)
588+
589+
tools.save('conanfile.py',
590+
content=conanfile.replace("{}", 'self.requires.add("name/version@user/test")'))
591+
output = self.conan(['create', '.', 'foo/version@user/test'])
592+
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] The method 'self.requires.add()' is not " \
593+
"allowed. Use 'self.requires()' instead.", output)
594+
595+
conanfile = textwrap.dedent("""\
596+
from conans import ConanFile
597+
class AConan(ConanFile):
598+
def build_requirements(self):
599+
{}
600+
""")
601+
602+
tools.save('conanfile.py',
603+
content=conanfile.replace("{}", 'self.build_requires("name/version@user/test")'))
604+
output = self.conan(['create', '.', 'foo/version@user/test'])
605+
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] OK", output)
606+
607+
# Conan >= 1.23 requires "context" parameter for build_requires.add()
608+
if Version(conan_version) < "1.23":
609+
tools.save('conanfile.py',
610+
content=conanfile.replace("{}", 'self.build_requires.add("name/version@user/test")'))
611+
output = self.conan(['create', '.', 'foo/version@user/test'])
612+
self.assertIn("[NO REQUIRES.ADD() (KB-H044)] The method 'self.build_requires.add()' is not " \
613+
"allowed. Use 'self.build_requires()' instead.", output)
614+
567615
def test_no_author(self):
568616
conanfile = textwrap.dedent("""\
569617
from conans import ConanFile

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[tox]
22
skipsdist=True
33
envlist =
4-
py{27,35,37,38}-conan{dev,latest,120,121}
4+
py{27,35,37,38}-conan{dev,latest,122,123}
55

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

0 commit comments

Comments
 (0)