Fix file.directory clobbering backupname path with test=True

This commit is contained in:
Erik Johnson 2024-02-21 17:57:36 -06:00 committed by Pedro Algarvio
parent c402aab17c
commit fb86bf3423

View file

@ -3888,13 +3888,25 @@ def directory(
if not force:
return _error(
ret,
"File exists where the backup target {} should go".format(
backupname
),
f"File exists where the backup target {backupname} should go",
)
if __opts__["test"]:
ret["changes"][
"forced"
] = f"Existing file at backup path {backupname} would be removed"
else:
__salt__["file.remove"](backupname)
os.rename(name, backupname)
if __opts__["test"]:
ret["changes"]["backup"] = f"{name} would be renamed to {backupname}"
ret["changes"][name] = {"directory": "new"}
ret[
"comment"
] = f"{name} would be backed up and replaced with a new directory"
ret["result"] = None
return ret
else:
os.rename(name, backupname)
elif force:
# Remove whatever is in the way
if os.path.isfile(name):