Debian Codegraph @

testing/test_legacypath.py

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