xref: /titanic_51/usr/src/lib/libshell/common/tests/alias.sh (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
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
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34alias foo='print hello'
35if	[[ $(foo) != hello ]]
36then	err_exit 'foo, where foo is alias for "print hello" failed'
37fi
38if	[[ $(foo world) != 'hello world' ]]
39then	err_exit 'foo world, where foo is alias for "print hello" failed'
40fi
41alias foo='print hello '
42alias bar=world
43if	[[ $(foo bar) != 'hello world' ]]
44then	err_exit 'foo bar, where foo is alias for "print hello " failed'
45fi
46if	[[ $(foo \bar) != 'hello bar' ]]
47then	err_exit 'foo \bar, where foo is alias for "print hello " failed'
48fi
49alias bar='foo world'
50if	[[ $(bar) != 'hello world' ]]
51then	err_exit 'bar, where bar is alias for "foo world" failed'
52fi
53if	[[ $(alias bar) != "bar='foo world'" ]]
54then	err_exit 'alias bar, where bar is alias for "foo world" failed'
55fi
56unalias foo  || err_exit  "unalias foo failed"
57alias foo 2> /dev/null  && err_exit "alias for non-existent alias foo returns true"
58unset bar
59alias bar="print foo$bar"
60bar=bar
61if	[[ $(bar) != foo ]]
62then	err_exit 'alias bar, where bar is alias for "print foo$bar" failed'
63fi
64unset bar
65alias bar='print hello'
66if	[[ $bar != '' ]]
67then	err_exit 'alias bar cause variable bar to be set'
68fi
69alias !!=print
70if	[[ $(!! hello 2>/dev/null) != hello ]]
71then	err_exit 'alias for !!=print not working'
72fi
73alias foo=echo
74if	[[ $(print  "$(foo bar)" ) != bar  ]]
75then	err_exit 'alias in command substitution not working'
76fi
77( unalias foo)
78if	[[ $(foo bar 2> /dev/null)  != bar  ]]
79then	err_exit 'alias not working after unalias in subshell'
80fi
81builtin -d rm 2> /dev/null
82if	whence rm > /dev/null
83then	[[ ! $(alias -t | grep rm= ) ]] && err_exit 'tracked alias not set'
84	PATH=$PATH
85	[[ $(alias -t | grep rm= ) ]] && err_exit 'tracked alias not cleared'
86fi
87if	hash -r 2>/dev/null && [[ ! $(hash) ]]
88then	PATH=$tmp:/bin:/usr/bin
89	for i in foo -foo --
90	do	print ':' > $tmp/$i
91		chmod +x $tmp/$i
92		hash -r -- $i 2>/dev/null || err_exit "hash -r -- $i failed"
93		[[ $(hash) == $i=$tmp/$i ]] || err_exit "hash -r -- $i failed, expected $i=$tmp/$i, got $(hash)"
94	done
95else	err_exit 'hash -r failed'
96fi
97( alias :pr=print) 2> /dev/null || err_exit 'alias beginning with : fails'
98( alias p:r=print) 2> /dev/null || err_exit 'alias with : in name fails'
99exit $((Errors))
100