fixes saltstack/salt#62986 fix file.tidied FileNotFoundError

This commit is contained in:
nicholasmhughes 2022-10-31 14:52:58 -04:00 committed by Megan Wilhite
parent 20bbbf88ba
commit 42e6297433
3 changed files with 72 additions and 40 deletions

1
changelog/62986.fixed Normal file
View file

@ -0,0 +1 @@
Fix file.tidied FileNotFoundError

View file

@ -2102,6 +2102,7 @@ def tidied(
mysize = 0
deleteme = True
path = os.path.join(root, elem)
try:
if os.path.islink(path):
# Get timestamp of symlink (not symlinked file)
if time_comparison == "ctime":
@ -2145,6 +2146,8 @@ def tidied(
if compare_age_size and _matches(name=filename) and deleteme:
todelete.append(path)
except FileNotFoundError:
continue
# Now delete the stuff
if todelete:

View file

@ -550,3 +550,31 @@ def test_tidied_age_size_args_AND_operator_size_and_age():
}
assert ret == exp
assert remove.call_count == 3
def test_tidied_filenotfound(tmp_path):
name = tmp_path / "not_found_test"
name.mkdir(parents=True, exist_ok=True)
name = str(tmp_path / "not_found_test")
walker = [
(os.path.join(name, "test1"), [], ["file1"]),
(os.path.join(name, "test2", "test3"), [], []),
(os.path.join(name, "test2"), ["test3"], ["file2"]),
(name, ["test1", "test2"], ["file3"]),
]
# mock the walk, but files aren't there
with patch("os.walk", return_value=walker), patch(
"os.path.islink", return_value=False
):
ret = filestate.tidied(
name=name,
age=1,
size=9,
)
exp = {
"name": name,
"changes": {},
"result": True,
"comment": "Nothing to remove from directory {}".format(name),
}
assert ret == exp