]> git.donarmstrong.com Git - biopieces.git/blob - bp_test/lib/test.sh
added assert_no_diff_dir function to test code
[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/$bp.out"
7 tmp_dir="$BP_TMP/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     src_cksum=`find $src_dir -type f | xargs cat | cksum`
68     dst_cksum=`find $dst_dir -type f | xargs cat | cksum`
69
70     if [ "$src_cksum" == "$dst_cksum" ]; then
71         echo_green "OK"
72         log "OK"
73     else
74         echo_red "FAIL"
75         log "FAIL"
76     fi
77 }
78
79 # Function to assert that all given files do exists.
80 function assert_files_exists
81 {
82     error=0
83
84     for arg in "$@"; do
85         if [ ! -f $arg ]; then
86             error=1
87         fi
88     done
89
90     if [ $error = 1 ]; then
91         echo_red "FAIL"
92         log "FAIL"
93     else
94         echo_green "OK"
95         log "OK"
96     fi
97 }
98
99 # Function to output a given message to the log file.
100 function log
101 {
102     local msg=$1
103
104     echo "$msg" >> $log_file
105 }
106
107 # Function that renders a given message in ASCII green.
108 function echo_green
109 {
110     local msg=$1
111
112     echo -e "\033[32;38m$msg\033[0m"
113 }
114
115 # Function that renders a given message in ASCII yellow.
116 function echo_yellow
117 {
118     local msg=$1
119
120     echo -e "\033[33;38m$msg\033[0m"
121 }
122
123 # Function that renders a given message in ASCII red.
124 function echo_red
125 {
126     local msg=$1
127
128     echo -e "\033[31;38m$msg\033[0m"
129 }
130
131 # Function to clean the temporary file.
132 function clean
133 {
134     if [ -f "$tmp" ]; then
135         rm "$tmp"
136     fi
137 }
138
139 # Function to test if the required version of Perl is installed.
140 function test_perl
141 {
142     echo -n "Testing Perl version ... "
143
144     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
145         echo_green "OK"
146         log "OK"
147     else
148         echo $error | sed "s/, stopped.*//"
149         echo_red "FAIL"
150         exit
151     fi  
152 }
153
154 # Function to test if a given Perl module is installed.
155 function test_perl_module
156 {
157     local module=$1
158
159     echo -n "Testing required Perl module - \"$module\": "
160
161     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
162         echo_red "FAIL"
163         echo "   Try: perl -MCPAN -e 'install $module'"
164         exit
165     else
166         echo_green "OK"
167         log "OK"
168     fi
169 }
170
171 # Function to test if the required version of Ruby is installed.
172 function test_ruby
173 {
174     echo -n "Testing Ruby version ... "
175
176     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
177         echo_green "OK"
178         log "OK"
179     else
180         echo $error | sed "s/.*: //"
181         echo_red "FAIL"
182         exit
183     fi  
184 }
185
186 # Function to test if a given Ruby gem is installed.
187 function test_ruby_gem
188 {
189     local gem=$1
190
191     echo -n "Testing required Ruby gem - \"$gem\": "
192
193     if error=$( gem list --local | grep $gem ); then
194         echo_green "OK"
195         log "OK"
196     else
197         echo_red "FAIL"
198         echo "   Try: gem install $gem"
199         exit
200     fi
201 }
202
203 # Function to test is a given auxillary program is in $PATH.
204 function test_aux_program
205 {
206     local program=$1
207
208     echo -n "Testing auxiliary program - \"$program\": "
209
210     if command -v $program >/dev/null; then
211         echo_green "OK"
212         log "OK"
213     else
214         echo_yellow "WARNING"
215         log "WARNING"
216     fi
217 }