Fixing a bug that prevents specifying wildcards for filenames.

This commit is contained in:
Gareth J. Greenaway 2018-07-30 16:32:56 -07:00
parent 00d06bda76
commit 2e01c55e7e
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
2 changed files with 35 additions and 1 deletions

View file

@ -6213,6 +6213,16 @@ def grep(path,
'''
path = os.path.expanduser(path)
# Backup the path in case the glob returns nothing
_path = path
path = glob.glob(path)
# If the list is empty no files exist
# so we revert back to the original path
# so the result is an error.
if not path:
path = _path
split_opts = []
for opt in opts:
try:
@ -6227,7 +6237,10 @@ def grep(path,
)
split_opts.extend(split)
cmd = ['grep'] + split_opts + [pattern, path]
if isinstance(path, list):
cmd = ['grep'] + split_opts + [pattern] + path
else:
cmd = ['grep'] + split_opts + [pattern, path]
try:
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
except (IOError, OSError) as exc:

View file

@ -522,6 +522,27 @@ class FileBlockReplaceTestCase(TestCase, LoaderModuleMockMixin):
backup=False
)
def test_grep_query_exists_wildcard(self):
_file = '{0}-junk*'.format(self.tfile.name)
result = filemod.grep(_file,
'Lorem ipsum')
self.assertTrue(result, None)
self.assertTrue(result['retcode'] == 0)
self.assertTrue(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
self.assertTrue(result['stderr'] == '')
def test_grep_file_not_exists_wildcard(self):
_file = '{0}-junk*'.format(self.tfile.name)
result = filemod.grep(_file,
'Lorem ipsum')
self.assertTrue(result, None)
self.assertFalse(result['retcode'] == 0)
self.assertFalse(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
_expected_stderr = 'grep: {0}-junk*: No such file or directory'.format(self.tfile.name)
self.assertTrue(result['stderr'] == _expected_stderr)
class FileModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):