Skip to content

Commit

Permalink
Fix running with role path containing single or multiple dirs (#390)
Browse files Browse the repository at this point in the history
* Fix running with role path containing single or multiple dirs

Signed-off-by: Andrew Crosby <acrosby@redhat.com>

* Changed fix to check for role files inside rolepath dir

Signed-off-by: Andrew Crosby <acrosby@redhat.com>

* Add test case for role path deeper than 2 dirs

Signed-off-by: Andrew Crosby <acrosby@redhat.com>
  • Loading branch information
awcrosby committed Nov 27, 2018
1 parent 7c380ea commit 88e9114
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ def _rolepath(basedir, role):
path_dwim(
basedir, os.path.join('..', '..', '..', 'roles', role)
),
path_dwim(basedir, os.path.join('..', '..', role))
path_dwim(basedir, os.path.join('..', '..', role)),
path_dwim(basedir, ''),
]

if constants.DEFAULT_ROLES_PATH:
Expand Down
78 changes: 78 additions & 0 deletions test/TestCliRolePaths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import unittest
import subprocess
import os


class TestCliRolePaths(unittest.TestCase):
def setUp(self):
self.local_test_dir = os.path.dirname(os.path.realpath(__file__))

def run_ansible_lint(self, cwd, bin, role_path):
command = '{} {}'.format(bin, role_path)

result, err = subprocess.Popen(
[command],
cwd=cwd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
).communicate()

self.assertFalse(err, 'Expected no error but was ' + str(err))

return result

def test_run_single_role_path_no_trailing_slash(self):
cwd = self.local_test_dir
bin = '../bin/ansible-lint'
role_path = 'test-role'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))

def test_run_single_role_path_with_trailing_slash(self):
cwd = self.local_test_dir
bin = '../bin/ansible-lint'
role_path = 'test-role/'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))

def test_run_multiple_role_path_no_trailing_slash(self):
cwd = self.local_test_dir
bin = '../bin/ansible-lint'
role_path = 'roles/test-role'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))

def test_run_multiple_role_path_with_trailing_slash(self):
cwd = self.local_test_dir
bin = '../bin/ansible-lint'
role_path = 'roles/test-role/'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))

def test_run_inside_role_dir(self):
cwd = os.path.join(self.local_test_dir, 'test-role/')
bin = '../../bin/ansible-lint'
role_path = '.'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))

def test_run_role_three_dir_deep(self):
cwd = self.local_test_dir
bin = '../bin/ansible-lint'
role_path = 'roles/roles/test-role'

result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
self.assertIn('Use shell only when shell functionality is required',
str(result))
2 changes: 2 additions & 0 deletions test/roles/roles/test-role/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- name: shell instead of command
shell: echo hello world
2 changes: 2 additions & 0 deletions test/roles/test-role/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- name: shell instead of command
shell: echo hello world

0 comments on commit 88e9114

Please sign in to comment.