Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/testing/test_legacypath.py

raw
1 # mypy: allow-untyped-defs
2 from pathlib import Path
3
4 from _pytest.compat import LEGACY_PATH
5 from _pytest.fixtures import TopRequest
6 from _pytest.legacypath import TempdirFactory
7 from _pytest.legacypath import Testdir
8 import pytest
9
10
11 def test_item_fspath(pytester: pytest.Pytester) -> None:
12 pytester.makepyfile("def test_func(): pass")
13 items, hookrec = pytester.inline_genitems()
14 assert len(items) == 1
15 (item,) = items
16 items2, hookrec = pytester.inline_genitems(item.nodeid)
17 (item2,) = items2
18 assert item2.name == item.name
19 assert item2.fspath == item.fspath
20 assert item2.path == item.path
21
22
23 def test_testdir_testtmproot(testdir: Testdir) -> None:
24 """Check test_tmproot is a py.path attribute for backward compatibility."""
25 assert testdir.test_tmproot.check(dir=1)
26
27
28 def test_testdir_makefile_dot_prefixes_extension_silently(
29 testdir: Testdir,
30 ) -> None:
31 """For backwards compat #8192"""
32 p1 = testdir.makefile("foo.bar", "")
33 assert ".foo.bar" in str(p1)
34
35
36 def test_testdir_makefile_ext_none_raises_type_error(testdir: Testdir) -> None:
37 """For backwards compat #8192"""
38 with pytest.raises(TypeError):
39 testdir.makefile(None, "")
40
41
42 def test_testdir_makefile_ext_empty_string_makes_file(testdir: Testdir) -> None:
43 """For backwards compat #8192"""
44 p1 = testdir.makefile("", "")
45 assert "test_testdir_makefile" in str(p1)
46
47
48 def attempt_symlink_to(path: str, to_path: str) -> None:
49 """Try to make a symlink from "path" to "to_path", skipping in case this platform
50 does not support it or we don't have sufficient privileges (common on Windows)."""
51 try:
52 Path(path).symlink_to(Path(to_path))
53 except OSError:
54 pytest.skip("could not create symbolic link")
55
56
57 def test_tmpdir_factory(
58 tmpdir_factory: TempdirFactory,
59 tmp_path_factory: pytest.TempPathFactory,
60 ) -> None:
61 assert str(tmpdir_factory.getbasetemp()) == str(tmp_path_factory.getbasetemp())
62 dir = tmpdir_factory.mktemp("foo")
63 assert dir.exists()
64
65
66 def test_tmpdir_equals_tmp_path(tmpdir: LEGACY_PATH, tmp_path: Path) -> None:
67 assert Path(tmpdir) == tmp_path
68
69
70 def test_tmpdir_always_is_realpath(pytester: pytest.Pytester) -> None:
71 # See test_tmp_path_always_is_realpath.
72 realtemp = pytester.mkdir("myrealtemp")
73 linktemp = pytester.path.joinpath("symlinktemp")
74 attempt_symlink_to(str(linktemp), str(realtemp))
75 p = pytester.makepyfile(
76 """
77 def test_1(tmpdir):
78 import os
79 assert os.path.realpath(str(tmpdir)) == str(tmpdir)
80 """
81 )
82 result = pytester.runpytest("-s", p, "--basetemp=%s/bt" % linktemp)
83 assert not result.ret
84
85
86 def test_cache_makedir(cache: pytest.Cache) -> None:
87 dir = cache.makedir("foo") # type: ignore[attr-defined]
88 assert dir.exists()
89 dir.remove()
90
91
92 def test_fixturerequest_getmodulepath(pytester: pytest.Pytester) -> None:
93 modcol = pytester.getmodulecol("def test_somefunc(): pass")
94 (item,) = pytester.genitems([modcol])
95 assert isinstance(item, pytest.Function)
96 req = TopRequest(item, _ispytest=True)
97 assert req.path == modcol.path
98 assert req.fspath == modcol.fspath # type: ignore[attr-defined]
99
100
101 class TestFixtureRequestSessionScoped:
102 @pytest.fixture(scope="session")
103 def session_request(self, request):
104 return request
105
106 def test_session_scoped_unavailable_attributes(self, session_request):
107 with pytest.raises(
108 AttributeError,
109 match="path not available in session-scoped context",
110 ):
111 _ = session_request.fspath
112
113
114 @pytest.mark.parametrize("config_type", ["ini", "pyproject"])
115 def test_addini_paths(pytester: pytest.Pytester, config_type: str) -> None:
116 pytester.makeconftest(
117 """
118 def pytest_addoption(parser):
119 parser.addini("paths", "my new ini value", type="pathlist")
120 parser.addini("abc", "abc value")
121 """
122 )
123 if config_type == "ini":
124 inipath = pytester.makeini(
125 """
126 [pytest]
127 paths=hello world/sub.py
128 """
129 )
130 elif config_type == "pyproject":
131 inipath = pytester.makepyprojecttoml(
132 """
133 [tool.pytest.ini_options]
134 paths=["hello", "world/sub.py"]
135 """
136 )
137 config = pytester.parseconfig()
138 values = config.getini("paths")
139 assert len(values) == 2
140 assert values[0] == inipath.parent.joinpath("hello")
141 assert values[1] == inipath.parent.joinpath("world/sub.py")
142 pytest.raises(ValueError, config.getini, "other")
143
144
145 def test_override_ini_paths(pytester: pytest.Pytester) -> None:
146 pytester.makeconftest(
147 """
148 def pytest_addoption(parser):
149 parser.addini("paths", "my new ini value", type="pathlist")"""
150 )
151 pytester.makeini(
152 """
153 [pytest]
154 paths=blah.py"""
155 )
156 pytester.makepyfile(
157 r"""
158 def test_overridden(pytestconfig):
159 config_paths = pytestconfig.getini("paths")
160 print(config_paths)
161 for cpf in config_paths:
162 print('\nuser_path:%s' % cpf.basename)
163 """
164 )
165 result = pytester.runpytest("--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s")
166 result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])
167
168
169 def test_inifile_from_cmdline_main_hook(pytester: pytest.Pytester) -> None:
170 """Ensure Config.inifile is available during pytest_cmdline_main (#9396)."""
171 p = pytester.makeini(
172 """
173 [pytest]
174 """
175 )
176 pytester.makeconftest(
177 """
178 def pytest_cmdline_main(config):
179 print("pytest_cmdline_main inifile =", config.inifile)
180 """
181 )
182 result = pytester.runpytest_subprocess("-s")
183 result.stdout.fnmatch_lines(f"*pytest_cmdline_main inifile = {p}")