From 08e2e04f2a055e729bcbdd54636206b2cf3079ff Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 17 Dec 2019 16:57:25 +0000 Subject: [PATCH] Bug 1604079 - Add a unit test for mozlint yamllint r=ahal + some python 3 changes Differential Revision: https://phabricator.services.mozilla.com/D57263 --HG-- extra : moz-landing-system : lando --- tools/lint/test/files/yaml/bad.yml | 7 +++++++ tools/lint/test/files/yaml/good.yml | 6 ++++++ tools/lint/test/python.ini | 1 + tools/lint/test/test_yaml.py | 26 ++++++++++++++++++++++++++ tools/lint/yamllint_/__init__.py | 6 +++--- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tools/lint/test/files/yaml/bad.yml create mode 100644 tools/lint/test/files/yaml/good.yml create mode 100644 tools/lint/test/test_yaml.py diff --git a/tools/lint/test/files/yaml/bad.yml b/tools/lint/test/files/yaml/bad.yml new file mode 100644 index 000000000000..ab09bd3755bf --- /dev/null +++ b/tools/lint/test/files/yaml/bad.yml @@ -0,0 +1,7 @@ +--- +yamllint: + description: YAML linteraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax + include: + - .cron.yml + - browser/config/ + - wrong diff --git a/tools/lint/test/files/yaml/good.yml b/tools/lint/test/files/yaml/good.yml new file mode 100644 index 000000000000..b30941b7976f --- /dev/null +++ b/tools/lint/test/files/yaml/good.yml @@ -0,0 +1,6 @@ +--- +yamllint: + description: YAML linter + include: + - .cron.yml + - browser/config/ diff --git a/tools/lint/test/python.ini b/tools/lint/test/python.ini index f184752beb9c..71003cab550d 100644 --- a/tools/lint/test/python.ini +++ b/tools/lint/test/python.ini @@ -16,3 +16,4 @@ skip-if = os == "win" [test_rst.py] [test_codespell.py] skip-if = os == "win" || os == "mac" # codespell installed on Linux +[test_yaml.py] \ No newline at end of file diff --git a/tools/lint/test/test_yaml.py b/tools/lint/test/test_yaml.py new file mode 100644 index 000000000000..a540c43b4ceb --- /dev/null +++ b/tools/lint/test/test_yaml.py @@ -0,0 +1,26 @@ +import pytest +import mozunit +from mozfile import which + +LINTER = 'yaml' +pytestmark = pytest.mark.skipif(not which('yamllint'), reason="yamllint is not installed") + + +def test_basic(lint, paths): + results = lint(paths()) + + assert len(results) == 2 + + assert "line too long (122 > 80 characters)'" in results[0].message + assert results[0].level == "error" + assert "bad.yml" in results[0].relpath + assert results[0].lineno == 3 + + assert "wrong indentation: expected 4 but found 8" in results[1].message + assert results[1].level == "error" + assert "bad.yml" in results[1].relpath + assert results[0].lineno == 3 + + +if __name__ == '__main__': + mozunit.main() diff --git a/tools/lint/yamllint_/__init__.py b/tools/lint/yamllint_/__init__.py index 4229ff511043..5efd66f7449b 100644 --- a/tools/lint/yamllint_/__init__.py +++ b/tools/lint/yamllint_/__init__.py @@ -23,7 +23,7 @@ Try to install it manually with: $ pip install -U --require-hashes -r {} """.strip().format(YAMLLINT_REQUIREMENTS_PATH) -YAMLLINT_FORMAT_REGEX = re.compile(r'(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$') +YAMLLINT_FORMAT_REGEX = re.compile(b'(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$') results = [] @@ -43,8 +43,8 @@ class YAMLLintProcess(ProcessHandlerMixin): print('Unable to match yaml regex against output: {}'.format(line)) return - res = {'path': os.path.relpath(abspath, self.config['root']), - 'message': message, + res = {'path': os.path.relpath(str(abspath), self.config['root']), + 'message': str(message), 'level': 'error', 'lineno': line, 'column': col,