Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Session.chdir() a context manager #543

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 32 additions & 3 deletions nox/sessions.py
Expand Up @@ -21,6 +21,7 @@
import sys
import unicodedata
import warnings
from types import TracebackType
from typing import (
Any,
Callable,
Expand All @@ -31,6 +32,7 @@
Optional,
Sequence,
Tuple,
Type,
Union,
)

Expand Down Expand Up @@ -210,10 +212,37 @@ def invoked_from(self) -> str:
"""
return self._runner.global_config.invoked_from

def chdir(self, dir: Union[str, os.PathLike]) -> None:
"""Change the current working directory."""
class _WorkingDirContext:
Tolker-KU marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, dir: Union[str, os.PathLike]) -> None:
self._prev_working_dir = os.getcwd()
os.chdir(dir)

def __enter__(self) -> "Session._WorkingDirContext":
return self

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
os.chdir(self._prev_working_dir)

def chdir(self, dir: Union[str, os.PathLike]) -> "Session._WorkingDirContext":
"""Change the current working directory.

Can be used as a context manager to automatically restore the working directory::

with session.chdir("somewhere/deep/in/monorepo"):
# Runs in "/somewhere/deep/in/monorepo"
session.run("pytest")

# Runs in original working directory
session.run("flake8")

"""
self.log(f"cd {dir}")
os.chdir(dir)
return Session._WorkingDirContext(dir)

cd = chdir
"""An alias for :meth:`chdir`."""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_sessions.py
Expand Up @@ -164,6 +164,19 @@ def test_chdir(self, tmpdir):
assert os.getcwd() == cdto
os.chdir(current_cwd)

def test_chdir_ctx(self, tmpdir):
cdto = str(tmpdir.join("cdbby").ensure(dir=True))
current_cwd = os.getcwd()

session, _ = self.make_session_and_runner()

with session.chdir(cdto):
assert os.getcwd() == cdto

assert os.getcwd() == current_cwd

os.chdir(current_cwd)

def test_invoked_from(self, tmpdir):
cdto = str(tmpdir.join("cdbby").ensure(dir=True))
current_cwd = os.getcwd()
Expand Down