tools: added `mkdir to xonsh.tools.chdir` (#5589)

* mkdir chdir

* mkdir chdir

* mkdir chdir

* mkdir chdir

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andy Kipp 2024-07-10 22:49:57 +02:00 committed by GitHub
parent 71e1f991d3
commit 2142a7b85e
Failed to generate hash of commit
3 changed files with 36 additions and 1 deletions

23
news/chdir_mkdir.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* tools: added ``mkdir`` to ``xonsh.tools.chdir`` e.g. ``with chdir('/tmp/new', mkdir=True): pass``.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -19,6 +19,16 @@ def test_simple(xession):
assert os.getcwd() == HERE
def test_chdir_mkdir(tmpdir):
d = str(tmpdir.join("chdir_mkdir"))
with chdir(d, mkdir=True):
assert os.getcwd() == d
assert os.getcwd() != d
with chdir(d, mkdir=True):
# Repeat to check there is no error for existing dir.
assert os.getcwd() == d
def test_cdpath_simple(xession):
xession.env.update(dict(CDPATH=PARENT, PWD=HERE))
with chdir(os.path.normpath("/")):

View file

@ -57,8 +57,10 @@ from xonsh.platform import (
@contextmanager
def chdir(adir):
def chdir(adir, mkdir=False):
old_dir = os.getcwd()
if mkdir:
os.makedirs(adir, exist_ok=True)
os.chdir(adir)
try:
yield