mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #46632 from dwoz/file-recurse-36802
Fix file.recurse w/ clean=True #36802
This commit is contained in:
commit
af45c49c42
2 changed files with 49 additions and 10 deletions
|
@ -607,28 +607,34 @@ def _check_file(name):
|
|||
return ret, msg
|
||||
|
||||
|
||||
def _clean_dir(root, keep, exclude_pat):
|
||||
def _find_keep_files(root, keep):
|
||||
'''
|
||||
Clean out all of the files and directories in a directory (root) while
|
||||
preserving the files in a list (keep) and part of exclude_pat
|
||||
Compile a list of valid keep files (and directories).
|
||||
'''
|
||||
removed = set()
|
||||
real_keep = set()
|
||||
real_keep.add(root)
|
||||
if isinstance(keep, list):
|
||||
for fn_ in keep:
|
||||
if not os.path.isabs(fn_):
|
||||
continue
|
||||
fn_ = os.path.normcase(os.path.abspath(fn_))
|
||||
real_keep.add(fn_)
|
||||
while True:
|
||||
fn_ = os.path.dirname(fn_)
|
||||
fn_ = os.path.abspath(os.path.dirname(fn_))
|
||||
real_keep.add(fn_)
|
||||
if fn_ in [
|
||||
os.sep,
|
||||
''.join([os.path.splitdrive(fn_)[0], os.sep]),
|
||||
''.join([os.path.splitdrive(fn_)[0], os.sep, os.sep])
|
||||
]:
|
||||
drive, path = os.path.splitdrive(fn_)
|
||||
if not path.lstrip(os.sep):
|
||||
break
|
||||
return real_keep
|
||||
|
||||
|
||||
def _clean_dir(root, keep, exclude_pat):
|
||||
'''
|
||||
Clean out all of the files and directories in a directory (root) while
|
||||
preserving the files in a list (keep) and part of exclude_pat
|
||||
'''
|
||||
real_keep = _find_keep_files(root, keep)
|
||||
removed = set()
|
||||
|
||||
def _delete_not_kept(nfn):
|
||||
if nfn not in real_keep:
|
||||
|
|
|
@ -1843,3 +1843,36 @@ class TestFileState(TestCase, LoaderModuleMockMixin):
|
|||
run_checks(test=True)
|
||||
run_checks(strptime_format=fake_strptime_format)
|
||||
run_checks(strptime_format=fake_strptime_format, test=True)
|
||||
|
||||
|
||||
class TestFindKeepFiles(TestCase):
|
||||
|
||||
@skipIf(salt.utils.is_windows(), 'Do not run on Windows')
|
||||
def test__find_keep_files_unix(self):
|
||||
keep = filestate._find_keep_files(
|
||||
'/test/parent_folder',
|
||||
['/test/parent_folder/meh.txt']
|
||||
)
|
||||
expected = [
|
||||
'/',
|
||||
'/test',
|
||||
'/test/parent_folder',
|
||||
'/test/parent_folder/meh.txt',
|
||||
]
|
||||
actual = sorted(list(keep))
|
||||
assert actual == expected, actual
|
||||
|
||||
@skipIf(not salt.utils.is_windows(), 'Only run on Windows')
|
||||
def test__find_keep_files_win32(self):
|
||||
keep = filestate._find_keep_files(
|
||||
'c:\\test\\parent_folder',
|
||||
['C:\\test\\parent_folder\\meh-2.txt']
|
||||
)
|
||||
expected = [
|
||||
'c:\\',
|
||||
'c:\\test',
|
||||
'c:\\test\\parent_folder',
|
||||
'c:\\test\\parent_folder\\meh-2.txt'
|
||||
]
|
||||
actual = sorted(list(keep))
|
||||
assert actual == expected, actual
|
||||
|
|
Loading…
Add table
Reference in a new issue