Skip to content

Commit

Permalink
path.local: as_cwd: do not chdir to None [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Feb 22, 2019
1 parent 7c2b288 commit 623b425
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- add ``"importlib"`` pyimport mode for python3.5+, allowing unimportable test suites
to contain identically named modules.

- fix ``LocalPath.as_cwd()`` not calling ``os.chdir()`` with ``None``, when
being invoked from a non-existing directory.

1.7.0 (2018-10-11)
==================

Expand Down
9 changes: 6 additions & 3 deletions py/_path/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,17 @@ def chdir(self):

@contextmanager
def as_cwd(self):
""" return context manager which changes to current dir during the
managed "with" context. On __enter__ it returns the old dir.
"""
Return a context manager, which changes to the path's dir during the
managed "with" context.
On __enter__ it returns the old dir, which might be ``None``.
"""
old = self.chdir()
try:
yield old
finally:
old.chdir()
if old is not None:
old.chdir()

def realpath(self):
""" return a new path which contains no symbolic links."""
Expand Down
13 changes: 13 additions & 0 deletions testing/path/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ def test_chdir_gone(self, path1):
assert path1.chdir() is None
assert os.getcwd() == str(path1)

with pytest.raises(py.error.ENOENT):
with p.as_cwd():
raise NotImplementedError

@skiponwin32
def test_chdir_gone_in_as_cwd(self, path1):
p = path1.ensure("dir_to_be_removed", dir=1)
p.chdir()
p.remove()

with path1.as_cwd() as old:
assert old is None

def test_as_cwd(self, path1):
dir = path1.ensure("subdir", dir=1)
old = py.path.local()
Expand Down

0 comments on commit 623b425

Please sign in to comment.