Skip to content

Commit bc92ff1

Browse files
committed
factor out symlink_or_skip
1 parent 3d3f065 commit bc92ff1

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

testing/acceptance_test.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -762,21 +762,13 @@ def test():
762762
result = testdir.runpytest(str(p) + "::test", "--doctest-modules")
763763
result.stdout.fnmatch_lines(["*1 passed*"])
764764

765-
def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
765+
def test_cmdline_python_package_symlink(
766+
self, testdir, monkeypatch, symlink_or_skip
767+
):
766768
"""
767769
test --pyargs option with packages with path containing symlink can
768770
have conftest.py in their package (#2985)
769771
"""
770-
# dummy check that we can actually create symlinks: on Windows `os.symlink` is available,
771-
# but normal users require special admin privileges to create symlinks.
772-
if sys.platform == "win32":
773-
try:
774-
os.symlink(
775-
str(testdir.tmpdir.ensure("tmpfile")),
776-
str(testdir.tmpdir.join("tmpfile2")),
777-
)
778-
except OSError as e:
779-
pytest.skip(str(e.args[0]))
780772
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
781773

782774
dirname = "lib"
@@ -794,7 +786,7 @@ def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
794786

795787
d_local = testdir.mkdir("local")
796788
symlink_location = os.path.join(str(d_local), "lib")
797-
os.symlink(str(d), symlink_location, target_is_directory=True)
789+
symlink_or_skip(str(d), symlink_location, target_is_directory=True)
798790

799791
# The structure of the test directory is now:
800792
# .

testing/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import sys
34
from typing import List
@@ -136,6 +137,24 @@ def testdir(testdir: Testdir) -> Testdir:
136137
return testdir
137138

138139

140+
@pytest.fixture
141+
def symlink_or_skip():
142+
"""Return a function to create symlinks or skip the test.
143+
144+
On Windows `os.symlink` is available, but normal users require special
145+
admin privileges to create symlinks.
146+
"""
147+
148+
def wrap_os_symlink(src, dst, *args, **kwargs):
149+
try:
150+
os.symlink(src, dst, *args, **kwargs)
151+
except OSError as e:
152+
pytest.skip("os.symlink({!r}) failed: {!r}".format((src, dst), e))
153+
assert os.path.islink(dst)
154+
155+
return wrap_os_symlink
156+
157+
139158
@pytest.fixture(scope="session")
140159
def color_mapping():
141160
"""Returns a utility class which can replace keys in strings in the form "{NAME}"

0 commit comments

Comments
 (0)