Skip to content

Commit

Permalink
with_filetree loop: reduce false positive (#582)
Browse files Browse the repository at this point in the history
don't report an error when a path ending with a slash or a variable are
used
  • Loading branch information
pilou- authored and awcrosby committed Dec 4, 2019
1 parent 84ba938 commit d7c0dad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/ansiblelint/rules/UsingBareVariablesIsDeprecatedRule.py
Expand Up @@ -18,6 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import re
import six
from ansiblelint import AnsibleLintRule
Expand All @@ -42,7 +43,7 @@ def matchtask(self, file, task):
loop_type = next((key for key in task
if key.startswith("with_")), None)
if loop_type:
if loop_type in ["with_nested", "with_together", "with_flattened"]:
if loop_type in ["with_nested", "with_together", "with_flattened", "with_filetree"]:
# These loops can either take a list defined directly in the task
# or a variable that is a list itself. When a single variable is used
# we just need to check that one variable, and not iterate over it like
Expand All @@ -63,8 +64,12 @@ def matchtask(self, file, task):
def _matchvar(self, varstring, task, loop_type):
if (isinstance(varstring, six.string_types) and
not self._jinja.match(varstring)):
if loop_type != 'with_fileglob' or not (self._jinja.search(varstring) or
self._glob.search(varstring)):
valid = loop_type == 'with_fileglob' and bool(self._jinja.search(varstring) or
self._glob.search(varstring))

valid |= loop_type == 'with_filetree' and bool(self._jinja.search(varstring) or
varstring.endswith(os.sep))
if not valid:
message = "Found a bare variable '{0}' used in a '{1}' loop." + \
" You should use the full variable syntax ('{{{{ {0} }}}}')"
return message.format(task[loop_type], loop_type)
2 changes: 1 addition & 1 deletion test/TestUsingBareVariablesIsDeprecated.py
Expand Up @@ -18,4 +18,4 @@ def test_file_negative(self):
failure = 'test/using-bare-variables-failure.yml'
bad_runner = Runner(self.collection, failure, [], [], [])
errs = bad_runner.run()
self.assertEqual(13, len(errs))
self.assertEqual(14, len(errs))
5 changes: 5 additions & 0 deletions test/using-bare-variables-failure.yml
Expand Up @@ -61,6 +61,11 @@
msg: "{{ item }}"
with_fileglob: my_list

- name: with_filetree loop using bare variable
debug:
msg: "{{ item }}"
with_filetree: my_list

- name: with_together loop using bare variable
debug:
msg: "{{ item.0 }} {{ item.1 }}"
Expand Down
20 changes: 20 additions & 0 deletions test/using-bare-variables-success.yml
Expand Up @@ -107,6 +107,26 @@
msg: "{{ item }}"
with_fileglob: 'foo{{glob}}'

### Testing with_filetree
- name: with_filetree loop using list of path
debug:
msg: "{{ item }}"
with_filetree:
- path/to/dir1/
- path/to/dir2/

### Testing non-list form of with_filetree
- name: with_filetree loop using single path
debug:
msg: "{{ item }}"
with_filetree: path/to/dir/

### Testing non-list form of with_filetree with trailing templated pattern
- name: with_filetree loop using templated pattern
debug:
msg: "{{ item }}"
with_filetree: 'path/to/{{ directory }}'

### Testing with_together
- name: with_together loop using variable lists
debug:
Expand Down

0 comments on commit d7c0dad

Please sign in to comment.