annotate scripts/testfun/rundemos.m @ 18054:70e83c641b48

Accept a bare directory name for rundemos and runtests. * rundemos.m, runtests.m: Use isdir to check whether input is a directory.
author Rik <rik@octave.org>
date Tue, 03 Dec 2013 09:35:54 -0800
parents d63878346099
children b675665ce328
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17460
diff changeset
1 ## Copyright (C) 2008-2013 John W. Eaton
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## your option) any later version.
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
12668
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
20 ## @deftypefn {Function File} {} rundemos ()
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
21 ## @deftypefnx {Function File} {} rundemos (@var{directory})
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
22 ## Execute built-in demos for all function files in the specified directory.
17456
2c2a6801cb57 rundemos.m, runtests.m: Operate on .cc files (bug #40052)
Mike Miller <mtmiller@ieee.org>
parents: 17148
diff changeset
23 ## Also executes demos in any C++ source files found in the directory, for
2c2a6801cb57 rundemos.m, runtests.m: Operate on .cc files (bug #40052)
Mike Miller <mtmiller@ieee.org>
parents: 17148
diff changeset
24 ## use with dynamically linked functions.
2c2a6801cb57 rundemos.m, runtests.m: Operate on .cc files (bug #40052)
Mike Miller <mtmiller@ieee.org>
parents: 17148
diff changeset
25 ##
12668
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
26 ## If no directory is specified, operate on all directories in Octave's
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
27 ## search path for functions.
e3dc23f7dd54 doc: Improve a few docstrings related to test functions and directories.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
28 ## @seealso{runtests, path}
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 ## @end deftypefn
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 ## Author: jwe
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 function rundemos (directory)
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 if (nargin == 0)
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
36 dirs = ostrsplit (path (), pathsep ());
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
37 do_class_dirs = true;
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 elseif (nargin == 1)
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 if (is_absolute_filename (directory))
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 dirs = {directory};
18054
70e83c641b48 Accept a bare directory name for rundemos and runtests.
Rik <rik@octave.org>
parents: 17744
diff changeset
41 elseif (isdir (directory))
17148
fa14aa77b514 Allow relative directory name for rundemos/runtests.
Rik <rik@octave.org>
parents: 16724
diff changeset
42 dirs = {canonicalize_file_name(directory)};
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 else
17148
fa14aa77b514 Allow relative directory name for rundemos/runtests.
Rik <rik@octave.org>
parents: 16724
diff changeset
44 if (directory(end) == filesep ())
fa14aa77b514 Allow relative directory name for rundemos/runtests.
Rik <rik@octave.org>
parents: 16724
diff changeset
45 directory = directory(1:end-1);
fa14aa77b514 Allow relative directory name for rundemos/runtests.
Rik <rik@octave.org>
parents: 16724
diff changeset
46 endif
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 fullname = find_dir_in_path (directory);
18054
70e83c641b48 Accept a bare directory name for rundemos and runtests.
Rik <rik@octave.org>
parents: 17744
diff changeset
48 if (isempty (fullname))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
49 error ("rundemos: DIRECTORY argument must be a valid pathname");
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 endif
18054
70e83c641b48 Accept a bare directory name for rundemos and runtests.
Rik <rik@octave.org>
parents: 17744
diff changeset
51 dirs = {fullname};
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 endif
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
53 do_class_dirs = false;
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 else
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 print_usage ();
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 endif
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57
8231
df28b55d03c0 make rundemos with no arguments work
John W. Eaton <jwe@octave.org>
parents: 8230
diff changeset
58 for i = 1:numel (dirs)
df28b55d03c0 make rundemos with no arguments work
John W. Eaton <jwe@octave.org>
parents: 8230
diff changeset
59 d = dirs{i};
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
60 run_all_demos (d, do_class_dirs);
8231
df28b55d03c0 make rundemos with no arguments work
John W. Eaton <jwe@octave.org>
parents: 8230
diff changeset
61 endfor
df28b55d03c0 make rundemos with no arguments work
John W. Eaton <jwe@octave.org>
parents: 8230
diff changeset
62
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 endfunction
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
65 function run_all_demos (directory, do_class_dirs)
17459
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
66 flist = readdir (directory);
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
67 dirs = {};
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 for i = 1:numel (flist)
17459
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
69 f = flist{i};
17456
2c2a6801cb57 rundemos.m, runtests.m: Operate on .cc files (bug #40052)
Mike Miller <mtmiller@ieee.org>
parents: 17148
diff changeset
70 if ((length (f) > 2 && strcmpi (f((end-1):end), ".m")) ||
2c2a6801cb57 rundemos.m, runtests.m: Operate on .cc files (bug #40052)
Mike Miller <mtmiller@ieee.org>
parents: 17148
diff changeset
71 (length (f) > 3 && strcmpi (f((end-2):end), ".cc")))
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 f = fullfile (directory, f);
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 if (has_demos (f))
14087
8782d41893c6 Add try-catch block to rundemos.
Ben Abbott <bpabbott@mac.com>
parents: 13065
diff changeset
74 try
8782d41893c6 Add try-catch block to rundemos.
Ben Abbott <bpabbott@mac.com>
parents: 13065
diff changeset
75 demo (f);
8782d41893c6 Add try-catch block to rundemos.
Ben Abbott <bpabbott@mac.com>
parents: 13065
diff changeset
76 catch
15202
f3b5cadfd6d5 fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents: 14237
diff changeset
77 printf ("error: %s\n\n", lasterror().message);
14087
8782d41893c6 Add try-catch block to rundemos.
Ben Abbott <bpabbott@mac.com>
parents: 13065
diff changeset
78 end_try_catch
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 8920
diff changeset
79 if (i != numel (flist))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 8920
diff changeset
80 input ("Press <enter> to continue: ", "s");
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 8920
diff changeset
81 endif
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 endif
17459
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
83 elseif (f(1) == "@")
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
84 f = fullfile (directory, f);
17459
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
85 if (isdir (f))
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
86 dirs(end+1) = f;
f271fa40074d runtests.m, rundemos.m: Use readdir, rather than dir, for ~700X speed-up.
Rik <rik@octave.org>
parents: 17457
diff changeset
87 endif
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 endif
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 endfor
17457
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
90
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
91 ## Recurse into class directories since they are implied in the path
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
92 if (do_class_dirs)
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
93 for i = 1:numel (dirs)
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
94 d = dirs{i};
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
95 run_all_demos (d, false);
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
96 endfor
a4f86f459744 rundemos.m, runtests.m: Include class directories in path (bug #40053)
Mike Miller <mtmiller@ieee.org>
parents: 17456
diff changeset
97 endif
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 endfunction
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 function retval = has_demos (f)
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 fid = fopen (f);
8699
6e764b7317bd test/fntests.m, scripts/test/demo.m: error on fopen failures
John W. Eaton <jwe@octave.org>
parents: 8245
diff changeset
102 if (f < 0)
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
103 error ("rundemos: fopen failed: %s", f);
8699
6e764b7317bd test/fntests.m, scripts/test/demo.m: error on fopen failures
John W. Eaton <jwe@octave.org>
parents: 8245
diff changeset
104 else
17460
9411ad1afa84 rundemos.m: Use regexp, instead of strfind, to report whether a file has_demos.
Rik <rik@octave.org>
parents: 17459
diff changeset
105 str = fread (fid, "*char").';
8699
6e764b7317bd test/fntests.m, scripts/test/demo.m: error on fopen failures
John W. Eaton <jwe@octave.org>
parents: 8245
diff changeset
106 fclose (fid);
17460
9411ad1afa84 rundemos.m: Use regexp, instead of strfind, to report whether a file has_demos.
Rik <rik@octave.org>
parents: 17459
diff changeset
107 retval = ! isempty (regexp (str, '^%!demo', 'lineanchors', 'once'));
8699
6e764b7317bd test/fntests.m, scripts/test/demo.m: error on fopen failures
John W. Eaton <jwe@octave.org>
parents: 8245
diff changeset
108 endif
8229
1bf51192fa1d imported patch rundemos
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 endfunction
13065
1ca3cde15b18 codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents: 12673
diff changeset
110
14214
2fe0f5fa8cc3 Replace to-be-deprecated findstr occurrences with strfind.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
111
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
112 %!error rundemos ("foo", 1)
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
113 %!error <DIRECTORY argument> rundemos ("#_TOTALLY_/_INVALID_/_PATHNAME_#")
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14214
diff changeset
114