From 7e924af87bdd1676f7735b832363f17bd847a8a5 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Thu, 11 Jul 2019 18:06:52 +0200 Subject: [PATCH] include only jinja2 strings if candidate is a template --- ansiblelater/rules/ansiblefiles.py | 38 ++++++++++++++++++++---------- test-requirements.txt | 1 + 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ansiblelater/rules/ansiblefiles.py b/ansiblelater/rules/ansiblefiles.py index bb779c1..a56f2f0 100644 --- a/ansiblelater/rules/ansiblefiles.py +++ b/ansiblelater/rules/ansiblefiles.py @@ -179,9 +179,21 @@ def check_empty_string_compare(candidate, settings): description = "use `when: var` rather than `when: var != ""` (or " \ "conversely `when: not var` rather than `when: var == ""`)" - empty_string_compare = re.compile("[=!]= ?[\"'][\"']") - if not errors: + if isinstance(candidate, Template): + matches = [] + jinja_string = re.compile("({{|{%)(.*?)(}}|%})") + + for i, line in yamllines: + match = jinja_string.findall(line) + if match: + for item in match: + matches.append((i, item[1])) + + yamllines = matches + + empty_string_compare = re.compile("[=!]= ?[\"'][\"']") + for i, line in yamllines: if empty_string_compare.findall(line): errors.append(Error(i, description)) @@ -194,21 +206,21 @@ def check_compare_to_literal_bool(candidate, settings): description = "use `when: var` rather than `when: var == True` " \ "(or conversely `when: not var`)" - if isinstance(candidate, Template): - matches = [] - braces = re.compile("({{|{%)(.*?)(}}|%})") + if not errors: + if isinstance(candidate, Template): + matches = [] + jinja_string = re.compile("({{|{%)(.*?)(}}|%})") - for i, line in yamllines: - match = braces.findall(line) - if match: - for item in match: - matches.append((i, item[1])) + for i, line in yamllines: + match = jinja_string.findall(line) + if match: + for item in match: + matches.append((i, item[1])) - yamllines = matches + yamllines = matches - literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)") + literal_bool_compare = re.compile("[=!]= ?(True|true|False|false)") - if not errors: for i, line in yamllines: if literal_bool_compare.findall(line): errors.append(Error(i, description)) diff --git a/test-requirements.txt b/test-requirements.txt index 5caab59..4cf6fd2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ +pydocstyle<4.0.0 flake8 flake8-colors flake8-blind-except