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

Expand environment vars and ~ #672

Merged
merged 13 commits into from
Feb 27, 2020
18 changes: 15 additions & 3 deletions lib/ansiblelint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ def matchyaml(self, file, text):

class RulesCollection(object):

def __init__(self):
def __init__(self, rulesdirs=[]):
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
self._update_rulesdirs(rulesdirs)
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
self.rules = []
self._update_rules(self.rulesdirs)
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved

def register(self, obj):
self.rules.append(obj)
Expand All @@ -152,6 +154,14 @@ def __iter__(self):
def __len__(self):
return len(self.rules)

def _update_rulesdirs(self, rulesdirs):
rulesdirs = ansiblelint.utils.expand_paths_vars(rulesdirs)
self.rulesdirs = rulesdirs

def _update_rules(self, rulesdirs):
for rulesdir in rulesdirs:
self.extend(ansiblelint.utils.load_plugins(rulesdir))

def extend(self, more):
self.rules.extend(more)

Expand Down Expand Up @@ -196,7 +206,9 @@ def listtags(self):
@classmethod
def create_from_directory(cls, rulesdir):
result = cls()
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
result.rules = ansiblelint.utils.load_plugins(os.path.expanduser(rulesdir))
rulesdir = os.path.expanduser(rulesdir)
rulesdir = os.path.expandvars(rulesdir)
result.rules = ansiblelint.utils.load_plugins(rulesdir)
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
return result


Expand Down Expand Up @@ -239,7 +251,7 @@ def __init__(self, rules, playbook, tags, skip_list, exclude_paths,
def _update_exclude_paths(self, exclude_paths):
if exclude_paths:
# These will be (potentially) relative paths
paths = [s.strip() for s in exclude_paths]
paths = ansiblelint.utils.expand_paths_vars(exclude_paths)
# Since ansiblelint.utils.find_children returns absolute paths,
# and the list of files we create in `Runner.run` can contain both
# relative and absolute paths, we need to cover both bases.
Expand Down
5 changes: 1 addition & 4 deletions lib/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ def main():
rulesdirs = options.rulesdir + [default_rulesdir]
else:
rulesdirs = options.rulesdir or [default_rulesdir]

rules = RulesCollection()
for rulesdir in rulesdirs:
rules.extend(RulesCollection.create_from_directory(rulesdir))
rules = RulesCollection(rulesdirs)

if options.listrules:
print(rules)
Expand Down
14 changes: 14 additions & 0 deletions lib/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,17 @@ def get_playbooks_and_roles(options=None):
print('Found playbooks: ' + ' '.join(playbooks))

return role_dirs + playbooks


def expand_path_vars(path):
"""Expand the environment or ~ variables in a path string."""
path = path.strip()
path = os.path.expanduser(path)
path = os.path.expandvars(path)
return path


def expand_paths_vars(paths):
"""Expand the environment or ~ variables in a list."""
paths = [expand_path_vars(p) for p in paths]
return paths
16 changes: 14 additions & 2 deletions test/TestRulesCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import collections
import unittest
import os

from ansiblelint import RulesCollection

Expand All @@ -31,7 +32,7 @@ class TestRulesCollection(unittest.TestCase):
bracketsmatchtestfile = dict(path='test/bracketsmatchtest.yml', type='playbook')

def setUp(self):
self.rules = RulesCollection.create_from_directory('./test/rules')
self.rules = RulesCollection(['./test/rules'])

def test_load_collection_from_directory(self):
self.assertEqual(len(self.rules), 2)
Expand Down Expand Up @@ -74,7 +75,18 @@ def test_skip_non_existent_id(self):
matches = self.rules.run(self.ematchtestfile, skip_list=['DOESNOTEXIST'])
self.assertEqual(len(matches), 3)

def test_rulesdir_var_expansion(self):
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
test_path = '/test/path'
os.environ['TEST_PATH'] = test_path
test_rulesdirs = ['$TEST_PATH']
expansion_rules = RulesCollection(test_rulesdirs)
self.assertIn(test_path, expansion_rules.rulesdirs)

def test_rulesdir_user_expansion(self):
expansion_rules = RulesCollection(['~'])
self.assertIn(os.path.expanduser('~'), expansion_rules.rulesdirs)

def test_no_duplicate_rule_ids(self):
real_rules = RulesCollection.create_from_directory('./lib/ansiblelint/rules')
real_rules = RulesCollection(['./lib/ansiblelint/rules'])
rule_ids = [rule.id for rule in real_rules]
self.assertEqual([x for x, y in collections.Counter(rule_ids).items() if y > 1], [])
13 changes: 13 additions & 0 deletions test/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ def test_unicode_standard_color_formatting(self):
formatter = ansiblelint.formatters.Formatter()
formatter.format(matches[0], colored=True)

def test_runner_exclude_var_expansion(self):
filename = 'example/lots_of_warnings.yml'
os.environ['EXCLUDE_PATH'] = filename
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
excludes = ['$EXCLUDE_PATH']
runner = Runner(self.rules, filename, [], [], excludes)
assert filename in runner.exclude_paths

def test_runner_exclude_user_expansion(self):
filename = 'example/lots_of_warnings.yml'
excludes = ['~']
runner = Runner(self.rules, filename, [], [], excludes)
assert os.path.expanduser('~') in runner.exclude_paths

def test_runner_excludes_paths(self):
filename = 'examples/lots_of_warnings.yml'
excludes = ['examples/lots_of_warnings.yml']
Expand Down
13 changes: 13 additions & 0 deletions test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# THE SOFTWARE.

import unittest
import os
try:
from pathlib import Path
except ImportError:
Expand Down Expand Up @@ -154,3 +155,15 @@ def test_normpath_with_string(self):
self.assertEqual(
utils.normpath("a/b/../"),
"a")

def test_expand_path_vars(self):
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
test_path = '/test/path'
os.environ['TEST_PATH'] = test_path
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(utils.expand_path_vars('~'), os.path.expanduser('~'))
evilhamsterman marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(utils.expand_path_vars('$TEST_PATH'), test_path)

def test_expand_paths_vars(self):
test_path = '/test/path'
os.environ['TEST_PATH'] = test_path
self.assertEqual(utils.expand_paths_vars(['~']), [os.path.expanduser('~')])
self.assertEqual(utils.expand_paths_vars(['$TEST_PATH']), [test_path])