Debian Codegraph @

dh_octave_check

raw
1 #!/bin/sh
2
3 # dh_octave_check: Automate checking of Octave add-on Debian packages
4 # This file is part of the dh-octave Debian package and was also part
5 # of the deprecated octave-pkg-dev package.
6
7 # Copyright (c) 2018, 2022-2024 Rafael Laboissière <[email protected]>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, see <https://www.gnu.org/licenses/>.
21
22 : <<=cut
23
24 =encoding utf8
25
26 =head1 NAME
27
28 dh_octave_check - automatically run an Octave add-on package test suite
29
30 =head1 SYNOPSIS
31
32 B<dh_octave_check>
33
34 =head1 DESCRIPTION
35
36 B<dh_octave_check> performs the automated tests present in both F<*.m>
37 and C++ files in the sub-directories of the current directory, which
38 should be the top-level source directory of an Octave add-on package.
39 It also runs the script F<debian/check.m>, if it exists.
40
41 =head1 ENVIRONMENT VARIABLES
42
43 Some aspects of the execution of B<dh_octave_check> can be controlled
44 through the environment variables listed below. These variables can be
45 set via either the C<export> directive in C<debian/rules> or the
46 C<debian/checkvars> file, using the shell syntax C<VARIABLE=value>.
47 The latter file is sourced by B<dh_octave_check>.
48
49 =over
50
51 =item DH_OCTAVE_EXCLUDE_TEST
52
53 A space-separated list with the the names of the files whose unit
54 tests should not be run.
55
56 =item DH_OCTAVE_TEST_OPT
57
58 The optional second argument for Octave’s test function. Allowed
59 values are C<"quiet">, C<"normal">, and C<"verbose">.
60
61 =item DH_OCTAVE_TEST_ENV
62
63 Prefix for calling the Octave command. It can be used for setting
64 environment variables to Sensible values (for instance
65 C<"LD_PRELOAD=">) and specifying another program under which Octave
66 must be run (like C<"xvfb-run">).
67
68 =item octave
69
70 Name of the Octave binary to run (defaults to C<"octave-cli">).
71
72 =item octave_options
73
74 Command line options for Octave (defaults to C<"--no-history --silent
75 --no-init-file --no-window-system">).
76
77 =back
78
79 =head1 SEE ALSO
80
81 L<debhelper(7)>, L<dh(1)>, L<dh_octave_substvar(1)>, L<dh_octave_clean(1)>,
82 L<dh_octave_version(1)>, L<dh_octave_changelogs(1)>, L<dh_octave_examples(1)>
83
84 This program is meant to be used together with debhelper for Debian
85 packages derived from Octave add-on packages.
86 It is recommended to enable it by adding the dh-sequence-octave
87 virtual package to Build-Depends, Build-Depends-Arch or
88 Build-Depends-Indep.
89 This is equivalent to adding dh-octave to the same field and adding
90 --with=octave to the dh sequencer in debian/rules, except that only
91 -arch/-indep/profiled builds are affected.
92 This is also slightly more simple in the most common case.
93
94 =head1 AUTHOR
95
96 Rafael Laboissière <[email protected]>
97
98 =cut
99
100 # Exit immediately on error
101 set -e
102
103 # Cleanup function fr files and directories created via mktemp
104 cleanup ()
105 {
106 rm -f $tmp_script $tmp_results
107 # Avoid deletion of precious /home/* directories
108 echo "$HOME" | grep -qv ^/home && rm -rf $HOME || true
109 }
110 trap cleanup EXIT
111
112 # Check whether we are at the top-level directory of a package's source
113 if [ ! -f DESCRIPTION ] ; then
114 echo "No DESCRIPTION file found. Are you at the top-level directory of a package's source?"
115 exit 0
116 fi
117
118 # Check whether nocheck is required
119 do_check=true
120 for i in $DEB_BUILD_OPTIONS $DEB_BUILD_MAINT_OPTIONS ; do
121 if [ "$i" = nocheck ] ; then
122 do_check=false
123 break
124 fi
125 done
126
127 if [ $do_check = "false" ] ; then
128 echo "Skipping checks since nocheck is required"
129 exit 0
130 fi
131
132 # Set HOME to a writeable, temporary directory
133 export HOME=$(mktemp -d)
134
135 # Get the current Octave package name
136 package=$(grep ^Name: DESCRIPTION | sed -e 's/Name: \(.*\)/\L\1/')
137
138 # Process the options. Currently, only --use-installed-package is
139 # recognized.
140 if [ $# = 1 -a "$1" = --use-installed-package ] ; then
141 use_installed_package=true
142 else
143 use_installed_package=false
144 fi
145
146 # Set the octave executable and the octave options, when the
147 # corresponding variables are absent from the environment.
148 : ${octave:=octave-cli}
149 : ${octave_options:="--no-history --silent --no-init-file --no-window-system"}
150
151 # Read extra variables definitions
152 if [ -f debian/checkvars ] ; then
153 . debian/checkvars
154 fi
155
156 [ -e PKG_ADD ] && mv PKG_ADD PKG_ADD.bak
157
158 echo Checking package...
159
160 unit_test_regex='^%!\(assert\|test\|error\|fail\|xtest\|warning\)'
161
162 # Extract tests from installed m files
163 tmp_script=$(mktemp)
164 tmp_results=$(mktemp)
165 cat >$tmp_script <<EOF
166 fid = fopen ("$tmp_results", "w");
167 disp ('Checking m files ...');
168 [usr_pkg, sys_pkg] = pkg ('list');
169 for i = 1 : length (sys_pkg)
170 name = sys_pkg {1, i}.name;
171 ## Do not load the package being checked, since
172 ## old, incompatible version may be installed.
173 if strcmp ("$package", name) != 1
174 pkg ('load', name);
175 endif
176 endfor
177 EOF
178
179 # For the unit test, we use alternatively the files from the built
180 # package under debian/$package (intended for building the package) or
181 # the package installed in the system (intended for autopkgtest).
182 if [ $use_installed_package = true ] ; then
183 cat >>$tmp_script <<EOF
184 pkg ('load', '$package');
185 EOF
186 else
187 cat >>$tmp_script <<EOF
188 addpath (genpath (sprintf ('%s/debian', pwd ())));
189 EOF
190 fi
191
192 excluded_files_expr=$(for i in $DH_OCTAVE_EXCLUDE_TEST ; do
193 echo " -a ! -name $i " ;
194 done)
195
196 # If the environment variable DH_OCTAVE_TEST_OPT is not set and the
197 # environment variable DEB_BUILD_OPTIONS contains the term "terse",
198 # then the tests will be run in "quiet" mode.
199 if test -z "$DH_OCTAVE_TEST_OPT" ; then
200 case "$DEB_BUILD_OPTIONS" in
201 *terse*) DH_OCTAVE_TEST_OPT=quiet ;;
202 esac
203 fi
204
205 if [ -d inst ] ; then
206 for f in $(find inst/ -name \*.m $excluded_files_expr | grep -v /private/) ; do
207 if grep -q "$unit_test_regex" $f ; then
208 cat >>$tmp_script <<EOF
209 disp ("[$f]");
210 [npass, ntest, nxfail, nskip] = test ("$f",
211 ifelse (strcmp ("$DH_OCTAVE_TEST_OPT", ""),
212 "verbose", "$DH_OCTAVE_TEST_OPT"));
213 printf ("%d test%s, %d passed, %d known failure%s, %d skipped\n",
214 ntest, ifelse (ntest > 1, "s", ""), npass, nxfail,
215 ifelse (nxfail > 1, "s", ""), nskip);
216 fprintf (fid, "%s %d %d %d %d\n", "$f", ntest, npass, nxfail, nskip);
217 EOF
218 fi
219 done
220 fi
221
222 # Extract tests from .cc and .cpp files - these are not installed, but
223 # the compiled .oct files are.
224 #
225 # We search for the tests in the .cc and .cpp files, but invoke the .oct files;
226 # this means we must add generate a loadpath starting at the current
227 # directory and source PKG_ADD files (they might add autoload()
228 # directives)
229
230 # We deactivate the warning about relative paths used for the PKG_ADD file.
231 cat >>$tmp_script <<EOF
232 disp ('Checking C++ files ...');
233 warning ('off', 'Octave:autoload-relative-file-name');
234 EOF
235 if [ -f PKG_ADD ] ; then
236 echo "source('PKG_ADD');" >> $tmp_script
237 fi
238 if [ -f PKG_ADD.bak ] ; then
239 echo "source('PKG_ADD.bak');" >> $tmp_script
240 fi
241 if [ -d src ] ; then
242 for f in $(find src/ \( -name \*.cc -o -name \*.cpp \) $excluded_files_expr) ; do
243 if grep -q "$unit_test_regex" $f ; then
244 cat >>$tmp_script <<EOF
245 disp ("[$f]");
246 [npass, ntest, nxfail, nskip] = test ("$f",
247 ifelse (strcmp ("$DH_OCTAVE_TEST_OPT", ""),
248 "verbose", "$DH_OCTAVE_TEST_OPT"));
249 printf ("%d test%s, %d passed, %d known failure%s, %d skipped\n",
250 ntest, ifelse (ntest > 1, "s", ""), npass, nxfail,
251 ifelse (nxfail > 1, "s", ""), nskip);
252 fprintf (fid, "%s %d %d %d %d\n", "$f", ntest, npass, nxfail, nskip);
253 EOF
254 fi
255 done
256 fi
257
258 if [ -f debian/check.m ] ; then
259 cat >>$tmp_script <<EOF
260 disp ("Run tests in debian/check.m");
261 try
262 source ("debian/check.m");
263 fprintf (fid, "debian/check.m 1 1 0 0\n");
264 catch
265 fprintf (fid, "debian/check.m 1 0 1 0\n");
266 end_try_catch
267 EOF
268 fi
269
270 echo "fclose (fid);" >> $tmp_script
271
272 echo Run the unit tests...
273 eval $DH_OCTAVE_TEST_ENV $octave $octave_options $tmp_script
274 echo Done running the unit tests.
275
276 if [ -e PKG_ADD.bak ] ; then
277 mv PKG_ADD.bak PKG_ADD
278 fi
279
280 ntest=0
281 npass=0
282 nxfail=0
283 nskip=0
284 while read f1 f2 f3 f4 f5 ; do
285 ntest=$((ntest + f2))
286 npass=$((npass + f3))
287 nxfail=$((nxfail + f4))
288 nskip=$((nskip + f5))
289 done < $tmp_results
290 echo "Summary: $ntest tests, $npass passed, $nxfail known failures, $nskip skipped"
291 if [ "$ntest" -gt $((npass + nxfail + nskip)) ] ; then
292 echo "Some tests failed. Giving up..." 1>&2
293 exit 1
294 fi
295
296 exit 0
297
298 # Local Variables:
299 # indent-tabs-mode: nil
300 # tab-width: 4
301 # End: