Skip to content

Commit

Permalink
Allow passing PathLike types to Session.chdir() (#376)
Browse files Browse the repository at this point in the history
os.chdir() has supported PathLib since Python 3.6.

Discovered while adding types pip's noxfile.py:
pypa/pip#9411
  • Loading branch information
jdufresne committed Jan 22, 2021
1 parent 319c796 commit 156765c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def interactive(self) -> bool:
"""Returns True if Nox is being run in an interactive session or False otherwise."""
return not self._runner.global_config.non_interactive and sys.stdin.isatty()

def chdir(self, dir: str) -> None:
def chdir(self, dir: Union[str, os.PathLike]) -> None:
"""Change the current working directory."""
self.log("cd {}".format(dir))
os.chdir(dir)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import sys
import tempfile
from pathlib import Path
from unittest import mock

import nox.command
Expand Down Expand Up @@ -151,6 +152,17 @@ def test_chdir(self, tmpdir):
assert os.getcwd() == cdto
os.chdir(current_cwd)

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

session, _ = self.make_session_and_runner()

session.chdir(Path(cdto))

assert os.getcwd() == cdto
os.chdir(current_cwd)

def test_run_bad_args(self):
session, _ = self.make_session_and_runner()

Expand Down

0 comments on commit 156765c

Please sign in to comment.