From 156765c343942233bf6cbfa59391ec63d6fedc29 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 22 Jan 2021 09:18:45 -0800 Subject: [PATCH] Allow passing PathLike types to Session.chdir() (#376) os.chdir() has supported PathLib since Python 3.6. Discovered while adding types pip's noxfile.py: https://github.com/pypa/pip/pull/9411 --- nox/sessions.py | 2 +- tests/test_sessions.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/nox/sessions.py b/nox/sessions.py index 396a09fc..d44f10b9 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -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) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index e1e19eae..326133e5 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -18,6 +18,7 @@ import os import sys import tempfile +from pathlib import Path from unittest import mock import nox.command @@ -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()