-
Notifications
You must be signed in to change notification settings - Fork 46
Implement a YAML linter hook #358
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
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
46c1ff9
imlement yaml linter hook
ericLemanissier c02f3c4
CI: install yamllint
ericLemanissier 1e914e6
Update yaml_linter.py
ericLemanissier f846701
Update yaml_linter.py
ericLemanissier 3cfc6cd
Update yaml_linter.py
ericLemanissier a470d0d
Update yaml_linter.py
ericLemanissier cedc758
Update recipe_linter.py
ericLemanissier 3932064
be explicit when yamllint is not installed
ericLemanissier 2a9a972
fix test
ericLemanissier 0876647
lint all yml files
ericLemanissier 5dbb4d1
Update test_yaml_linter.py
ericLemanissier 65b0f5d
Update test_yaml_linter.py
ericLemanissier 58f9019
Update requirements_test.txt
ericLemanissier 9a403d4
Update generate_env_windows.bat
ericLemanissier bc02165
Update requirements_linux.txt
ericLemanissier d9a5a3c
Update test_yaml_linter.py
ericLemanissier a088850
Update recipe_linter.py
ericLemanissier d99c718
force recreation of tox environment
ericLemanissier a33d237
Update test_yaml_linter.py
ericLemanissier b7f9671
Scan also ../config.yml
ericLemanissier 3b1ab55
Update hooks/yaml_linter.py
ericLemanissier 67f855a
don't exit if yamllint is not available
ericLemanissier f4fdc61
Update yaml_linter.py
ericLemanissier 6d7cc69
Update yaml_linter.py
ericLemanissier 77059c5
Update tox.ini
ericLemanissier 4df32e6
Update README.md
ericLemanissier 9ba6953
Update README.md
ericLemanissier 88abe9b
Merge branch 'master' into yamllinter
ericLemanissier 197247b
Update README.md
ericLemanissier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# coding=utf-8 | ||
|
||
import os | ||
import platform | ||
import subprocess | ||
import sys | ||
|
||
from conans.errors import ConanException | ||
from conans.tools import logger | ||
|
||
|
||
try: | ||
import yamllint | ||
except ImportError as e: | ||
sys.stderr.write("Install yamllint to use 'yaml_linter' hook: 'pip install yamllint'") | ||
sys.exit(1) | ||
|
||
|
||
CONAN_HOOK_YAMLLINT_WERR = "CONAN_YAMLLINT_WERR" | ||
|
||
|
||
def pre_export(output, conanfile_path, *args, **kwargs): | ||
output.info("Lint yaml '{}'".format(conanfile_path)) | ||
conanfile_dirname = os.path.dirname(conanfile_path) | ||
|
||
rules = { | ||
"document-start": "disable", | ||
"line-length": "disable", | ||
"new-lines": "{level: warning}", | ||
"empty-lines": "{level: warning}", | ||
"indentation": "{level: warning}", | ||
"trailing-spaces": "{level: warning}", | ||
} | ||
|
||
lint_args = ['-f', 'parsable', | ||
'-d', '"{extends: default, rules: {%s}}"' % | ||
", ".join("%s: %s" % (r, rules[r]) for r in rules)] | ||
lint_args.append('"%s"' % conanfile_dirname.replace('\\', '/')) | ||
configfile = os.path.join(conanfile_dirname), "..", "config.yml") | ||
if os.path.isfile(configfile): | ||
lint_args.append('"%s"' % configfile.replace('\\', '/')) | ||
|
||
try: | ||
command = ['yamllint'] + lint_args | ||
command = " ".join(command) | ||
shell = bool(platform.system() != "Windows") | ||
p = subprocess.Popen(command, shell=shell, bufsize=10, | ||
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
yamllint_stdout, yamllint_stderr = p.communicate() | ||
yamllint_stdout = yamllint_stdout.decode('utf-8') | ||
except Exception as exc: | ||
output.error("Unexpected error running linter: {}".format(exc)) | ||
return | ||
errors = 0 | ||
for line in yamllint_stdout.splitlines(): | ||
output.info(line) | ||
i = line.find(":") | ||
line = line[i:] | ||
i = line.find(":") | ||
line = line[i:] | ||
parts = line.split(' ') | ||
errors += int(parts[1] == "[error]") | ||
|
||
output.info("YAML Linter detected '{}' errors".format(errors)) | ||
if os.getenv(CONAN_HOOK_YAMLLINT_WERR) and errors: | ||
raise ConanException("Package recipe has YAML linter errors. Please fix them.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# coding=utf-8 | ||
|
||
import os | ||
import textwrap | ||
import unittest | ||
|
||
from parameterized import parameterized | ||
|
||
from conans import tools | ||
from conans.client.command import ERROR_GENERAL, SUCCESS | ||
from conans.tools import environment_append | ||
from tests.utils.test_cases.conan_client import ConanClientTestCase | ||
|
||
|
||
class YAMLLinterTests(ConanClientTestCase): | ||
conanfile = textwrap.dedent(r""" | ||
from conans import ConanFile, tools | ||
|
||
class TestConan(ConanFile): | ||
name = "name" | ||
version = "version" | ||
""") | ||
|
||
def _get_environ(self, **kwargs): | ||
kwargs = super(YAMLLinterTests, self)._get_environ(**kwargs) | ||
kwargs.update({'CONAN_HOOKS': os.path.join(os.path.dirname( | ||
__file__), '..', '..', 'hooks', 'yaml_linter')}) | ||
return kwargs | ||
|
||
@parameterized.expand([(False, ), (True, )]) | ||
def test_basic(self, yamllint_werr): | ||
conandatafile = textwrap.dedent(r""" | ||
sources: | ||
"version": | ||
url: "https://url.to/name/version.tar.xz" | ||
sha256: "3a530d1b243b5dec00bc54937455471aaa3e56849d2593edb8ded07228202240" | ||
patches: | ||
"version": | ||
- patch_file: "patches/abcdef.diff" | ||
base_path: "source" | ||
patches: | ||
""") | ||
tools.save('conanfile.py', content=self.conanfile) | ||
tools.save('conandata.yml', content=conandatafile) | ||
yamllint_werr_value = "1" if yamllint_werr else None | ||
with environment_append({"CONAN_YAMLLINT_WERR": yamllint_werr_value}): | ||
return_code = ERROR_GENERAL if yamllint_werr else SUCCESS | ||
output = self.conan(['export', '.', 'name/version@'], expected_return_code=return_code) | ||
|
||
if yamllint_werr: | ||
self.assertIn("pre_export(): Package recipe has YAML linter errors." | ||
" Please fix them.", output) | ||
|
||
self.assertIn("conandata.yml:10:1:" | ||
" [error] duplication of key \"patches\" in mapping (key-duplicates)", | ||
output) | ||
|
||
def test_path_with_spaces(self): | ||
conandatafile = textwrap.dedent(r""" | ||
sources: | ||
"version": | ||
url: "https://url.to/name/version.tar.xz" | ||
sha256: "3a530d1b243b5dec00bc54937455471aaa3e56849d2593edb8ded07228202240" | ||
patches: | ||
"version": | ||
- patch_file: "patches/abcdef.diff" | ||
base_path: "source" | ||
""") | ||
tools.save(os.path.join("path spaces", "conanfile.py"), content=self.conanfile) | ||
tools.save(os.path.join("path spaces", "conandata.py"), content=conandatafile) | ||
output = self.conan(['export', 'path spaces/conanfile.py', 'name/version@']) | ||
recipe_path = os.path.join(os.getcwd(), "path spaces", "conanfile.py") | ||
self.assertIn("pre_export(): Lint yaml '{}'".format(recipe_path), output) | ||
self.assertIn("pre_export(): YAML Linter detected '0' errors", output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.