mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
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:
parent
71e1f991d3
commit
2142a7b85e
3 changed files with 36 additions and 1 deletions
23
news/chdir_mkdir.rst
Normal file
23
news/chdir_mkdir.rst
Normal 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>
|
|
@ -19,6 +19,16 @@ def test_simple(xession):
|
||||||
assert os.getcwd() == HERE
|
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):
|
def test_cdpath_simple(xession):
|
||||||
xession.env.update(dict(CDPATH=PARENT, PWD=HERE))
|
xession.env.update(dict(CDPATH=PARENT, PWD=HERE))
|
||||||
with chdir(os.path.normpath("/")):
|
with chdir(os.path.normpath("/")):
|
||||||
|
|
|
@ -57,8 +57,10 @@ from xonsh.platform import (
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def chdir(adir):
|
def chdir(adir, mkdir=False):
|
||||||
old_dir = os.getcwd()
|
old_dir = os.getcwd()
|
||||||
|
if mkdir:
|
||||||
|
os.makedirs(adir, exist_ok=True)
|
||||||
os.chdir(adir)
|
os.chdir(adir)
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
|
|
Loading…
Add table
Reference in a new issue