Fixed issue with forward slashes on Windows with file.recurse

This commit is contained in:
Twangboy 2022-10-03 17:45:15 -06:00 committed by Megan Wilhite
parent e7fdbf6709
commit 906fddf4e0
3 changed files with 77 additions and 15 deletions

1
changelog/62179.fixed Normal file
View file

@ -0,0 +1 @@
Fixed issue with forward slashes on Windows with file.recurse and clean=True

View file

@ -667,32 +667,18 @@ 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
"""
case_keep = None
if salt.utils.files.case_insensitive_filesystem():
# Create a case-sensitive dict before doing comparisons
# if file system is case sensitive
case_keep = keep
root = os.path.normcase(root)
real_keep = _find_keep_files(root, keep)
removed = set()
def _delete_not_kept(nfn):
if nfn not in real_keep:
if os.path.normcase(nfn) not in real_keep:
# -- check if this is a part of exclude_pat(only). No need to
# check include_pat
if not salt.utils.stringutils.check_include_exclude(
os.path.relpath(nfn, root), None, exclude_pat
):
return
# Before we can accurately assess the removal of a file, we must
# check for systems with case sensitive files. If we originally
# meant to keep a file, but due to case sensitivity python would
# otherwise remove the file, check against the original list.
if case_keep:
for item in case_keep:
if item.casefold() == nfn.casefold():
return
removed.add(nfn)
if not __opts__["test"]:
try:

View file

@ -0,0 +1,75 @@
"""
Tests for _clean_dir function
"""
import pytest
import salt.states.file as file
pytestmark = [
pytest.mark.windows_whitelisted,
]
def test_normal():
expected = []
result = file._clean_dir(
root=r"/tmp/parent_folder",
keep=[
r"/tmp/parent_folder/meh-1.txt",
r"/tmp/parent_folder/meh-2.txt"
],
exclude_pat=None,
)
assert result == expected
def test_win_forward_slash():
expected = []
result = file._clean_dir(
root=r"C:/test/parent_folder",
keep=[
r"C:/test/parent_folder/meh-1.txt",
r"C:/test/parent_folder/meh-2.txt"
],
exclude_pat=None,
)
assert result == expected
def test_win_forward_slash_mixed_case():
expected = []
result = file._clean_dir(
root=r"C:/test/parent_folder",
keep=[
r"C:/test/parent_folder/meh-1.txt",
r"C:/test/Parent_folder/Meh-2.txt"
],
exclude_pat=None,
)
assert result == expected
def test_win_back_slash():
expected = []
result = file._clean_dir(
root=r"C:\test\parent_folder",
keep=[
r"C:\test\parent_folder\meh-1.txt",
r"C:\test\parent_folder\meh-2.txt"
],
exclude_pat=None,
)
assert result == expected
def test_win_back_slash_mixed_cased():
expected = []
result = file._clean_dir(
root=r"C:\test\parent_folder",
keep=[
r"C:\test\parent_folder\meh-1.txt",
r"C:\test\Parent_folder\Meh-2.txt"
],
exclude_pat=None,
)
assert result == expected