OILS / spec / command_.test.sh View on Github | oilshell.org

194 lines, 96 significant
1#
2# Miscellaneous tests for the command language.
3
4#### Command block
5PATH=/bin
6
7{ which ls; }
8## stdout: /bin/ls
9
10#### Permission denied
11touch $TMP/text-file
12$TMP/text-file
13## status: 126
14
15#### Not a dir
16$TMP/not-a-dir/text-file
17## status: 127
18
19#### Name too long
20./0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
21## status: 127
22## OK dash status: 2
23## OK bash status: 126
24
25#### External programs don't have _OVM in environment
26# bug fix for leakage
27env | grep _OVM
28echo status=$?
29## stdout: status=1
30
31#### File with no shebang is executed
32# most shells execute /bin/sh; bash may execute itself
33echo 'echo hi' > $TMP/no-shebang
34chmod +x $TMP/no-shebang
35$SH -c '$TMP/no-shebang'
36## stdout: hi
37## status: 0
38
39#### File with relative path and no shebang is executed
40cd $TMP
41echo 'echo hi' > no-shebang
42chmod +x no-shebang
43"$SH" -c './no-shebang'
44## stdout: hi
45## status: 0
46
47#### File in relative subdirectory and no shebang is executed
48cd $TMP
49mkdir -p test-no-shebang
50echo 'echo hi' > test-no-shebang/script
51chmod +x test-no-shebang/script
52"$SH" -c 'test-no-shebang/script'
53## stdout: hi
54## status: 0
55
56#### $PATH lookup
57cd $TMP
58mkdir -p one two
59echo 'echo one' > one/mycmd
60echo 'echo two' > two/mycmd
61chmod +x one/mycmd two/mycmd
62
63PATH='one:two'
64mycmd
65## STDOUT:
66one
67## END
68
69#### filling $PATH cache, then insert the same command earlier in cache
70cd $TMP
71PATH="one:two:$PATH"
72mkdir -p one two
73rm -f one/* two/*
74echo 'echo two' > two/mycmd
75chmod +x two/mycmd
76mycmd
77
78# Insert earlier in the path
79echo 'echo one' > one/mycmd
80chmod +x one/mycmd
81mycmd # still runs the cached 'two'
82
83# clear the cache
84hash -r
85mycmd # now it runs the new 'one'
86
87## STDOUT:
88two
89two
90one
91## END
92
93# zsh doesn't do caching!
94## OK zsh STDOUT:
95two
96one
97one
98## END
99
100#### filling $PATH cache, then deleting command
101cd $TMP
102PATH="one:two:$PATH"
103mkdir -p one two
104rm -f one/mycmd two/mycmd
105
106echo 'echo two' > two/mycmd
107chmod +x two/mycmd
108mycmd
109echo status=$?
110
111# Insert earlier in the path
112echo 'echo one' > one/mycmd
113chmod +x one/mycmd
114rm two/mycmd
115mycmd # still runs the cached 'two'
116echo status=$?
117
118## STDOUT:
119two
120status=0
121status=127
122## END
123
124# mksh and zsh correctly searches for the executable again!
125## OK zsh/mksh STDOUT:
126two
127status=0
128one
129status=0
130## END
131
132#### Non-executable on $PATH
133
134# shells differ in whether they actually execve('one/cmd') and get EPERM
135
136mkdir -p one two
137PATH="one:two:$PATH"
138
139rm -f one/mycmd two/mycmd
140echo 'echo one' > one/mycmd
141echo 'echo two' > two/mycmd
142
143# only make the second one executable
144chmod +x two/mycmd
145mycmd
146echo status=$?
147
148## STDOUT:
149two
150status=0
151## END
152
153#### hash without args prints the cache
154whoami >/dev/null
155hash
156echo status=$?
157## STDOUT:
158/usr/bin/whoami
159status=0
160## END
161
162# bash uses a weird table. Although we could use TSV2.
163## OK bash stdout-json: "hits\tcommand\n 1\t/usr/bin/whoami\nstatus=0\n"
164
165## OK mksh/zsh STDOUT:
166whoami=/usr/bin/whoami
167status=0
168## END
169
170#### hash with args
171hash whoami
172echo status=$?
173hash | grep -o /whoami # prints it twice
174hash _nonexistent_
175echo status=$?
176## STDOUT:
177status=0
178/whoami
179status=1
180## END
181
182# mksh doesn't fail
183## BUG mksh STDOUT:
184status=0
185/whoami
186status=0
187## END
188
189#### hash -r doesn't allow additional args
190hash -r whoami >/dev/null # avoid weird output with mksh
191echo status=$?
192## stdout: status=1
193## OK osh stdout: status=2
194## BUG dash/bash stdout: status=0