]> git.donarmstrong.com Git - biopieces.git/blob - bp_test/lib/test.sh
f560e2042266a236d4e6dfc810c41a5a1ebcd802
[biopieces.git] / bp_test / lib / test.sh
1 #!/bin/bash
2
3 bp=`basename $0 | sed s/^test_//`
4 in="$BP_DIR/bp_test/in/$bp.in"
5 out="$BP_DIR/bp_test/out/$bp.out"
6 tmp="$BP_TMP/$USER.$bp.out"
7 tmp_dir="$BP_TMP/$USER.test_tmp"
8 log_file="$BP_TMP/$USER.test.log"
9
10 # Function to run a given command (verbose).
11 function run
12 {
13     local command=$1
14
15     msg="${command/$BP_DIR/\$BP_DIR}"
16     msg="${msg//$BP_TMP/\$BP_TMP}"
17
18     echo -n "Testing $msg ... "
19     eval $command > /dev/null 2>&1
20 }
21
22 # Function to run a given command (quiet).
23 function run_quiet
24 {
25     local command=$1
26
27     eval $command > /dev/null 2>&1
28 }
29
30 # Function to assert no difference between
31 # two given files.
32 function assert_no_diff
33 {
34     local src=$1
35     local dst=$2
36
37     if [ ! -f $src ]; then
38         echo_red "FAIL"
39         log "FAIL"
40         return
41     fi
42
43     if [ ! -f $dst ]; then
44         echo_red "FAIL"
45         log "FAIL"
46         return
47     fi
48
49     local diff=`diff -q $src $dst`
50      
51     if [ "$diff" != "" ]; then
52         echo_red "FAIL"
53         log "FAIL"
54     else     
55         echo_green "OK"
56         log "OK"
57     fi     
58 }
59
60 # Function to assert no difference between the content
61 # of two given direcories (recursive).
62 function assert_no_diff_dir
63 {
64     local src_dir=$1
65     local dst_dir=$2
66
67     if [ ! -d $src_dir ]; then
68         echo_red "FAIL"
69         log "FAIL"
70         return
71     fi
72
73     if [ ! -d $dst_dir ]; then
74         echo_red "FAIL"
75         log "FAIL"
76         return
77     fi
78
79     local src_cksum=`find $src_dir -type f | grep -v "\.svn" | sort | xargs cat | cksum`
80     local dst_cksum=`find $dst_dir -type f | grep -v "\.svn" | sort | xargs cat | cksum`
81
82     if [ "$src_cksum" == "$dst_cksum" ]; then
83         echo_green "OK"
84         log "OK"
85     else
86         echo_red "FAIL"
87         log "FAIL"
88     fi
89 }
90
91 # Function to assert that all given files do exists.
92 function assert_files_exists
93 {
94     error=0
95
96     for arg in "$@"; do
97         if [ ! -f $arg ]; then
98             error=1
99         fi
100     done
101
102     if [ $error = 1 ]; then
103         echo_red "FAIL"
104         log "FAIL"
105     else
106         echo_green "OK"
107         log "OK"
108     fi
109 }
110
111 # Function to output a given message to the log file.
112 function log
113 {
114     local msg=$1
115
116     echo "$msg" >> $log_file
117 }
118
119 # Function that renders a given message in ASCII green.
120 function echo_green
121 {
122     local msg=$1
123
124     echo -e "\033[32;38m$msg\033[0m"
125 }
126
127 # Function that renders a given message in ASCII yellow.
128 function echo_yellow
129 {
130     local msg=$1
131
132     echo -e "\033[33;38m$msg\033[0m"
133 }
134
135 # Function that renders a given message in ASCII red.
136 function echo_red
137 {
138     local msg=$1
139
140     echo -e "\033[31;38m$msg\033[0m"
141 }
142
143 # Function to clean the temporary file.
144 function clean
145 {
146     if [ -f "$tmp" ]; then
147         rm "$tmp"
148     fi
149 }
150
151 # Function to test if the required version of Perl is installed.
152 function test_perl
153 {
154     echo -n "Testing Perl version ... "
155
156     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
157         echo_green "OK"
158         log "OK"
159     else
160         echo $error | sed "s/, stopped.*//"
161         echo_red "FAIL"
162         exit
163     fi  
164 }
165
166 # Function to test if a given Perl module is installed.
167 function test_perl_module
168 {
169     local module=$1
170
171     echo -n "Testing required Perl module - \"$module\": "
172
173     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
174         echo_red "FAIL"
175         echo "   Try: perl -MCPAN -e 'install $module'"
176         exit
177     else
178         echo_green "OK"
179         log "OK"
180     fi
181 }
182
183 # Function to test if the required version of Ruby is installed.
184 function test_ruby
185 {
186     echo -n "Testing Ruby version ... "
187
188     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
189         echo_green "OK"
190         log "OK"
191     else
192         echo $error | sed "s/.*: //"
193         echo_red "FAIL"
194         exit
195     fi  
196 }
197
198 # Function to test if a given Ruby gem is installed.
199 function test_ruby_gem
200 {
201     local gem=$1
202
203     echo -n "Testing required Ruby gem - \"$gem\": "
204
205     if error=$( gem list --local | grep $gem ); then
206         echo_green "OK"
207         log "OK"
208     else
209         echo_red "FAIL"
210         echo "   Try: gem install $gem"
211         exit
212     fi
213 }
214
215 # Function to test is a given auxillary program is in $PATH.
216 function test_aux_program
217 {
218     local program=$1
219
220     echo -n "Testing auxiliary program - \"$program\": "
221
222     if command -v $program >/dev/null; then
223         echo_green "OK"
224         log "OK"
225     else
226         echo_yellow "WARNING"
227         log "WARNING"
228     fi
229 }