Fix other tests that do not restore CWD

This commit is contained in:
Sadra Barikbin 2023-08-06 11:35:47 +03:30
parent 8576b61da3
commit 5ffbbef0a5
4 changed files with 22 additions and 16 deletions

View File

@ -618,6 +618,7 @@ class TestLocalPath(CommonFSTests):
@skiponwin32 @skiponwin32
def test_chdir_gone(self, path1): def test_chdir_gone(self, path1):
original_cwd = os.getcwd()
p = path1.ensure("dir_to_be_removed", dir=1) p = path1.ensure("dir_to_be_removed", dir=1)
p.chdir() p.chdir()
p.remove() p.remove()
@ -628,15 +629,18 @@ class TestLocalPath(CommonFSTests):
with pytest.raises(error.ENOENT): with pytest.raises(error.ENOENT):
with p.as_cwd(): with p.as_cwd():
raise NotImplementedError raise NotImplementedError
os.chdir(original_cwd)
@skiponwin32 @skiponwin32
def test_chdir_gone_in_as_cwd(self, path1): def test_chdir_gone_in_as_cwd(self, path1):
original_cwd = os.getcwd()
p = path1.ensure("dir_to_be_removed", dir=1) p = path1.ensure("dir_to_be_removed", dir=1)
p.chdir() p.chdir()
p.remove() p.remove()
with path1.as_cwd() as old: with path1.as_cwd() as old:
assert old is None assert old is None
os.chdir(original_cwd)
def test_as_cwd(self, path1): def test_as_cwd(self, path1):
dir = path1.ensure("subdir", dir=1) dir = path1.ensure("subdir", dir=1)

View File

@ -6,6 +6,7 @@ import types
import pytest import pytest
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.monkeypatch import MonkeyPatch
from _pytest.pathlib import symlink_or_skip from _pytest.pathlib import symlink_or_skip
from _pytest.pytester import Pytester from _pytest.pytester import Pytester
@ -651,7 +652,7 @@ class TestInvocationVariants:
result.stderr.fnmatch_lines(["*not*found*test_missing*"]) result.stderr.fnmatch_lines(["*not*found*test_missing*"])
def test_cmdline_python_namespace_package( def test_cmdline_python_namespace_package(
self, pytester: Pytester, monkeypatch self, pytester: Pytester, monkeypatch: MonkeyPatch
) -> None: ) -> None:
"""Test --pyargs option with namespace packages (#1567). """Test --pyargs option with namespace packages (#1567).
@ -731,6 +732,7 @@ class TestInvocationVariants:
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*test_world.py::test_other*PASSED*", "*1 passed*"] ["*test_world.py::test_other*PASSED*", "*1 passed*"]
) )
monkeypatch.undo()
def test_invoke_test_and_doctestmodules(self, pytester: Pytester) -> None: def test_invoke_test_and_doctestmodules(self, pytester: Pytester) -> None:
p = pytester.makepyfile( p = pytester.makepyfile(

View File

@ -67,22 +67,21 @@ class FilesCompleter:
class TestArgComplete: class TestArgComplete:
@pytest.mark.skipif("sys.platform in ('win32', 'darwin')") @pytest.mark.skipif("sys.platform in ('win32', 'darwin')")
def test_compare_with_compgen( def test_compare_with_compgen(self, tmp_path: Path) -> None:
self, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None:
from _pytest._argcomplete import FastFilesCompleter from _pytest._argcomplete import FastFilesCompleter
ffc = FastFilesCompleter() ffc = FastFilesCompleter()
fc = FilesCompleter() fc = FilesCompleter()
monkeypatch.chdir(tmp_path) with MonkeyPatch.context() as mp:
mp.chdir(tmp_path)
assert equal_with_bash("", ffc, fc, out=sys.stdout) assert equal_with_bash("", ffc, fc, out=sys.stdout)
tmp_path.cwd().joinpath("data").touch() tmp_path.cwd().joinpath("data").touch()
for x in ["d", "data", "doesnotexist", ""]: for x in ["d", "data", "doesnotexist", ""]:
assert equal_with_bash(x, ffc, fc, out=sys.stdout) assert equal_with_bash(x, ffc, fc, out=sys.stdout)
@pytest.mark.skipif("sys.platform in ('win32', 'darwin')") @pytest.mark.skipif("sys.platform in ('win32', 'darwin')")
def test_remove_dir_prefix(self): def test_remove_dir_prefix(self):

View File

@ -244,7 +244,7 @@ class TestCollectFS:
rec = pytester.inline_run("xyz123/test_2.py") rec = pytester.inline_run("xyz123/test_2.py")
rec.assertoutcome(failed=1) rec.assertoutcome(failed=1)
def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: def test_testpaths_ini(self, pytester: Pytester) -> None:
pytester.makeini( pytester.makeini(
""" """
[pytest] [pytest]
@ -275,10 +275,11 @@ class TestCollectFS:
# changing cwd to each subdirectory and running pytest without # changing cwd to each subdirectory and running pytest without
# arguments collects the tests in that directory normally # arguments collects the tests in that directory normally
for dirname in ("a", "b", "c"): with MonkeyPatch.context() as mp:
monkeypatch.chdir(pytester.path.joinpath(dirname)) for dirname in ("a", "b", "c"):
items, reprec = pytester.inline_genitems() mp.chdir(pytester.path.joinpath(dirname))
assert [x.name for x in items] == ["test_%s" % dirname] items, reprec = pytester.inline_genitems()
assert [x.name for x in items] == ["test_%s" % dirname]
class TestCollectPluginHookRelay: class TestCollectPluginHookRelay:
@ -660,7 +661,7 @@ class Test_getinitialnodes:
for parent in col.listchain(): for parent in col.listchain():
assert parent.config is config assert parent.config is config
def test_pkgfile(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: def test_pkgfile(self, pytester: Pytester) -> None:
"""Verify nesting when a module is within a package. """Verify nesting when a module is within a package.
The parent chain should match: Module<x.py> -> Package<subdir> -> Session. The parent chain should match: Module<x.py> -> Package<subdir> -> Session.
Session's parent should always be None. Session's parent should always be None.
@ -669,7 +670,7 @@ class Test_getinitialnodes:
subdir = tmp_path.joinpath("subdir") subdir = tmp_path.joinpath("subdir")
x = ensure_file(subdir / "x.py") x = ensure_file(subdir / "x.py")
ensure_file(subdir / "__init__.py") ensure_file(subdir / "__init__.py")
with monkeypatch.context() as mp: with MonkeyPatch.context() as mp:
mp.chdir(subdir) mp.chdir(subdir)
config = pytester.parseconfigure(x) config = pytester.parseconfigure(x)
col = pytester.getnode(config, x) col = pytester.getnode(config, x)