Skip to content

Commit a64bde9

Browse files
committed
test_avoids_changing...: don't leave test artifacts behind
Prior to this the test would fail [silently] on my macOS host during the test and then pytest would complain loudly about it being an issue post-session (regardless of whether or not the test was being run). Squash the unwritable directory to mute noise complaints from pytest. Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
1 parent d966a0d commit a64bde9

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

test/test_util.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_deletes_dir_with_readonly_files(self, tmp_path):
113113
sys.platform == "cygwin",
114114
reason="Cygwin can't set the permissions that make the test meaningful.",
115115
)
116-
def test_avoids_changing_permissions_outside_tree(self, tmp_path):
116+
def test_avoids_changing_permissions_outside_tree(self, tmp_path, request):
117117
# Automatically works on Windows, but on Unix requires either special handling
118118
# or refraining from attempting to fix PermissionError by making chmod calls.
119119

@@ -125,9 +125,27 @@ def test_avoids_changing_permissions_outside_tree(self, tmp_path):
125125

126126
dir2 = tmp_path / "dir2"
127127
dir2.mkdir()
128-
(dir2 / "symlink").symlink_to(dir1 / "file")
128+
symlink = dir2 / "symlink"
129+
symlink.symlink_to(dir1 / "file")
129130
dir2.chmod(stat.S_IRUSR | stat.S_IXUSR)
130131

132+
def preen_dir2():
133+
"""Don't leave unwritable directories behind.
134+
135+
pytest has difficulties cleaning up after the fact on some platforms,
136+
e.g., macOS, and whines incessantly until the issue is resolved--regardless
137+
of the pytest session.
138+
"""
139+
rwx = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
140+
if not dir2.exists():
141+
return
142+
if symlink.exists():
143+
symlink.chmod(rwx)
144+
dir2.chmod(rwx)
145+
rmtree(dir2)
146+
147+
request.addfinalizer(preen_dir2)
148+
131149
try:
132150
rmtree(dir2)
133151
except PermissionError:

0 commit comments

Comments
 (0)