Implemented vars files scanning on n levels

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-15 18:13:33 +02:00
parent 111d6ac50d
commit a6afbaff38
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

View File

@ -10,18 +10,21 @@ class TestVariableDefinitions(unittest.TestCase):
self.project_root = os.path.abspath( self.project_root = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../') os.path.join(os.path.dirname(__file__), '../../')
) )
# Gather all definition files: include any .yml under vars/ and defaults/, plus group_vars/all # Gather all definition files recursively under vars/ and defaults/, plus group_vars/all
self.var_files = ( self.var_files = []
glob(os.path.join(self.project_root, 'roles/*/vars/*.yml')) + patterns = [
glob(os.path.join(self.project_root, 'roles/*/defaults/*.yml')) + os.path.join(self.project_root, 'roles', '*', 'vars', '**', '*.yml'),
glob(os.path.join(self.project_root, 'group_vars/all/*.yml')) os.path.join(self.project_root, 'roles', '*', 'defaults', '**', '*.yml'),
) os.path.join(self.project_root, 'group_vars', 'all', '*.yml'),
]
for pat in patterns:
self.var_files.extend(glob(pat, recursive=True))
# Valid file extensions to scan for definitions and usages # Valid file extensions to scan for definitions and usages
self.scan_extensions = {'.yml', '.j2'} self.scan_extensions = {'.yml', '.j2'}
# Regex patterns # Regex patterns
self.simple_var_pattern = re.compile(r"{{\s*([a-zA-Z_]\w*)\s*(?:\|[^}]*)?}}") self.simple_var_pattern = re.compile(r"{{\s*([a-zA-Z_]\w*)\s*(?:\|[^}]*)?}}")
# new: allow an optional '-' after the '{%'
self.jinja_set_def = re.compile(r'{%\s*-?\s*set\s+([a-zA-Z_]\w*)\s*=') self.jinja_set_def = re.compile(r'{%\s*-?\s*set\s+([a-zA-Z_]\w*)\s*=')
self.jinja_for_def = re.compile(r'{%\s*-?\s*for\s+([a-zA-Z_]\w*)(?:\s*,\s*([a-zA-Z_]\w*))?\s+in') self.jinja_for_def = re.compile(r'{%\s*-?\s*for\s+([a-zA-Z_]\w*)(?:\s*,\s*([a-zA-Z_]\w*))?\s+in')
self.ansible_set_fact = re.compile(r'^(?:\s*[-]\s*)?set_fact\s*:\s*$') self.ansible_set_fact = re.compile(r'^(?:\s*[-]\s*)?set_fact\s*:\s*$')
@ -120,8 +123,8 @@ class TestVariableDefinitions(unittest.TestCase):
continue continue
# skip defaults_var fallback # skip defaults_var fallback
if var not in self.defined and \ if var not in self.defined and \
f"defaults_{var}" not in self.defined and \ f"default_{var}" not in self.defined and \
f"default_{var}" not in self.defined: f"defaults_{var}" not in self.defined:
undefined_uses.append( undefined_uses.append(
f"{path}:{lineno}: '{{{{ {var} }}}}' used but not defined" f"{path}:{lineno}: '{{{{ {var} }}}}' used but not defined"
) )