Permalink
Browse files

Implement cd -P and -L; respect -- (#109)

-P uses os.path.realpath().
  • Loading branch information...
BatmanAoD authored and andychu committed May 18, 2018
1 parent 65e487b commit 179be3f4f3ca45a2251f9f0b6fd17f2f94a5be70
Showing with 70 additions and 6 deletions.
  1. +17 −6 core/builtin.py
  2. +53 −0 spec/builtins.test.sh
View
@@ -322,7 +322,7 @@ def Jobs(argv, job_state):
# - If there are more words than names, the remaining words and their
# intervening delimiters are assigned to the last name.
# - If there are fewer words read from the input stream than names, the
# remaining names are assigned empty values.
# remaining names are assigned empty values.
# - The characters in the value of the IFS variable are used to split the line
# into words using the same rules the shell uses for expansion (described
# above in Word Splitting).
@@ -484,11 +484,16 @@ def Shift(argv, mem):
return mem.Shift(n)
CD_SPEC = _Register('cd')
CD_SPEC.ShortFlag('-L')
CD_SPEC.ShortFlag('-P')
def Cd(argv, mem, dir_stack):
# TODO: Parse flags, error checking, etc.
arg, i = CD_SPEC.Parse(argv)
# TODO: error checking, etc.
try:
dest_dir = argv[0]
dest_dir = argv[i]
except IndexError:
val = mem.GetVar('HOME')
if val.tag == value_e.Undef:
@@ -526,7 +531,13 @@ def Cd(argv, mem, dir_stack):
util.error("cd %r: %s", dest_dir, os.strerror(e.errno))
return 1
state.SetGlobalString(mem, 'PWD', dest_dir) # Set $PWD.
# Set $PWD.
# `-L` is the default behavior; no need to check it
# TODO: ensure that if multiple flags are provided, the *last* one overrides
# the others
pwd = os.path.realpath(dest_dir) if arg.P else dest_dir
state.SetGlobalString(mem, 'PWD', pwd)
dir_stack.Reset() # for pushd/popd/dirs
return 0
@@ -830,7 +841,7 @@ def _ResolveNames(names, funcs, path_val):
results.append(kind)
return results
COMMAND_SPEC = _Register('command')
COMMAND_SPEC.ShortFlag('-v')
@@ -1088,7 +1099,7 @@ def Umask(argv):
# NOTE: dash disables interrupts around the two umask() calls, but that
# shouldn't be a concern for us. Signal handlers won't call umask().
mask = os.umask(0)
os.umask(mask) #
os.umask(mask) #
print('0%03o' % mask) # octal format
return 0
View
@@ -54,6 +54,59 @@ cd-nonexistent
status=0
## END
### cd permits double bare dash
cd -- /
echo $PWD
# stdout: /
### cd to non-symlink with `-P`
targ=$TMP/cd-symtarget
lnk=$TMP/cd-symlink
mkdir -p $targ
ln -s $targ $lnk
cd -P $targ
test $PWD = "$TMP/cd-symtarget" && echo OK
cd $TMP
rmdir $targ
rm $lnk
# stdout: OK
### cd to symlink default behavior
targ=$TMP/cd-symtarget
lnk=$TMP/cd-symlink
mkdir -p $targ
ln -s $targ $lnk
cd $lnk
test $PWD = "$TMP/cd-symlink" && echo OK
cd $TMP
rmdir $targ
rm $lnk
# stdout: OK
### cd to symlink with `-L`
targ=$TMP/cd-symtarget
lnk=$TMP/cd-symlink
mkdir -p $targ
ln -s $targ $lnk
cd -L $lnk
test $PWD = "$TMP/cd-symlink" && echo OK
cd $TMP
rmdir $targ
rm $lnk
# stdout: OK
### cd to symlink with `-P`
targ=$TMP/cd-symtarget
lnk=$TMP/cd-symlink
mkdir -p $targ
ln -s $targ $lnk
cd -P $lnk
test $PWD = "$TMP/cd-symtarget" && echo OK
cd $TMP
rmdir $targ
rm $lnk
# stdout: OK
### pushd/popd
set -o errexit
cd /

0 comments on commit 179be3f

Please sign in to comment.