xref: /titanic_41/usr/src/lib/libshell/common/tests/alias.sh (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30alias foo='print hello'
31if	[[ $(foo) != hello ]]
32then	err_exit 'foo, where foo is alias for "print hello" failed'
33fi
34if	[[ $(foo world) != 'hello world' ]]
35then	err_exit 'foo world, where foo is alias for "print hello" failed'
36fi
37alias foo='print hello '
38alias bar=world
39if	[[ $(foo bar) != 'hello world' ]]
40then	err_exit 'foo bar, where foo is alias for "print hello " failed'
41fi
42if	[[ $(foo \bar) != 'hello bar' ]]
43then	err_exit 'foo \bar, where foo is alias for "print hello " failed'
44fi
45alias bar='foo world'
46if	[[ $(bar) != 'hello world' ]]
47then	err_exit 'bar, where bar is alias for "foo world" failed'
48fi
49if	[[ $(alias bar) != "bar='foo world'" ]]
50then	err_exit 'alias bar, where bar is alias for "foo world" failed'
51fi
52unalias foo  || err_exit  "unalias foo failed"
53alias foo 2> /dev/null  && err_exit "alias for non-existent alias foo returns true"
54unset bar
55alias bar="print foo$bar"
56bar=bar
57if	[[ $(bar) != foo ]]
58then	err_exit 'alias bar, where bar is alias for "print foo$bar" failed'
59fi
60unset bar
61alias bar='print hello'
62if	[[ $bar != '' ]]
63then	err_exit 'alias bar cause variable bar to be set'
64fi
65alias !!=print
66if	[[ $(!! hello 2>/dev/null) != hello ]]
67then	err_exit 'alias for !!=print not working'
68fi
69alias foo=echo
70if	[[ $(print  "$(foo bar)" ) != bar  ]]
71then	err_exit 'alias in command substitution not working'
72fi
73( unalias foo)
74if	[[ $(foo bar 2> /dev/null)  != bar  ]]
75then	err_exit 'alias not working after unalias in subshell'
76fi
77builtin -d rm 2> /dev/null
78if	whence rm > /dev/null
79then	[[ ! $(alias -t | grep rm= ) ]] && err_exit 'tracked alias not set'
80	PATH=$PATH
81	[[ $(alias -t | grep rm= ) ]] &&  err_exit 'tracked alias not cleared'
82fi
83exit $((Errors))
84