Merge pull request #27441 from Deewiant/issue-27401

states.file: allow prepending to non-existent file (#27401)
This commit is contained in:
Mike Place 2015-09-28 11:40:31 -06:00
commit 7dcec62fcc
2 changed files with 56 additions and 8 deletions

View File

@ -3290,11 +3290,9 @@ def append(name,
if not check_res:
return _error(ret, check_msg)
# Make sure that we have a file
__salt__['file.touch'](name)
check_res, check_msg = _check_file(name)
if not check_res:
# Try to create the file
touch(name, makedirs=makedirs)
retry_res, retry_msg = _check_file(name)
if not retry_res:
@ -3464,12 +3462,13 @@ def prepend(name,
if not check_res:
return _error(ret, check_msg)
# Make sure that we have a file
__salt__['file.touch'](name)
check_res, check_msg = _check_file(name)
if not check_res:
return _error(ret, check_msg)
# Try to create the file
touch(name, makedirs=makedirs)
retry_res, retry_msg = _check_file(name)
if not retry_res:
return _error(ret, check_msg)
# Follow the original logic and re-assign 'text' if using source(s)...
if sl_:

View File

@ -1067,7 +1067,8 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
def test_append_issue_1864_makedirs(self):
'''
file.append but create directories if needed as an option
file.append but create directories if needed as an option, and create
the file if it doesn't exist
'''
fname = 'append_issue_1864_makedirs'
name = os.path.join(integration.TMP, fname)
@ -1112,6 +1113,54 @@ class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
ignore_errors=True
)
def test_prepend_issue_27401_makedirs(self):
'''
file.prepend but create directories if needed as an option, and create
the file if it doesn't exist
'''
fname = 'prepend_issue_27401'
name = os.path.join(integration.TMP, fname)
try:
self.assertFalse(os.path.exists(name))
except AssertionError:
os.remove(name)
try:
# Non existing file get's touched
if os.path.isfile(name):
# left over
os.remove(name)
ret = self.run_state(
'file.prepend', name=name, text='cheese', makedirs=True
)
self.assertSaltTrueReturn(ret)
finally:
if os.path.isfile(name):
os.remove(name)
# Nested directory and file get's touched
name = os.path.join(integration.TMP, 'issue_27401', fname)
try:
ret = self.run_state(
'file.prepend', name=name, text='cheese', makedirs=True
)
self.assertSaltTrueReturn(ret)
finally:
if os.path.isfile(name):
os.remove(name)
try:
# Parent directory exists but file does not and makedirs is False
ret = self.run_state(
'file.prepend', name=name, text='cheese'
)
self.assertSaltTrueReturn(ret)
self.assertTrue(os.path.isfile(name))
finally:
shutil.rmtree(
os.path.join(integration.TMP, 'issue_27401'),
ignore_errors=True
)
def test_touch(self):
'''
file.touch