-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from greg-hellings/when_formatting
Add format checking for when lines
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
from ansiblelint import AnsibleLintRule | ||
try: | ||
from types import StringTypes | ||
except ImportError: | ||
# Python3 removed types.StringTypes | ||
StringTypes = str, | ||
|
||
|
||
class NoFormattingInWhenRule(AnsibleLintRule): | ||
id = 'ANSIBLE0019' | ||
shortdesc = 'No Jinja2 in when' | ||
description = '"when" lines should not include Jinja2 variables' | ||
tags = ['deprecated'] | ||
|
||
def _is_valid(self, when): | ||
if not isinstance(when, StringTypes): | ||
return True | ||
return when.find('{{') == -1 and when.find('}}') == -1 | ||
|
||
def matchplay(self, file, play): | ||
errors = [] | ||
if isinstance(play, dict): | ||
if 'roles' not in play: | ||
return errors | ||
for role in play['roles']: | ||
if self.matchtask(file, role): | ||
errors.append(({'when': role}, | ||
'role "when" clause has Jinja2 templates')) | ||
if isinstance(play, list): | ||
for play_item in play: | ||
sub_errors = self.matchplay(file, play_item) | ||
if sub_errors: | ||
errors = errors + sub_errors | ||
return errors | ||
|
||
def matchtask(self, file, task): | ||
return 'when' in task and not self._is_valid(task['when']) |
This file contains 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,21 @@ | ||
import unittest | ||
from ansiblelint import RulesCollection, Runner | ||
from ansiblelint.rules.NoFormattingInWhenRule import NoFormattingInWhenRule | ||
|
||
|
||
class TestNoFormattingInWhenRule(unittest.TestCase): | ||
collection = RulesCollection() | ||
|
||
def setUp(self): | ||
self.collection.register(NoFormattingInWhenRule()) | ||
|
||
def test_file_positive(self): | ||
success = 'test/jinja2-when-success.yml' | ||
good_runner = Runner(self.collection, success, [], [], []) | ||
self.assertEqual([], good_runner.run()) | ||
|
||
def test_file_negative(self): | ||
failure = 'test/jinja2-when-failure.yml' | ||
bad_runner = Runner(self.collection, failure, [], [], []) | ||
errs = bad_runner.run() | ||
self.assertEqual(2, len(errs)) |
This file contains 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,10 @@ | ||
- hosts: all | ||
tasks: | ||
- name: test when with jinja2 | ||
debug: msg=text | ||
when: "{{ false }}" | ||
|
||
- hosts: all | ||
roles: | ||
- role: test | ||
when: "{{ '1' = '1' }}" |
This file contains 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,10 @@ | ||
- hosts: all | ||
tasks: | ||
- name: test when | ||
debug: msg=text | ||
when: true | ||
|
||
- hosts: all | ||
roles: | ||
- role: role | ||
when: 1 = 1 |