Skip to content

Commit 6fc4742

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 6fc4742

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

test/test_util.py

Lines changed: 25 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,32 @@ 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+
try:
144+
# Try lchmod first, if the platform supports it.
145+
symlink.lchmod(rwx)
146+
except NotImplementedError:
147+
# The platform (probably win32) doesn't support lchmod; fall back to chmod.
148+
symlink.chmod(rwx)
149+
dir2.chmod(rwx)
150+
rmtree(dir2)
151+
152+
request.addfinalizer(preen_dir2)
153+
131154
try:
132155
rmtree(dir2)
133156
except PermissionError:

0 commit comments

Comments
 (0)