modified: openstmerge.py
[GalaxyCodeBases.git] / bash / realpath-lib / make-readlink-test.sh
blobb25f1a2fb17b11035b7057f17da7fee670869835
1 #!/usr/bin/env bash
3 # make-readlink-test.sh. A test script for realpath-lib. Use this script to
4 # assess compatibility with GNU readlink -f. This script is based upon
5 # concepts and contributions by Mikael Auno that can be found within the issues
6 # thread 1 of November 2013 at:
8 # https://github.com/AsymLabs/realpath-lib/issues/1
10 # Note that this will create a directory structure, identified by root 'foo',
11 # that is shown below:
13 # foo
14 # ├── bar1
15 # │ ├── baz.phys
16 # │ ├── foo->bar1.sym -> ../bar2/foo->bar2.sym
17 # │ └── foo->baz.phys
18 # ├── bar2
19 # │ ├── bazbaz.sym -> baz.sym
20 # │ ├── baz.sym -> ../bar1/baz.phys
21 # │ ├── broken.sym -> ../bar1/broken.phys
22 # │ ├── foo->bar2.sym -> ../bar1/foo->bar1.sym
23 # │ └── foobaz.sym -> ../bar1/foo->baz.phys
24 # └── bar3 -> bar1
26 # If the root 'foo' already exists then no modifications will be made. To
27 # use it, unpack realpath-lib-master.zip (or acquire it from the repo using
28 # git), change into the root directory and do:
30 # ./make-readlink-test.sh # with executable permission
32 # or
34 # bash make-readlink-test.sh # without executable permission
36 # Version : 2014.02.21.00
37 # Usage : ./make-readlink-test.sh
38 # Output : results of tests.
40 # This script requires Bash 4+ and a few POSIX standard utilities, 'mkdir',
41 # 'date', 'uname', 'ln', 'tee', 'tput' and 'more' (or 'less', if installed
42 # but 'less' is not posix). The results are stored in a local file that is
43 # uniquely stamped and suffixed with the extension '.log'. The content of
44 # this file is displayed upon completion of execution.
46 # Note that no warranty is given, either implied or expressed, and the
47 # license terms are according to the MIT license that is included within
48 # this repository. Use at your own risk! You have been warned!
50 # Written by G R Summers. Last updated on Fri Feb 21 20:58:32 GMT 2014.
52 #### ENVIRONMENT
54 source realpath-lib
55 readonly pwd_log="$(pwd)"
56 readonly pwd_phys="$(pwd -P)"
57 readonly suffix="$(uname -s)"'-'"$(date +%s)"'s-'"${RANDOM}${RANDOM}"'.log'
58 readonly stdout_log='readlink-tests-'"$suffix"
59 readonly stderr_log='readlink-errors-'"$suffix"
61 #### FUNCTIONS
63 # check _dependencies : confirm that dependencies are installed.
64 function check_dependencies(){
66 # Posix utilties.
67 hash mkdir &&
68 hash date &&
69 hash uname &&
70 hash ln &&
71 hash tee &&
72 hash tput &&
73 hash more || {
74 echo "One or more dependencies cannot be found, throwing exit condition ..."
75 return 1
80 # make_header : produces file header for results of tests
81 function make_header(){
82 echo "INITIATED TESTS OF REALPATH-LIB V$RPL_VERSION ON $(date)"
83 echo "SYSTEM: $(uname -srm)"
84 echo
87 # make_footer : produces file foolter for results of tests.
88 function make_footer(){
89 local _failed;
90 echo
91 if (( $failcntr )); then
92 if (( $failcntr > 1 )); then
93 _failed="THERE ARE ($failcntr) FAILURES."
94 else
95 _failed="THERE IS ONE (1) FAILURE."
97 echo "SUMMARY OF RESULTS: OF ($totalcntr) TESTS PERFORMED, $_failed"
98 else
99 echo "SUMMARY OF RESULTS: OF ($totalcntr) TESTS PERFORMED, ALL TESTS HAVE PASSED."
101 echo "COMPLETED TESTS OF REALPATH-LIB V$RPL_VERSION ON $(date)"
104 # make_paths : make path (directory) structure.
105 function make_paths(){
106 # Very simple safety check.
107 if [[ ! -d 'foo' ]]; then
108 echo "Directory 'foo' does not exist, creating..."
110 mkdir foo
111 mkdir foo/bar1
112 mkdir foo/bar2
113 ln -s bar1 foo/bar3
114 echo 'test file' > foo/bar1/baz.phys
115 echo 'test file' > foo/bar1/foo-\>baz.phys
116 ln -s ../bar1/baz.phys foo/bar2/baz.sym
117 ln -s baz.sym foo/bar2/bazbaz.sym
118 ln -s ../bar1/foo-\>bar1.sym foo/bar2/foo-\>bar2.sym # circular
119 ln -s ../bar2/foo-\>bar2.sym foo/bar1/foo-\>bar1.sym # circular
120 ln -s ../bar1/broken.phys foo/bar2/broken.sym
121 ln -s ../bar1/foo-\>baz.phys foo/bar2/foobaz.sym
122 } &>/dev/null || {
123 echo "Could not create test directories, throwing exit condition..."
124 return 1
126 else
127 echo "Directory 'foo' already exists, proceeding..."
131 # make_test "function" "message" "path"
132 function make_test() {
133 local _function="$1"
134 local _message="$2"
135 local _path="$3"
136 local _expected="$4"
137 local _result=''
138 local _printf=''
140 # get _result
141 _result="$($_function "$_path")"
143 # produce message
144 _printf="$(printf 'Try %-14s %-37s set_logical=%-4s ' "$_function" "$_message $_path" "$set_logical")"
145 if [[ "$_result" != "$_expected" ]]; then
146 echo "$_printf Fail"
147 echo "--> specified \"$_expected\" but got \"$_result\""
148 ((failcntr++))
149 else
150 echo "$_printf Pass"
152 ((totalcntr++))
156 #### MAIN PROCEDURES
158 # Confirm dependencies.
159 check_dependencies || exit 1
161 # Initialize counters
162 failcntr=0 # failing test counter.
163 totalcntr=0 # total test counter.
165 # Initialize tee_stderr
166 readonly tee_stderr='tee -a /dev/stderr'
168 # Initialize file_reader
169 hash less &>/dev/null && readonly file_reader='less -S' || readonly file_reader='more -d'
171 # Make header for logs.
172 make_header | tee "$stdout_log" > "$stderr_log"
175 # Make directory structure required for tests.
176 make_paths || exit 1
180 # Using 'set_strict=' for all.
182 echo | $tee_stderr
183 echo "===============================================================================" | $tee_stderr
184 echo "= Testing 'Realpath-Lib' as default 'set_strict=' and 'set_logical=' =" | $tee_stderr
185 echo "= (default settings will emulate the results of GNU command 'readlink -f') =" | $tee_stderr
186 echo "===============================================================================" | $tee_stderr
187 echo | $tee_stderr
189 # Begin tests
190 echo "### Logical and physical paths from 'foo/' for symlinks that exist ############" | $tee_stderr
192 make_test get_realpath "existing symlink" "foo/bar3/baz.phys" "$pwd_phys/foo/bar1/baz.phys"
193 make_test get_realpath "existing symlink" "foo/bar2/foobaz.sym" "$pwd_phys/foo/bar1/foo->baz.phys"
194 make_test get_realpath "existing symlink" "foo/bar2/bazbaz.sym" "$pwd_phys/foo/bar1/baz.phys"
196 make_test get_dirname "existing symlink" "foo/bar3/baz.phys" "$pwd_phys/foo/bar1"
197 make_test get_dirname "existing symlink" "foo/bar2/foobaz.sym" "$pwd_phys/foo/bar1"
198 make_test get_dirname "existing symlink" "foo/bar2/bazbaz.sym" "$pwd_phys/foo/bar1"
200 make_test get_filename "existing symlink" "foo/bar3/baz.phys" "baz.phys"
201 make_test get_filename "existing symlink" "foo/bar2/foobaz.sym" "foo->baz.phys"
202 make_test get_filename "existing symlink" "foo/bar2/bazbaz.sym" "baz.phys"
204 make_test get_stemname "existing symlink" "foo/bar3/baz.phys" "baz"
205 make_test get_stemname "existing symlink" "foo/bar2/foobaz.sym" "foo->baz"
206 make_test get_stemname "existing symlink" "foo/bar2/bazbaz.sym" "baz"
208 make_test get_extension "existing symlink" "foo/bar3/baz.phys" "phys"
209 make_test get_extension "existing symlink" "foo/bar2/foobaz.sym" "phys"
210 make_test get_extension "existing symlink" "foo/bar2/bazbaz.sym" "phys"
212 echo | $tee_stderr
213 echo "### Logical and physical paths from 'foo/bar2/' for symlinks that exist #######" | $tee_stderr
215 cd foo/bar2 &>/dev/null
217 make_test get_realpath "existing symlink" "baz.sym" "$pwd_phys/foo/bar1/baz.phys"
218 make_test get_dirname "existing symlink" "baz.sym" "$pwd_phys/foo/bar1"
219 make_test get_filename "existing symlink" "baz.sym" "baz.phys"
220 make_test get_stemname "existing symlink" "baz.sym" "baz"
221 make_test get_extension "existing symlink" "baz.sym" "phys"
223 cd - &>/dev/null
225 echo | $tee_stderr
226 echo "### Logical and physical paths from 'foo/bar3/' for symlinks that exist #######" | $tee_stderr
228 cd foo/bar3 &>/dev/null
230 make_test get_realpath "existing symlink" "baz.phys" "$pwd_phys/foo/bar1/baz.phys"
231 make_test get_realpath "existing symlink" "foo->baz.phys" "$pwd_phys/foo/bar1/foo->baz.phys"
232 make_test get_dirname "existing symlink" "baz.phys" "$pwd_phys/foo/bar1"
233 make_test get_dirname "existing symlink" "foo->baz.phys" "$pwd_phys/foo/bar1"
234 make_test get_filename "existing symlink" "baz.phys" "baz.phys"
235 make_test get_filename "existing symlink" "foo->baz.phys" "foo->baz.phys"
236 make_test get_stemname "existing symlink" "baz.phys" "baz"
237 make_test get_stemname "existing symlink" "foo->baz.phys" "foo->baz"
238 make_test get_extension "existing symlink" "baz.phys" "phys"
239 make_test get_extension "existing symlink" "foo->baz.phys" "phys"
241 cd - &>/dev/null
243 echo | $tee_stderr
244 echo "### Logical and physical paths from 'foo/' for symlinks that do not exist #####" | $tee_stderr
246 make_test get_realpath "non-existant symlink" "foo/bar2/no.foo" "$pwd_phys/foo/bar2/no.foo"
247 make_test get_dirname "non-existant symlink" "foo/bar2/no.foo" "$pwd_phys/foo/bar2"
248 make_test get_filename "non-existant symlink" "foo/bar2/no.foo" "no.foo"
249 make_test get_stemname "non-existant symlink" "foo/bar2/no.foo" "no"
250 make_test get_extension "non-existant symlink" "foo/bar2/no.foo" "foo"
252 echo | $tee_stderr
253 echo "### Logical and physical paths from 'foo/' for symlinks that are broken #######" | $tee_stderr
255 make_test get_realpath "broken symlink" "foo/bar2/broken.sym" "$pwd_phys/foo/bar1/broken.phys"
256 make_test get_dirname "broken symlink" "foo/bar2/broken.sym" "$pwd_phys/foo/bar1"
257 make_test get_filename "broken symlink" "foo/bar2/broken.sym" "broken.phys"
258 make_test get_stemname "broken symlink" "foo/bar2/broken.sym" "broken"
259 make_test get_extension "broken symlink" "foo/bar2/broken.sym" "phys"
261 echo | $tee_stderr
262 echo "### Logical and physical paths from 'foo/' for files that are not symlinks ####" | $tee_stderr
264 make_test get_realpath "ordinary file" "foo/bar1/baz.phys" "$pwd_phys/foo/bar1/baz.phys"
265 make_test get_realpath "ordinary file" "foo/bar1/foo->baz.phys" "$pwd_phys/foo/bar1/foo->baz.phys"
266 make_test get_dirname "ordinary file" "foo/bar1/baz.phys" "$pwd_phys/foo/bar1"
267 make_test get_dirname "ordinary file" "foo/bar1/foo->baz.phys" "$pwd_phys/foo/bar1"
268 make_test get_filename "ordinary file" "foo/bar1/baz.phys" "baz.phys"
269 make_test get_filename "ordinary file" "foo/bar1/foo->baz.phys" "foo->baz.phys"
270 make_test get_stemname "ordinary file" "foo/bar1/baz.phys" "baz"
271 make_test get_stemname "ordinary file" "foo/bar1/foo->baz.phys" "foo->baz"
272 make_test get_extension "ordinary file" "foo/bar1/baz.phys" "phys"
273 make_test get_extension "ordinary file" "foo/bar1/foo->baz.phys" "phys"
275 echo | $tee_stderr
276 echo "### Circular references, paths from 'foo/' for files that are symlinks ########" | $tee_stderr
278 make_test get_realpath "circular ref" "foo/bar1/foo->bar1.sym" ""
279 make_test get_realpath "circular ref" "foo/bar2/foo->bar2.sym" ""
280 make_test get_dirname "circular ref" "foo/bar1/foo->bar1.sym" ""
281 make_test get_dirname "circular ref" "foo/bar2/foo->bar2.sym" ""
282 make_test get_filename "circular ref" "foo/bar1/foo->bar1.sym" ""
283 make_test get_filename "circular ref" "foo/bar2/foo->bar2.sym" ""
284 make_test get_stemname "circular ref" "foo/bar1/foo->bar1.sym" ""
285 make_test get_stemname "circular ref" "foo/bar2/foo->bar2.sym" ""
286 make_test get_extension "circular ref" "foo/bar1/foo->bar1.sym" ""
287 make_test get_extension "circular ref" "foo/bar2/foo->bar2.sym" ""
289 } 2>> "$stderr_log"
291 } 1>> "$stdout_log"
293 make_footer | tee -a "$stdout_log" >> "$stderr_log"
295 tput clear
296 $file_reader "$stdout_log"
298 tput clear
299 $file_reader "$stderr_log"
301 # end make-readlink-test.sh