26754: tweak zgetdir() and test for realpath()
[zsh.git] / Test / V01zmodload.ztst
blobd5e3605ac7dae760d26e4d73793f9451e063f947
1 # Test basic module loading
3 %prep
4 # Figure out which modules it ought to be possible to load by looking at
5 # the config.modules file.  This differs for static vs. dynamic builds.
7  mods=()
8  deps="$(zmodload -Ld)"
9  while read name modfile link auto load funcs
10  do
11    [[ $name == \#* ]] && continue
12    eval "$name $modfile $link $auto $load"
13    [[ $link == no ]] && continue
14    mods=($mods $name)
15    moddeps=
16    modfile=$ZTST_srcdir/../$modfile
17    eval ${${${(f)"$(<$modfile)"}[(r)moddeps=*]}:-:}
18    [[ -n $moddeps ]] && zmodload -d $name $=moddeps
19  done < $ZTST_testdir/../config.modules
21  zmodunload() {
22    local m n=$#
23    (( n == 0 )) && return 0
24    for m
25    do
26      if [[ -z ${(M)${(f)"$(zmodload -d)"}:#*:* $m( *|)} ]]
27      then
28        zmodload -u $m && zmodload -ud $m || return 1
29        shift
30      else
31        set $@[2,-1] $m
32      fi
33    done
34    if (( $# < n ))
35    then
36      zmodunload $*
37    else
38      zmodload -u $*
39    fi
40  }
42  mkdir zmodload.tmp
43  cd zmodload.tmp
45 %test
47 # This first test depends on knowing that zsh is run with +Z from the
48 # Makefile, and that ztst.zsh loads the parameter module.
50  zmodload -L
51 0:List the loaded modules
52 >zmodload zsh/main
53 >zmodload zsh/parameter
55 # You use to need zmodload -i to avoid an error.
56 # That has been deemed pointless, so now an attempt
57 # to load a loaded module should succeed.
58  zmodload zsh/main
59 0:Test reloading an already-loaded module
61 # Loop over the modules found above and attempt to load each one.  Use
62 # the -i flag in case dependencies cause multiple modules to be loaded,
63 # or in case some previous test suite loaded a module.
65  for m in $mods
66  do
67    zmodload -i $m || mods[(r)$m]=()
68  done
69 0d:Test loading of all compiled modules
71  zmodload -e $mods
72 0d:Check that zsh believes the modules did load
74 # Now check for proper failure conditions by trying some operations on
75 # a nonexistent module.
77  zmodload -i bogus/notamodule
78 1D:Check that loading a nonexistent module fails
80  zmodload -u bogus/notamodule
81 1D:Check that unloading a nonexistent module fails
83 # Test adding and removing autoloads, using a nonexistent module.
85  zmodload -ab bogus
86  zmodload -ub bogus
87 0:Add/remove autoloaded builtin
89  zmodload -ub bogus
90 1:Fail to remove unautoloaded builtin
91 ?(eval):zmodload:1: bogus: no such builtin
93  zmodload -ac bogus
94  zmodload -uc bogus
95 0:Add/remove autoloaded condition
97  zmodload -uc bogus
98 1:Fail to remove unautoloaded condition
99 ?(eval):zmodload:1: bogus: no such condition
101  zmodload -ap bogus
102  zmodload -up bogus
103 0:Add/remove autoloaded parameter
105  zmodload -up bogus
106 1:Fail to remove unautoloaded parameter
107 ?(eval):zmodload:1: bogus: no such parameter
109  zmodload -af bogus
110  zmodload -uf bogus
111 0:Add/remove autoloaded math function
113  zmodload -uf bogus
114 1:Fail to remove unautoloaded math function
115 ?(eval):zmodload:1: bogus: no such math function
117 # If the "example" module is available, test various autoloading behavior.
119  if [[ $mods[(r)zsh/example] == zsh/example ]]; then
120    zmodload -u zsh/example
121    zmodload -ab zsh/example example
122    builtin example
123    zmodload -e zsh/example
124  else print -u$ZTST_fd Warning: zsh/example not linked: not checking autoloading
125  fi
126 0d:Autoload a module via a builtin
128   zmodload -u zsh/example
129   builtin example
130 0d:Autoloads are persistent
132   (zmodload -u zsh/parameter
133   zmodload -aF zsh/parameter b:fail
134   fail
135   print "Shouldn't get here.")
136 1:Failed builtin autoload
137 ?(eval):3: module `zsh/parameter' has no such feature: `b:fail': autoload cancelled
138 ?(eval):3: unknown builtin: fail
140   (zmodload -u zsh/parameter
141   zmodload -aF zsh/parameter p:fail
142   print $fail
143   print "Shouldn't get here.")
144 1:Failed parameter autoload
145 ?(eval):3: module `zsh/parameter' has no such feature: `p:fail': autoload cancelled
146 ?(eval):3: unknown parameter: fail
148   (zmodload -u zsh/parameter
149   zmodload -aF zsh/parameter c:fail
150   [[ -fail foo ]]
151   print "Shouldn't get here.")
152 2:Failed condition autoload
153 ?(eval):3: module `zsh/parameter' has no such feature: `c:fail': autoload cancelled
154 ?(eval):3: unknown condition: -foo
156   (zmodload -u zsh/parameter
157   zmodload -aF zsh/parameter f:fail
158   (( fail() )) )
159 2:Failed math function autoload
160 ?(eval):3: module `zsh/parameter' has no such feature: `f:fail': autoload cancelled
161 ?(eval):3: unknown function: fail
163   zmodload -aF zsh/parameter f:fail2
164 1:Immediate autoload failure on non-existent feature when module loaded
165 ?(eval):zmodload:1: module `zsh/parameter' has no such feature: `f:fail2'
167   (zmodload -u zsh/parameter
168   zmodload -aF zsh/parameter p:fail
169   print $(( ${#modules} > 1 )) )
170 0:Autoloads checked on loading but don't necessarily effect current command
172 ?(eval):3: module `zsh/parameter' has no such feature: `p:fail': autoload cancelled
174   zmodload -laF zsh/parameter
175 0:List default autoloads
176 >p:aliases
177 >p:builtins
178 >p:commands
179 >p:dirstack
180 >p:dis_aliases
181 >p:dis_builtins
182 >p:dis_functions
183 >p:dis_galiases
184 >p:dis_reswords
185 >p:dis_saliases
186 >p:funcfiletrace
187 >p:funcsourcetrace
188 >p:funcstack
189 >p:functions
190 >p:functrace
191 >p:galiases
192 >p:history
193 >p:historywords
194 >p:jobdirs
195 >p:jobstates
196 >p:jobtexts
197 >p:modules
198 >p:nameddirs
199 >p:options
200 >p:parameters
201 >p:reswords
202 >p:saliases
203 >p:userdirs
205  if [[ $mods[(r)zsh/example] == zsh/example ]]; then
206    zmodload -u zsh/example
207    zmodload -ac -I zsh/example ex
208    [[ exam -ex ple ]]
209    zmodload -e zsh/example
210  else :
211  fi
212 0d:Autoload a module via a condition
214  if [[ $mods[(r)zsh/example] == zsh/example ]]; then
215    zmodload -u zsh/example
216    zmodload -ap zsh/example exint
217    : $exint
218    zmodload -e zsh/example
219  else :
220  fi
221 0d:Autoload a module via a parameter
223  if [[ $mods[(r)zsh/example] == zsh/example ]]; then
224    zmodload -u zsh/example
225    zmodload -af zsh/example sum
226    (( sum(1) ))
227    zmodload -e zsh/example
228  else :
229  fi
230 0d:Autoload a module via a math function
232 # Test module aliases
234  zmodload -A example=zsh/example
235  zmodload -A
236 0:Test creating a module alias
237 >example -> zsh/example
239  if [[ $mods[(r)zsh/example] == zsh/example ]]; then
240    zmodload -u example
241    zmodload -ab example
242    builtin example
243    zmodload -e example
244  else :
245  fi
246 0d:Unload/autoload the module via its alias
248  zmodload -R example
249  zmodload -e example
250 1:Delete the module alias again
252   zmodload >zmodload_list
253   print -l ${(o)mods} >mods_list
254   diff zmodload_list mods_list
255 0:Listing with zmodload should give all our modules.
257 # Don't unload the main module.
258 # Do unload zsh/parameter, but reload it as it is needed.
260  mods[(r)zsh/main]=()
261  zmodunload $mods
262  zmodload zsh/parameter
263 0d:Unload the modules loaded by this test suite
265   zmodload -aF zsh/zftp b:zftp
266   zmodload -LaF | grep zftp
267 0:Listing feature autoloads includes unloaded modules
268 >zmodload -Fa zsh/zftp b:zftp
270 %clean
272  eval "$deps"
273  unset deps name modfile link auto load funcs mods moddeps
274  unfunction zmodunload