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