Skip to content

Commit 63bc49d

Browse files
authored
Merge pull request #8440 from bluetech/unnecessary-py-path
Remove/replace some unneeded usages of py.path
2 parents cdbeb03 + 59251e8 commit 63bc49d

File tree

18 files changed

+102
-105
lines changed

18 files changed

+102
-105
lines changed

doc/en/builtin.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a
148148
on warning categories.
149149
150150
tmpdir_factory [session scope]
151-
Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
151+
Return a :class:`pytest.TempdirFactory` instance for the test session.
152152
153153
tmp_path_factory [session scope]
154-
Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
154+
Return a :class:`pytest.TempPathFactory` instance for the test session.
155155
156156
tmpdir
157157
Return a temporary directory path object which is unique to each test

doc/en/example/assertion/test_failures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
pytest_plugins = ("pytester",)
66

77

8-
def test_failure_demo_fails_properly(testdir):
9-
target = testdir.tmpdir.join(os.path.basename(failure_demo))
8+
def test_failure_demo_fails_properly(pytester):
9+
target = pytester.path.joinpath(os.path.basename(failure_demo))
1010
shutil.copy(failure_demo, target)
11-
result = testdir.runpytest(target, syspathinsert=True)
11+
result = pytester.runpytest(target, syspathinsert=True)
1212
result.stdout.fnmatch_lines(["*44 failed*"])
1313
assert result.ret != 0

doc/en/example/multipython.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
@pytest.fixture(params=pythonlist)
15-
def python1(request, tmpdir):
16-
picklefile = tmpdir.join("data.pickle")
15+
def python1(request, tmp_path):
16+
picklefile = tmp_path / "data.pickle"
1717
return Python(request.param, picklefile)
1818

1919

@@ -30,8 +30,8 @@ def __init__(self, version, picklefile):
3030
self.picklefile = picklefile
3131

3232
def dumps(self, obj):
33-
dumpfile = self.picklefile.dirpath("dump.py")
34-
dumpfile.write(
33+
dumpfile = self.picklefile.with_name("dump.py")
34+
dumpfile.write_text(
3535
textwrap.dedent(
3636
r"""
3737
import pickle
@@ -46,8 +46,8 @@ def dumps(self, obj):
4646
subprocess.check_call((self.pythonpath, str(dumpfile)))
4747

4848
def load_and_is_true(self, expression):
49-
loadfile = self.picklefile.dirpath("load.py")
50-
loadfile.write(
49+
loadfile = self.picklefile.with_name("load.py")
50+
loadfile.write_text(
5151
textwrap.dedent(
5252
r"""
5353
import pickle

doc/en/example/simple.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,8 @@ case we just write some information out to a ``failures`` file:
768768
mode = "a" if os.path.exists("failures") else "w"
769769
with open("failures", mode) as f:
770770
# let's also access a fixture for the fun of it
771-
if "tmpdir" in item.fixturenames:
772-
extra = " ({})".format(item.funcargs["tmpdir"])
771+
if "tmp_path" in item.fixturenames:
772+
extra = " ({})".format(item.funcargs["tmp_path"])
773773
else:
774774
extra = ""
775775
@@ -781,7 +781,7 @@ if you then have failing tests:
781781
.. code-block:: python
782782
783783
# content of test_module.py
784-
def test_fail1(tmpdir):
784+
def test_fail1(tmp_path):
785785
assert 0
786786
787787
@@ -804,9 +804,9 @@ and run them:
804804
================================= FAILURES =================================
805805
________________________________ test_fail1 ________________________________
806806
807-
tmpdir = local('PYTEST_TMPDIR/test_fail10')
807+
tmp_path = Path('PYTEST_TMPDIR/test_fail10')
808808
809-
def test_fail1(tmpdir):
809+
def test_fail1(tmp_path):
810810
> assert 0
811811
E assert 0
812812

doc/en/getting-started.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,35 +213,35 @@ Request a unique temporary directory for functional tests
213213

214214
.. code-block:: python
215215
216-
# content of test_tmpdir.py
217-
def test_needsfiles(tmpdir):
218-
print(tmpdir)
216+
# content of test_tmp_path.py
217+
def test_needsfiles(tmp_path):
218+
print(tmp_path)
219219
assert 0
220220
221-
List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:
221+
List the name ``tmp_path`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:
222222

223223
.. code-block:: pytest
224224
225-
$ pytest -q test_tmpdir.py
225+
$ pytest -q test_tmp_path.py
226226
F [100%]
227227
================================= FAILURES =================================
228228
_____________________________ test_needsfiles ______________________________
229229
230-
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
230+
tmp_path = Path('PYTEST_TMPDIR/test_needsfiles0')
231231
232-
def test_needsfiles(tmpdir):
233-
print(tmpdir)
232+
def test_needsfiles(tmp_path):
233+
print(tmp_path)
234234
> assert 0
235235
E assert 0
236236
237237
test_tmpdir.py:3: AssertionError
238238
--------------------------- Captured stdout call ---------------------------
239239
PYTEST_TMPDIR/test_needsfiles0
240240
========================= short test summary info ==========================
241-
FAILED test_tmpdir.py::test_needsfiles - assert 0
241+
FAILED test_tmp_path.py::test_needsfiles - assert 0
242242
1 failed in 0.12s
243243
244-
More info on tmpdir handling is available at :ref:`Temporary directories and files <tmpdir handling>`.
244+
More info on temporary directory handling is available at :ref:`Temporary directories and files <tmpdir handling>`.
245245

246246
Find out what kind of builtin :ref:`pytest fixtures <fixtures>` exist with the command:
247247

doc/en/reference/doctest.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ It is possible to use fixtures using the ``getfixture`` helper:
194194
.. code-block:: text
195195
196196
# content of example.rst
197-
>>> tmp = getfixture('tmpdir')
197+
>>> tmp = getfixture('tmp_path')
198198
>>> ...
199199
>>>
200200

doc/en/reference/reference.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ Example of a fixture requiring another fixture:
284284
.. code-block:: python
285285
286286
@pytest.fixture
287-
def db_session(tmpdir):
288-
fn = tmpdir / "db.file"
289-
return connect(str(fn))
287+
def db_session(tmp_path):
288+
fn = tmp_path / "db.file"
289+
return connect(fn)
290290
291291
For more details, consult the full :ref:`fixtures docs <fixture>`.
292292

doc/en/reference/unittest.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,22 @@ and define the fixture function in the context where you want it used.
190190
Let's look at an ``initdir`` fixture which makes all test methods of a
191191
``TestCase`` class execute in a temporary directory with a
192192
pre-initialized ``samplefile.ini``. Our ``initdir`` fixture itself uses
193-
the pytest builtin :ref:`tmpdir <tmpdir>` fixture to delegate the
193+
the pytest builtin :fixture:`tmp_path` fixture to delegate the
194194
creation of a per-test temporary directory:
195195

196196
.. code-block:: python
197197
198198
# content of test_unittest_cleandir.py
199+
import os
199200
import pytest
200201
import unittest
201202
202203
203204
class MyTest(unittest.TestCase):
204205
@pytest.fixture(autouse=True)
205-
def initdir(self, tmpdir):
206-
tmpdir.chdir() # change to pytest-provided temporary directory
207-
tmpdir.join("samplefile.ini").write("# testdata")
206+
def initdir(self, tmp_path, monkeypatch):
207+
monkeypatch.chdir(tmp_path) # change to pytest-provided temporary directory
208+
tmp_path.joinpath("samplefile.ini").write_text("# testdata")
208209
209210
def test_method(self):
210211
with open("samplefile.ini") as f:

src/_pytest/pytester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def test_something(pytester):
875875
def syspathinsert(
876876
self, path: Optional[Union[str, "os.PathLike[str]"]] = None
877877
) -> None:
878-
"""Prepend a directory to sys.path, defaults to :py:attr:`tmpdir`.
878+
"""Prepend a directory to sys.path, defaults to :attr:`path`.
879879
880880
This is undone automatically when this object dies at the end of each
881881
test.
@@ -964,7 +964,7 @@ def getnode(
964964
"""
965965
session = Session.from_config(config)
966966
assert "::" not in str(arg)
967-
p = legacy_path(arg)
967+
p = Path(os.path.abspath(arg))
968968
config.hook.pytest_sessionstart(session=session)
969969
res = session.perform_collect([str(p)], genitems=False)[0]
970970
config.hook.pytest_sessionfinish(session=session, exitstatus=ExitCode.OK)

src/_pytest/tmpdir.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ def get_user() -> Optional[str]:
166166

167167

168168
def pytest_configure(config: Config) -> None:
169-
"""Create a TempdirFactory and attach it to the config object.
169+
"""Create a TempPathFactory and attach it to the config object.
170170
171171
This is to comply with existing plugins which expect the handler to be
172172
available at pytest_configure time, but ideally should be moved entirely
173-
to the tmpdir_factory session fixture.
173+
to the tmp_path_factory session fixture.
174174
"""
175175
mp = MonkeyPatch()
176176
tmppath_handler = TempPathFactory.from_config(config, _ispytest=True)
@@ -182,14 +182,14 @@ def pytest_configure(config: Config) -> None:
182182

183183
@fixture(scope="session")
184184
def tmpdir_factory(request: FixtureRequest) -> TempdirFactory:
185-
"""Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session."""
185+
"""Return a :class:`pytest.TempdirFactory` instance for the test session."""
186186
# Set dynamically by pytest_configure() above.
187187
return request.config._tmpdirhandler # type: ignore
188188

189189

190190
@fixture(scope="session")
191191
def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
192-
"""Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session."""
192+
"""Return a :class:`pytest.TempPathFactory` instance for the test session."""
193193
# Set dynamically by pytest_configure() above.
194194
return request.config._tmp_path_factory # type: ignore
195195

0 commit comments

Comments
 (0)