Skip to content

Commit fef91f7

Browse files
authored
Move KB-H047 - Non ASCII (#359)
* Discard hook 47 - Non ASCII Signed-off-by: Uilian Ries <[email protected]> * Remove windows 2.7 Signed-off-by: Uilian Ries <[email protected]> * Decouple non-ascii hook Signed-off-by: Uilian Ries <[email protected]> * Remove python 2.7 Signed-off-by: Uilian Ries <[email protected]> * Remove python 2.7 from windows env Signed-off-by: Uilian Ries <[email protected]> * Update output Signed-off-by: Uilian Ries <[email protected]> * Fix windows support Signed-off-by: Uilian Ries <[email protected]>
1 parent 86ac417 commit fef91f7

File tree

6 files changed

+74
-52
lines changed

6 files changed

+74
-52
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ There several environment variables you can use to configure it:
170170

171171
This hook requires additional dependencies to work: `pip install pylint astroid`.
172172

173+
### [Non ASCII](hooks/non_ascii.py)
174+
175+
Separate KB-H047 from Conan Center, which is no longer required due Python 2.7 deprecation.
176+
177+
Validates if `conanfile.py` and `test_package/conanfile.py` contain a non-ascii present, when there is a character, it logs an error.
178+
173179
## License
174180

175181
[MIT License](LICENSE)

hooks/conan-center.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"KB-H044": "NO REQUIRES.ADD()",
4848
"KB-H045": "DELETE OPTIONS",
4949
"KB-H046": "CMAKE VERBOSE MAKEFILE",
50-
"KB-H047": "NO ASCII CHARACTERS",
5150
"KB-H048": "CMAKE VERSION REQUIRED",
5251
"KB-H049": "CMAKE WINDOWS EXPORT ALL SYMBOLS",
5352
"KB-H050": "DEFAULT SHARED OPTION VALUE",
@@ -470,33 +469,6 @@ def test(out):
470469
if "self.options.remove" in conanfile_content:
471470
out.error("Found 'self.options.remove'. Replace it by 'del self.options.<opt>'.")
472471

473-
@run_test("KB-H047", output)
474-
def test(out):
475-
476-
def _check_non_ascii(filename, content):
477-
import unicodedata
478-
for num, line in enumerate(content.splitlines(), 1):
479-
bad_chars = {num: char for num, char in enumerate(line, 1) if ord(char) >= 128}
480-
if bad_chars:
481-
out.error("The file '{}' contains a non-ascii character at line ({})."
482-
" Only ASCII characters are allowed, please remove it.".format(filename, num))
483-
indexes = bad_chars.keys()
484-
draw = ['^' if i in indexes else ' ' for i in range(1, len(line))]
485-
draw = ''.join(draw)
486-
bad_chars = bad_chars.values()
487-
bad_chars = ["\\x%s (%s)" % (format(ord(c), 'x'), unicodedata.name(c)) for c in bad_chars]
488-
message = "bad characters: " + ' '.join(bad_chars)
489-
output.info(message)
490-
output.info(line)
491-
output.info(draw)
492-
493-
_check_non_ascii("conanfile.py", conanfile_content)
494-
test_package_dir = os.path.join(os.path.dirname(conanfile_path), "test_package")
495-
test_package_path = os.path.join(test_package_dir, "conanfile.py")
496-
if os.path.exists(test_package_path):
497-
test_package_content = tools.load(test_package_path)
498-
_check_non_ascii("test_package/conanfile.py", test_package_content)
499-
500472
@run_test("KB-H046", output)
501473
def test(out):
502474

hooks/non_ascii.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import unicodedata
5+
from conans import tools
6+
7+
8+
def check_non_ascii(filename, content, output):
9+
for num, line in enumerate(content.splitlines(), 1):
10+
bad_chars = {num: char for num, char in enumerate(line, 1) if ord(char) >= 128}
11+
if bad_chars:
12+
output.error("The file '{}' contains a non-ascii character at line ({})."
13+
" Only ASCII characters are allowed, please remove it.".format(filename, num))
14+
indexes = bad_chars.keys()
15+
draw = ['^' if i in indexes else ' ' for i in range(1, len(line))]
16+
draw = ''.join(draw)
17+
bad_chars = bad_chars.values()
18+
bad_chars = ["\\x%s (%s)" % (format(ord(c), 'x'), unicodedata.name(c)) for c in bad_chars]
19+
message = "bad characters: " + ' '.join(bad_chars)
20+
output.info(message)
21+
output.info(line)
22+
output.info(draw)
23+
24+
25+
def pre_export(output, conanfile, conanfile_path, reference, **kwargs):
26+
conanfile_content = tools.load(conanfile_path)
27+
check_non_ascii("conanfile.py", conanfile_content, output)
28+
test_package_dir = os.path.join(os.path.dirname(conanfile_path), "test_package")
29+
test_package_path = os.path.join(test_package_dir, "conanfile.py")
30+
if os.path.exists(test_package_path):
31+
test_package_content = tools.load(test_package_path)
32+
check_non_ascii("test_package/conanfile.py", test_package_content)

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -775,29 +775,6 @@ def test(self):
775775
" Remove it from {}."
776776
.format(os.path.join("test_package", "CMakeLists.txt")), output)
777777

778-
@pytest.mark.skipif(six.PY2, reason="Python2 doesn't support utf-8 by default")
779-
def test_non_ascii_characters(self):
780-
conanfile = textwrap.dedent("""\
781-
from conans import ConanFile
782-
class AConan(ConanFile):
783-
{}
784-
pass
785-
""")
786-
tools.save('conanfile.py', content=conanfile.replace("{}", "# Conan, the barbarian"))
787-
tools.save(os.path.join('test_package', 'conanfile.py'),
788-
content=conanfile.replace("{}", "def test(self): # Conan, the barbarian").replace("pass", " pass"))
789-
output = self.conan(['create', '.', 'name/version@user/test'])
790-
self.assertIn("[NO ASCII CHARACTERS (KB-H047)] OK", output)
791-
792-
tools.save('conanfile.py', content=conanfile.replace("{}", "# Conan, o bárbaro"))
793-
tools.save(os.path.join('test_package', 'conanfile.py'),
794-
content=conanfile.replace("{}", "def test(self): # Conan, o bárbaro").replace("pass", " pass"))
795-
output = self.conan(['create', '.', 'name/version@user/test'])
796-
self.assertIn("ERROR: [NO ASCII CHARACTERS (KB-H047)] The file 'conanfile.py' contains a non-ascii character at line (3)." \
797-
" Only ASCII characters are allowed, please remove it.", output)
798-
self.assertIn("ERROR: [NO ASCII CHARACTERS (KB-H047)] The file 'test_package/conanfile.py' contains a non-ascii character at line (3)." \
799-
" Only ASCII characters are allowed, please remove it.", output)
800-
801778
def test_delete_option(self):
802779
conanfile = textwrap.dedent("""\
803780
from conans import ConanFile

tests/test_hooks/test_non_ascii.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
import textwrap
3+
4+
from conans import tools
5+
6+
from tests.utils.test_cases.conan_client import ConanClientTestCase
7+
8+
9+
class NonASCIITests(ConanClientTestCase):
10+
conanfile = textwrap.dedent("""\
11+
from conans import ConanFile
12+
13+
class AConan(ConanFile):
14+
author = "Юрий Алексеевич Гагарин"
15+
description = "A Terra é Azul"
16+
license = "fake_license"
17+
""")
18+
19+
def _get_environ(self, **kwargs):
20+
kwargs = super(NonASCIITests, self)._get_environ(**kwargs)
21+
kwargs.update({'CONAN_HOOKS': os.path.join(os.path.dirname(__file__), '..', '..', 'hooks', 'non_ascii')})
22+
return kwargs
23+
24+
def test_with_non_ascii(self):
25+
tools.save('conanfile.py', content=self.conanfile)
26+
output = self.conan(['export', '.', 'name/version@user/channel'])
27+
self.assertIn("ERROR: The file \'conanfile.py\' contains a non-ascii character at line (5)."
28+
" Only ASCII characters are allowed, please remove it.", output)
29+
30+
def test_with_no_non_ascii(self):
31+
tools.save('conanfile.py', content=self.conanfile
32+
.replace("Юрий Алексеевич Гагарин", "Yuri Alekseyevich Gagarin")
33+
.replace("A Terra é Azul", "The Earth is Blue"))
34+
output = self.conan(['export', '.', 'name/version@user/channel'])
35+
self.assertNotIn("ERROR:", output)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
skipsdist=True
33
envlist =
4-
py{27,36,37,38}-conan{dev,current,prev,prevprev}
4+
py{36,37,38}-conan{dev,current,prev,prevprev}
55

66
[testenv]
77
deps =

0 commit comments

Comments
 (0)