Permalink
Browse files

New spec tests for 'alias' and 'unalias' builtins, and made them pass.

This is mostly about the argument syntax and error status.  The actual
alias expansion still needs work.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 2, 2018
1 parent 76425d2 commit f7e096dcbe0c56aba1b4eacef095493b900ebc17
Showing with 99 additions and 27 deletions.
  1. +16 −10 core/builtin.py
  2. +83 −17 spec/alias.test.sh
View
@@ -973,6 +973,7 @@ def DeclareTypeset(argv, mem, funcs):
val = mem.GetVar(name)
if val.tag != value_e.Undef:
# TODO: Print flags.
print(name)
else:
status = 1
@@ -989,43 +990,48 @@ def DeclareTypeset(argv, mem, funcs):
ALIAS_SPEC = _Register('alias')
# TODO: Test out all the details here
def Alias(argv, aliases):
if not argv:
for name, alias_exp in aliases.iteritems():
# TODO: Print this better
print('%s %s' % (name, alias_exp))
for name in sorted(aliases):
alias_exp = aliases[name]
# This is somewhat like bash, except we use %r for ''.
print('alias %s=%r' % (name, alias_exp))
return 0
status = 0
for arg in argv:
parts = arg.split('=', 1)
if len(parts) == 1:
if len(parts) == 1: # if we get a plain word without, print alias
name = parts[0]
alias_exp = aliases.get(name)
if alias_exp is None:
print('alias %r is not defined' % name) # TODO: error?
util.error('alias %r is not defined', name) # TODO: error?
status = 1
else:
print('%s %s' % (name, alias_exp))
print('alias %s=%r' % (name, alias_exp))
else:
name, alias_exp = parts
aliases[name] = alias_exp
#print(argv)
#log('AFTER ALIAS %s', aliases)
return 0
return status
UNALIAS_SPEC = _Register('unalias')
# TODO: Test out all the details here
def UnAlias(argv, aliases):
if not argv:
util.usage('unalias NAME...')
return 2
status = 0
for name in argv:
try:
del aliases[name]
except KeyError:
print('alias %r is not defined' % name) # TODO: error?
util.error('alias %r is not defined', name)
status = 1
return status
View
@@ -18,6 +18,89 @@ hi
expected failure
## END
#### defining multiple aliases, then unalias
shopt -s expand_aliases # bash requires this
x=x
y=y
alias echo-x='echo $x' echo-y='echo $y'
echo status=$?
echo-x X
echo-y Y
unalias echo-x echo-y
echo status=$?
echo-x X || echo undefined
echo-y Y || echo undefined
## STDOUT:
status=0
x X
y Y
status=0
undefined
undefined
## END
#### alias not defined
alias e='echo' nonexistentZ
echo status=$?
## STDOUT:
status=1
## END
## OK mksh STDOUT:
nonexistentZ alias not found
status=1
## END
#### unalias not defined
alias e=echo ll='ls -l'
unalias e nonexistentZ ll
echo status=$?
## STDOUT:
status=1
## END
#### listing given aliases
alias e=echo ll='ls -l'
alias e ll
## STDOUT:
alias e='echo'
alias ll='ls -l'
## END
## OK mksh/zsh STDOUT:
e=echo
ll='ls -l'
## END
## OK dash STDOUT:
e='echo'
ll='ls -l'
## END
#### alias without args lists all aliases
alias ex=exit ll='ls -l'
alias | grep -E 'ex=|ll=' # need to grep because mksh/zsh have builtin aliases
echo status=$?
## STDOUT:
alias ex='exit'
alias ll='ls -l'
status=0
## END
## OK dash STDOUT:
ex='exit'
ll='ls -l'
status=0
## END
## OK mksh/zsh STDOUT:
ex=exit
ll='ls -l'
status=0
## END
#### unalias without args is a usage error
unalias
echo status=$?
## stdout: status=2
## BUG mksh/dash stdout: status=0
## BUG zsh stdout: status=1
#### alias with trailing space causes alias expansion on second word
shopt -s expand_aliases # bash requires this
@@ -89,23 +172,6 @@ echo-x echo-x
x echo x
## END
#### defining multiple aliases, then unalias
shopt -s expand_aliases # bash requires this
x=x
y=y
alias echo-x='echo $x' echo-y='echo $y'
echo-x X
echo-y Y
unalias echo-x echo-y
echo-x X || echo undefined
echo-y Y || echo undefined
## STDOUT:
x X
y Y
undefined
undefined
## END
#### Invalid syntax of alias
shopt -s expand_aliases # bash requires this

0 comments on commit f7e096d

Please sign in to comment.