forked from reidpr/quac
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·170 lines (146 loc) · 4.7 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·170 lines (146 loc) · 4.7 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
# It used to be that you could test a Python module which called
# testable.register() simply by running it as a script. However, the Powers
# that Be have decreed that running scripts within packages is a big fail
# (IMO, this is shortsighted, as the complexity of this script demonstrates).
# Supposedly, there's a package called runpy which does the right magic, but I
# couldn't get it to work properly.
#
# See:
#
# http://www.python.org/dev/peps/pep-0328/
# http://www.python.org/dev/peps/pep-0366/
# http://bugs.python.org/issue1510172
#
# Anyway, the usage of this script is as follows:
#
# * If you pass the filename of a module, it will be tested.
#
# * Otherwise, this script will find all candidate modules under lib/, all
# scripts under bin/ and test them. Modules can declare that they should
# not be included in these automatic tests but remain testable manually;
# if you pass -a, then these are included anyway.
#
# * If you pass -i as the first argument, then the function
# test_interactive() will be called instead of the non-interactive test
# harness. This is good for drawing pictures and whatnot that need human
# interpretation to determine correctness.
#
# * If a script has import problems, a complaint about that will be printed
# instead of running the tests (which obviously can't be done).
#
# BUGS:
#
# * Fails on filenames with spaces or other funny characters.
set -e
#set -x
BASEDIR=$(cd $(dirname $0); pwd)
while getopts "ai" opt; do
case $opt in
a)
echo '* test everything (override manual-only)'
test_all=1
;;
i)
echo '* interactive mode'
interactive=1
;;
\?)
exit 1
;;
esac
done
shift $((OPTIND-1))
to_test=$*
# Choose the right sed option for extended regexes, in a lame way.
case $(uname) in
Linux)
sed='sed -r'
;;
Darwin)
sed='sed -E'
;;
*)
echo "don't know how to sed on your platform" >&2
exit 1
;;
esac
## Functions ##
# Test whether the imports in script or module $1 can work. This is done by
# extracting all the imports into a test .py file in the same directory as $1,
# and then running that file. Only the first import failure is reported.
import_test () {
local traw=$(dirname $1)/IMPORTTEST.py
# strip indents and doctest stuff on import lines
egrep '^[ >]*(import|from)' $1 | $sed 's/^[ >]*//' > $traw
python -m $(file_to_module $traw) 2> >(tail -n1)
local retval=$?
if [ $retval -eq 0 ]; then
# success; need newline (on failure, Python error message has one)
echo
fi
rm $traw
return $retval
}
# Transform filename to Python module name: strip leading ./, if any, and
# trailing .py, and change slashes to dots.
file_to_module () {
echo $1 | $sed 's/^(\.\/)?(.*)\.py$/\2/g' | $sed 's/\//./g'
}
# # Python will give an ImportError message in case of import skips
# # FIXME: This doesn't test "from . import foo" correctly.
# if ( egrep "$import_skip" $mraw | python $testimports 2> >(tail -n1) ); then
# echo
# python -m $m
# fi
## Test scripts ##
cd $BASEDIR/bin
# Can't specify scripts to test.sh (use --unittest), so do nothing if anything
# is specified.
if [ "$to_test" == "" ]; then
for script in $(find . -xtype f); do
# is it really a Python script? hacky test...
if ( head -n1 $script | fgrep -q python ); then
echo -n "+ $script ... "
if ( ! fgrep -q quacpath $script ); then
echo 'Does not import quacpath'
continue
fi
if ( import_test $script ); then
if ( fgrep -q -- --unittest $script ); then
$script --unittest
fi
fi
fi
done
fi
## Test modules ##
cd $BASEDIR/lib
if [ "$to_test" == "" ]; then
if [ $test_all ]; then
grepstr='testable\.(manualonly_)?register'
else
grepstr='testable\.register'
fi
modules=$(find . -name '*.py' -exec egrep -l $grepstr {} \;)
else
modules=$to_test
fi
# Remove all .pyc files: otherwise, importing modules that were removed or
# renamed will still work!
find . -name '*.pyc' -exec rm {} \;
for mraw in $modules; do
# Strip leading lib/, if present (so you can use tab completion with
# test.sh at top level).
mraw=$(echo $mraw | $sed 's/lib\///' )
m=$(file_to_module $mraw)
echo -n "+ $m ... "
if [ $interactive ]; then
echo
python -c "import $m ; $m.test_interactive()"
else
if ( import_test $mraw ); then
python -m $m
fi
fi
done