Skip to content
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

Add syntax checker for Ansible #1858

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- New features and improvements

- New checker for Ansible YAML using ``ansible-lint``.
- The ``flycheck-verify-setup`` UI now includes buttons to re-enable manually
disabled checkers and to try to re-enable automatically disabled checkers
(command checkers are automatically disabled when their executable cannot be
Expand Down
8 changes: 8 additions & 0 deletions doc/languages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ to view the docstring of the syntax checker. Likewise, you may use
A list of additional warnings to enable. Each item is the name of a
warning category to enable.

.. supported-language:: Ansible

.. syntax-checker:: ansible-ansiblelint

Check Ansible YAML files with ansible-lint_.

.. _ansible-lint: https://ansible-lint.readthedocs.io

.. supported-language:: AsciiDoc

.. syntax-checker:: asciidoctor
Expand Down
38 changes: 38 additions & 0 deletions flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ attention to case differences."

(defcustom flycheck-checkers
'(ada-gnat
ansible-ansiblelint
asciidoctor
asciidoc
awk-gawk
Expand Down Expand Up @@ -7464,6 +7465,43 @@ Uses the GNAT compiler from GCC. See URL
": " (message) line-end))
:modes ada-mode)

(flycheck-define-checker ansible-ansiblelint
"An Ansible linter using the ansible-lint tool.

See URL `https://ansible-lint.readthedocs.io/en/latest/'."
;; emacs-ansible provides ansible, not ansible-mode
:enabled (bound-and-true-p ansible)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:enabled is still a predicate and needs a lambda(), like it was before it has been changed to bound-and-true-p.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsoleted by @fredericgiquel's branch.

:command ("ansible-lint" "--nocolor" "-p" source)
:error-patterns
((error "CRITICAL Couldn't parse task at " (file-name) ":" line " " (message))
(warning line-start (file-name) ":" line ": [E" (id (+ digit)) "] " (message)
line-end))
:error-explainer
(lambda (err)
(let* ((id (flycheck-error-id err))
(lines (process-lines "ansible-lint" "-L" "-f" "plain"))
;; process-lines may flush output before the entire line of the rule
;; is ready; reconstruct the desired error to a single line
(start (+ 1 (seq-position
lines t
(lambda (elt _)
(string-prefix-p id elt)))))
(next-id (car (seq-filter
(lambda (elt)
(string-match-p "^[0-9]\\{3,4\\}: " elt))
(nthcdr start lines))))
(end (if (not next-id)
(length lines)
(seq-position
lines t
(lambda (elt _)
(string-prefix-p next-id elt))))))
(substring (mapconcat 'identity (seq-subseq lines start end) "") 2)))
:modes yaml-mode
:next-checkers ((t . yaml-jsyaml)
(t . yaml-ruby)
(t . yaml-yamllint)))

(flycheck-define-checker asciidoc
"A AsciiDoc syntax checker using the AsciiDoc compiler.

Expand Down
16 changes: 16 additions & 0 deletions test/flycheck-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3102,6 +3102,22 @@ evaluating BODY."
'( 6 4 warning "variable \"Name\" is not referenced" :checker ada-gnat)
'(8 11 warning "unrecognized pragma \"Foo\"" :checker ada-gnat)))

(flycheck-ert-def-checker-test ansible-ansiblelint ansible errors
(flycheck-ert-should-syntax-check
"language/ansible/error.yml" 'yaml-mode
'(3 nil error "(conflicting action statements: debug, command)"
:checker ansible-ansiblelint)))

(flycheck-ert-def-checker-test ansible-ansiblelint ansible warnings
(flycheck-ert-should-syntax-check
"language/ansible/warnings.yml" 'yaml-mode
'(5 nil warning "Commands should not change things if nothing needs doing"
:checker ansible-ansiblelint)
'(5 nil warning "service used in place of service module"
:checker ansible-ansiblelint)
'(9 nil warning "variables should have spaces before and after: {{ varname }}"
:checker ansible-ansiblelint)))

(flycheck-ert-def-checker-test asciidoc asciidoc nil
(let ((flycheck-disabled-checkers '(asciidoctor)))
(flycheck-ert-should-syntax-check
Expand Down
11 changes: 11 additions & 0 deletions test/resources/language/ansible/error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- hosts: localhost
tasks:
- name: foo
debug:
msg: bar
command: echo
# On this file ansible-playbook --syntax-check reports:
# ERROR! conflicting action statements: debug, command
#
# The error appears to be in 'test/conflicting_action.yml': line 3, column 7, but may
# be elsewhere in the file depending on the exact syntax problem.
9 changes: 9 additions & 0 deletions test/resources/language/ansible/warnings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- hosts: bobbins

tasks:
- name: a bad play
action: command service blah restart

- name: incorrect brackets
file: "{{space}}"