From 57254db9ef1e0b0bf21466ed2d3ebaf60338768f Mon Sep 17 00:00:00 2001 From: SkrrtBacharach <61713784+SkrrtBacharach@users.noreply.github.com> Date: Fri, 7 Apr 2023 01:19:58 +0100 Subject: Fix error from ansible-lint versions >=6.11.0. (#4492) * Fix error from ansible-lint versions >=6.11.0. The JSON output format of ansible-lint has changed since 6.11.0. Issue locations can have either a 'positions' or a 'lines' member, rather than just a 'lines' member as it was before. This fix checks which member is present, and passes that member name to subsequent dictionary lookups. The error was caused by the following change: https://github.com/ansible/ansible-lint/pull/2897 * Add ansible-lint test to check each type of ansible-lint issue json. * Change long single-line JSON in ansible test into multiline JSON. * Fix linting errors in ansible_lint.vim. --- ale_linters/ansible/ansible_lint.vim | 16 +++++-- test/handler/test_ansible_lint_handler.vader | 66 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/ale_linters/ansible/ansible_lint.vim b/ale_linters/ansible/ansible_lint.vim index bdfbee3c..e15008c8 100644 --- a/ale_linters/ansible/ansible_lint.vim +++ b/ale_linters/ansible/ansible_lint.vim @@ -29,10 +29,20 @@ function! ale_linters#ansible#ansible_lint#Handle(buffer, version, lines) abort for l:issue in l:linter_issues if ale#path#IsBufferPath(a:buffer, l:issue.location.path) + if exists('l:issue.location.positions') + let l:coord_keyname = 'positions' + else + let l:coord_keyname = 'lines' + endif + + let l:column_member = printf( + \ 'l:issue.location.%s.begin.column', l:coord_keyname + \) + call add(l:output, { - \ 'lnum': exists('l:issue.location.lines.begin.column') ? l:issue.location.lines.begin.line : - \ l:issue.location.lines.begin, - \ 'col': exists('l:issue.location.lines.begin.column') ? l:issue.location.lines.begin.column : 0, + \ 'lnum': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.line : + \ l:issue.location[l:coord_keyname].begin, + \ 'col': exists(l:column_member) ? l:issue.location[l:coord_keyname].begin.column : 0, \ 'text': l:issue.check_name, \ 'detail': l:issue.description, \ 'code': l:issue.severity, diff --git a/test/handler/test_ansible_lint_handler.vader b/test/handler/test_ansible_lint_handler.vader index fbc38f30..92e11332 100644 --- a/test/handler/test_ansible_lint_handler.vader +++ b/test/handler/test_ansible_lint_handler.vader @@ -86,6 +86,72 @@ Execute (The ansible-lint handler for version group >=5 should handle names with \ fnamemodify(tempname(), ':h') . "/test playbook.yml:3:148: [syntax-check] [VERY_HIGH] 'var' is not a valid attribute for a Play", \ ]) +Execute (The ansible-lint handler should work with issues with positions and lines members): + AssertEqual + \ [ + \ { + \ 'lnum': 6, + \ 'col': 7, + \ 'code': 'major', + \ 'type': 'W', + \ 'text': "syntax-check[specific]", + \ 'detail': 'fakedesc', + \ }, + \ { + \ 'lnum': 6, + \ 'col': 0, + \ 'code': 'major', + \ 'type': 'W', + \ 'text': 'fqcn[action-core]', + \ 'detail': 'fakedesc2' + \ } + \ ], + \ ale_linters#ansible#ansible_lint#Handle(bufnr(''), [6, 11, 0], [ + \ '[', + \ ' {', + \ ' "type": "issue",', + \ ' "check_name": "syntax-check[specific]",', + \ ' "categories": [', + \ ' "core",', + \ ' "unskippable"', + \ ' ],', + \ ' "url": "https://ansible-lint.readthedocs.io/rules/syntax-check/",', + \ ' "severity": "major",', + \ ' "description": "fakedesc",', + \ ' "fingerprint": "4",', + \ ' "location": {', + \ ' "path": "test playbook.yml",', + \ ' "positions": {', + \ ' "begin": {', + \ ' "line": 6,', + \ ' "column": 7', + \ ' }', + \ ' }', + \ ' }', + \ ' },', + \ ' {', + \ ' "type": "issue",', + \ ' "check_name": "fqcn[action-core]",', + \ ' "categories": [', + \ ' "formatting"', + \ ' ],', + \ ' "url": "https://ansible-lint.readthedocs.io/rules/fqcn/",', + \ ' "severity": "major",', + \ ' "description": "fakedesc2",', + \ ' "fingerprint": "f",', + \ ' "location": {', + \ ' "path": "test playbook.yml",', + \ ' "lines": {', + \ ' "begin": 6', + \ ' }', + \ ' },', + \ ' "content": {', + \ ' "body": "Use `ansible.builtin.command` or `ansible.legacy.command` instead."', + \ ' }', + \ ' }', + \ ']' + \ ]) + Execute (The ansible-lint handler should ignore errors from other files): AssertEqual \ [ -- cgit v1.2.3