Skip to content

Commit

Permalink
Moving chdir to xonsh.tools (xonsh#5073)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Mar 2, 2023
1 parent ec37809 commit 237e231
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
23 changes: 23 additions & 0 deletions news/tools_chdir.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added ``chdir`` to ``xonsh.tools``. This allows to use ``with chdir("dir"):`` to run commands block in the certain directory without manually cd-ing.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
10 changes: 1 addition & 9 deletions tests/test_dirstack.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
"""Testing dirstack"""

import os
from contextlib import contextmanager

import pytest # noqa F401

from xonsh import dirstack
from xonsh.tools import chdir

HERE = os.path.abspath(os.path.dirname(__file__))
PARENT = os.path.dirname(HERE)


@contextmanager
def chdir(adir):
old_dir = os.getcwd()
os.chdir(adir)
yield
os.chdir(old_dir)


def test_simple(xession):
xession.env.update(dict(CDPATH=PARENT, PWD=PARENT))
with chdir(PARENT):
Expand Down
9 changes: 9 additions & 0 deletions xonsh/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import traceback
import typing as tp
import warnings
from contextlib import contextmanager

# adding imports from further xonsh modules is discouraged to avoid circular
# dependencies
Expand All @@ -53,6 +54,14 @@
)


@contextmanager
def chdir(adir):
old_dir = os.getcwd()
os.chdir(adir)
yield
os.chdir(old_dir)


@functools.lru_cache(1)
def is_superuser():
if ON_WINDOWS:
Expand Down

0 comments on commit 237e231

Please sign in to comment.