Merge branch 'naufraghi-cdpath'

This commit is contained in:
Anthony Scopatz 2015-07-06 10:12:51 +03:00
commit 93f3832205
6 changed files with 102 additions and 4 deletions

View file

@ -225,6 +225,11 @@ SHELL_TYPE ``'readline'`` Which shell is used.
library installed. To specify
which shell should be used, do
so in the run control file.
CDPATH ``[]`` A list of paths to be used as
roots for a `cd`, breaking
compatibility with bash, xonsh
always prefer an existing
relative path.
================== ============================= ================================
Environment Lookup with ``${}``

1
recipe/bld.bat Normal file
View file

@ -0,0 +1 @@
python setup.py install

View file

@ -1,13 +1,15 @@
package:
name: xonsh
version: "0.1.6"
version: {{ environ['GIT_DESCRIBE_TAG'] }}
source:
fn: xonsh.tar.gz
url: https://github.com/scopatz/xonsh/archive/master.tar.gz
git_url: ../
build:
script: python setup.py install
number: {{ environ.get('GIT_DESCRIBE_NUMBER', 0) }}
entry_points:
- xonsh = xonsh.main:main
requirements:
build:

61
tests/test_dirstack.py Normal file
View file

@ -0,0 +1,61 @@
"""Testing dirstack"""
from __future__ import unicode_literals, print_function
from contextlib import contextmanager
from functools import wraps
import os
import builtins
from nose.tools import assert_equal, assert_not_equal
import nose
from xonsh import dirstack
from xonsh.environ import Env
from xonsh.built_ins import load_builtins
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)
@contextmanager
def xonsh_env(env):
load_builtins()
old_env = builtins.__xonsh_env__
builtins.__xonsh_env__ = env
yield
builtins.__xonsh_env__ = old_env
def test_simple():
load_builtins()
with chdir(PARENT):
assert_not_equal(os.getcwd(), HERE)
dirstack.cd(["tests"])
assert_equal(os.getcwd(), HERE)
def test_cdpath_simple():
with xonsh_env(Env(CDPATH=PARENT)):
with chdir(os.path.normpath("/")):
assert_not_equal(os.getcwd(), HERE)
dirstack.cd(["tests"])
assert_equal(os.getcwd(), HERE)
def test_cdpath_collision():
with xonsh_env(Env(CDPATH=PARENT)):
sub_tests = os.path.join(HERE, "tests")
if not os.path.exists(sub_tests):
os.mkdir(sub_tests)
with chdir(HERE):
assert_equal(os.getcwd(), HERE)
dirstack.cd(["tests"])
assert_equal(os.getcwd(), os.path.join(HERE, "tests"))
if __name__ == '__main__':
nose.runmodule()

View file

@ -134,6 +134,13 @@ class Completer(object):
if prefix == '..':
paths.add('../')
def _add_cdpaths(self, paths, prefix):
"""Completes current prefix using CDPATH"""
env = builtins.__xonsh_env__
for cdp in env.get("CDPATH", []):
for s in iglobpath(os.path.join(cdp, prefix) + '*'):
paths.add(s)
def cmd_complete(self, cmd):
"""Completes a command name based on what is on the $PATH"""
space = ' '
@ -163,7 +170,8 @@ class Completer(object):
paths = {s.replace(home, tilde) for s in paths}
self._add_env(paths, prefix)
self._add_dots(paths, prefix)
return paths
self._add_cdpaths(paths, prefix)
return {os.path.normpath(s) for s in paths}
def bash_complete(self, prefix, line, begidx, endidx):
"""Attempts BASH completion."""

View file

@ -2,6 +2,7 @@
"""
import os
import builtins
from glob import iglob
from argparse import ArgumentParser
DIRSTACK = []
@ -31,6 +32,23 @@ def _change_working_directory(newdir):
env['PWD'] = new
def _try_cdpath(apath):
# NOTE: this CDPATH implementation differs from the bash one.
# In bash if a CDPATH is set, an unqualified local folder
# is considered after all CDPATHs, example:
# CDPATH=$HOME/src (with src/xonsh/ inside)
# $ cd xonsh -> src/xonsh (whith xonsh/xonsh)
# a second $ cd xonsh has no effects, to move in the nested xonsh
# in bash a full $ cd ./xonsh is needed.
# In xonsh a relative folder is allways preferred.
env = builtins.__xonsh_env__
cdpaths = env.get('CDPATH', [])
for cdp in cdpaths:
for cdpath_prefixed_path in iglob(os.path.join(cdp, apath)):
return cdpath_prefixed_path
return apath
def cd(args, stdin=None):
"""Changes the directory.
@ -40,6 +58,7 @@ def cd(args, stdin=None):
env = builtins.__xonsh_env__
oldpwd = env.get('OLDPWD', None)
cwd = _get_cwd()
if len(args) == 0:
d = os.path.expanduser('~')
elif len(args) == 1:
@ -64,6 +83,8 @@ def cd(args, stdin=None):
return '', e.format(len(DIRSTACK))
else:
d = DIRSTACK[num - 1]
else:
d = _try_cdpath(d)
else:
return '', 'cd takes 0 or 1 arguments, not {0}\n'.format(len(args))
if not os.path.exists(d):