raz / test / library-path.sh

309 lines · 10.2 KiB · executable · raw · history · last change f7fb2836

1#!/bin/sh
2# Exercises the compiler's library lookup in an isolated environment:
3# the default library in $HOME/.raz/lib, RAZ_PATH, -I, the files a module
4# has for other targets, and modules of one name found twice.
5# Usage: test/library-path.sh path/to/raz
6# Each case runs in a subshell with its own scratch directory and HOME.
7
8raz=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
9top=$(mktemp -d "${TMPDIR:-/tmp}/raz-path-XXXXXX") || exit 1
10trap 'rm -rf "$top"' EXIT
11pass=0
12fail=0
13
14# Sets up a fresh scratch directory $root, with HOME in it (its name has a
15# space), the default library directory $default and the main program.
16setup() {
17 root=$top/$1
18 mkdir -p "$root/src" "$root/home space"
19 unset RAZ_PATH RAZ_PROFILE RUN
20 HOME="$root/home space"
21 export HOME
22 default=$HOME/.raz/lib
23 main=$root/src/Main.raz
24 cat >"$main" <<'END'
25module Main;
26import Out, PathLib;
27proc main*;
28begin Out.int(PathLib.value); Out.ln; end;
29end Main.
30END
31}
32
33# library DIR [VALUE [SUFFIX]] writes DIR/PathLib<SUFFIX>.raz.
34library() {
35 mkdir -p "$1"
36 echo "module PathLib; const value* = ${2:-42}; end PathLib." >"$1/PathLib$3.raz"
37}
38
39# compile FLAGS... builds the program, its messages in $root/err. With
40# RUN set it runs it by "raz run" instead, its output in $root/out.
41compile() {
42 if [ -n "$RUN" ]; then
43 (cd "$root" && "$raz" run -Werror "$@" "$main") >"$root/out" 2>"$root/err"
44 else
45 (cd "$root" && "$raz" -o "$root/app" -Werror "$@" "$main") >"$root/out" 2>"$root/err"
46 fi
47}
48
49# succeeds FLAGS...: it compiles, and the program prints 42.
50succeeds() {
51 compile "$@" || { echo "compile failed:"; cat "$root/err"; return 1; }
52 [ -n "$RUN" ] || "$root/app" >"$root/out" || return 1
53 [ "$(cat "$root/out")" = 42 ] || { echo "printed: $(cat "$root/out")"; return 1; }
54}
55
56# fails MESSAGE FLAGS...: compiling gives status 1 and MESSAGE in its errors.
57fails() {
58 msg=$1
59 shift
60 compile "$@"
61 [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; }
62 grep -F -q -- "$msg" "$root/err" || { echo "no '$msg' in:"; cat "$root/err"; return 1; }
63}
64
65# runcase NAME runs the function NAME in a subshell and counts it.
66runcase() {
67 if (setup "$1"; "$1"); then pass=$((pass + 1))
68 else echo "FAIL library-path: $1"; fail=$((fail + 1)); fi
69}
70
71t_default() {
72 library "$default"
73 succeeds || return 1
74 RUN=1 succeeds
75}
76
77t_colon_in_home() { # unlike RAZ_PATH, HOME is one path, not a list
78 HOME="$root/home : space"
79 export HOME
80 library "$HOME/.raz/lib"
81 succeeds
82}
83
84t_empty_disables_default() {
85 library "$default"
86 RAZ_PATH=; export RAZ_PATH
87 fails "can't find module PathLib" || return 1
88 succeeds -I "$default" # explicit directories still work
89}
90
91t_list_replaces_default() {
92 library "$default" 99
93 library "$root/second directory"
94 RAZ_PATH="$root/first directory:$root/second directory"; export RAZ_PATH
95 succeeds || return 1
96 RUN=1 succeeds || return 1
97 RAZ_PATH="$root/first directory"
98 fails "can't find module PathLib"
99}
100
101t_relative_and_empty_entries() {
102 library "$root/relative"
103 library "$root" 99 # a module in the cwd must not be found by an empty entry
104 RAZ_PATH=:relative::missing:; export RAZ_PATH
105 succeeds || return 1
106 RAZ_PATH=:::
107 fails "can't find module PathLib"
108}
109
110t_missing_home() {
111 unset HOME
112 fails "can't find module PathLib" || return 1
113 HOME=; export HOME
114 fails "can't find module PathLib" || return 1
115 library "$root/explicit"
116 RAZ_PATH=explicit; export RAZ_PATH
117 succeeds
118}
119
120t_no_recursion() {
121 library "$default/package"
122 fails "can't find module PathLib" || return 1
123 RAZ_PATH=$default/package; export RAZ_PATH
124 succeeds
125}
126
127t_missing_module_lists_all_search_directories() {
128 RAZ_PATH=first:second; export RAZ_PATH
129 compile -I explicit
130 [ $? = 1 ] || return 1
131 grep -F -q "explicit, first, second," "$root/err" || { cat "$root/err"; return 1; }
132 grep -F -q "$(dirname "$raz")/lib" "$root/err" || { cat "$root/err"; return 1; }
133}
134
135t_no_path_expansion() {
136 library "$default"
137 for p in '~/.raz/lib' '$HOME/.raz/lib'; do
138 RAZ_PATH=$p; export RAZ_PATH
139 fails "can't find module PathLib" || return 1
140 done
141}
142
143t_target_file_and_interface() {
144 library "$default" 42 .linux-x86_64
145 library "$default" 99
146 succeeds || return 1
147 echo "module PathLib; const other* = 42; end PathLib." >"$default/PathLib.raz"
148 fails "isn't exported"
149}
150
151t_duplicate_main_and_include() {
152 library "$default"
153 for dir in "$root/src" "$root/include"; do
154 library "$dir"
155 flags=
156 [ "$dir" = "$root/src" ] || flags="-I $dir"
157 fails "module PathLib is in" $flags || return 1
158 rm "$dir/PathLib.raz"
159 ln -s "$default/PathLib.raz" "$dir/PathLib.raz" # a link to the chosen file is no duplicate
160 succeeds $flags || return 1
161 rm "$dir/PathLib.raz"
162 done
163}
164
165t_duplicate_user_directories() {
166 library "$default"
167 library "$root/other"
168 RAZ_PATH=$default:$root/other; export RAZ_PATH
169 fails "module PathLib is in" || return 1
170 RAZ_PATH=$default:$default
171 succeeds
172}
173
174t_duplicate_standard_library() {
175 library "$default"
176 echo "module Out; end Out." >"$default/Out.raz"
177 fails "module Out is in"
178}
179
180# -lib DIR: a copy of the library, which the program uses instead of the
181# one beside raz; PathLib comes from -I, so the library is searched last
182t_lib_replaces_standard_library() {
183 cp -R "$(dirname "$raz")/lib" "$root/mylib"
184 library "$root/inc"
185 succeeds -I "$root/inc" -lib "$root/mylib"
186}
187
188# -lib changes where the library is, not the order: a module in both the
189# library and an -I directory is found twice
190t_lib_is_searched_last() {
191 cp -R "$(dirname "$raz")/lib" "$root/mylib"
192 library "$root/inc"
193 echo "module Out; end Out." >"$root/inc/Out.raz"
194 fails "module Out is in" -I "$root/inc" -lib "$root/mylib"
195}
196
197# RAZ_PATH still finds PathLib, the library coming after it
198t_lib_leaves_user_path_alone() {
199 cp -R "$(dirname "$raz")/lib" "$root/mylib"
200 library "$root/user"
201 RAZ_PATH="$root/user"; export RAZ_PATH
202 succeeds -lib "$root/mylib"
203}
204
205t_lib_missing_directory() {
206 fails "-lib $root/nowhere: no such directory" -lib "$root/nowhere"
207}
208
209t_user_paths_do_not_consume_include_slots() {
210 library "$default"
211 flags=
212 p=
213 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do flags="$flags -I missing"; done
214 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32; do
215 p=${p}missing:
216 done
217 succeeds $flags || return 1
218 RAZ_PATH=$p$default; export RAZ_PATH
219 succeeds $flags
220}
221
222# A directory given with a trailing slash, as -I or in RAZ_PATH, joins to
223# a module's name with no second slash, in the messages too.
224t_trailing_slash_keeps_sibling_paths() {
225 dir=$root/target
226 library "$dir" 42 .linux-x86_64
227 echo "module PathLib; const other* = 42; end PathLib." >"$dir/PathLib.raz"
228 for source in include user; do
229 if [ $source = include ]; then RAZ_PATH=; else RAZ_PATH=$dir/; fi
230 export RAZ_PATH
231 if [ $source = include ]; then compile -I "$dir/"; else compile; fi
232 [ $? = 1 ] || { echo "$source: expected status 1"; return 1; }
233 grep -F -q "isn't exported as in" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; }
234 grep -F -q "$dir/PathLib.linux-x86_64.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; }
235 grep -F -q "$dir/PathLib.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; }
236 if grep -F -q "//" "$root/err"; then echo "$source: //"; cat "$root/err"; return 1; fi
237 done
238}
239
240# Module identity is the file's device and inode: a hard link to the
241# chosen file is the same file.
242t_hard_link_chooses_same_module() {
243 library "$default"
244 ln "$default/PathLib.raz" "$root/src/PathLib.raz"
245 succeeds
246}
247
248# ... and equal contents in another file are not.
249t_equal_contents_do_not_choose_distinct_module() {
250 library "$default"
251 cp "$default/PathLib.raz" "$root/src/PathLib.raz"
252 fails "module PathLib is in"
253}
254
255# ... and a link to some other file is not the chosen one.
256t_symbolic_link_to_distinct_module_does_not_choose() {
257 library "$default"
258 library "$root/elsewhere"
259 ln -s "$root/elsewhere/PathLib.raz" "$root/src/PathLib.raz"
260 fails "module PathLib is in"
261}
262
263# The main file at the root of a file system has "/" for its directory,
264# not "": its module's files for other targets are still compared. Needs
265# a chroot, made by an unprivileged user namespace; skipped without one.
266t_root_directory_interface() {
267 if ! unshare -r true 2>/dev/null; then
268 echo "skipped t_root_directory_interface: unshare -r not permitted"
269 return 0
270 fi
271 r=$root/chroot
272 mkdir -p "$r/out"
273 cp "$raz" "$r/raz"
274 cp -R "$(dirname "$raz")/lib" "$r/lib"
275 cat >"$r/iface.raz" <<'END'
276module iface;
277import Out, Variant;
278proc main*;
279begin Out.int(Variant.f); Out.ln; end;
280end iface.
281END
282 echo "module Variant; const f* = 1; end Variant." >"$r/Variant.raz"
283 # the target's file exports f too, so only the interface check can
284 # refuse it: unchecked, the program builds and prints 2
285 echo "module Variant; const f* = 2; const g* = 1; end Variant." >"$r/Variant.linux-x86_64.raz"
286 unshare -r chroot "$r" /raz -o /out/app /iface.raz >"$root/out" 2>"$root/err"
287 [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; }
288 grep -F -q "isn't exported as in" "$root/err" || { cat "$root/err"; return 1; }
289 if grep -F -q "//" "$root/err"; then cat "$root/err"; return 1; fi
290}
291
292for t in t_default t_colon_in_home t_empty_disables_default \
293 t_list_replaces_default t_relative_and_empty_entries t_missing_home \
294 t_no_recursion t_missing_module_lists_all_search_directories \
295 t_no_path_expansion t_target_file_and_interface \
296 t_duplicate_main_and_include t_duplicate_user_directories \
297 t_duplicate_standard_library \
298 t_lib_replaces_standard_library t_lib_is_searched_last \
299 t_lib_leaves_user_path_alone t_lib_missing_directory \
300 t_user_paths_do_not_consume_include_slots \
301 t_trailing_slash_keeps_sibling_paths t_hard_link_chooses_same_module \
302 t_equal_contents_do_not_choose_distinct_module \
303 t_symbolic_link_to_distinct_module_does_not_choose \
304 t_root_directory_interface; do
305 runcase $t
306done
307
308echo "library-path: $pass passed, $fail failed"
309[ $fail = 0 ]