std.c: usage(): Use `myerror()` instead of `fprintf()`
[sunny256-utils.git] / Lib / std / c / src / t / std.t
blobe1df176f0503c941491c9fdd0bf4b0743bb3c60a
1 #!/usr/bin/env perl
3 #==============================================================================
4 # STDfilenameDTS
5 # File ID: STDuuidDTS
7 # Test suite for STDprognameDTS(1).
9 # Character set: UTF-8
10 # ©opyleft STDyearDTS– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
18 BEGIN {
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 local $| = 1;
27 our $CMDB = "STDprognameDTS";
28 our $CMD = "../$CMDB";
30 our %Opt = (
32 'all' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'todo' => 0,
36 'valgrind' => 0,
37 'verbose' => 0,
38 'version' => 0,
42 our $progname = $0;
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = '0.0.0'; # Not used here, $CMD decides
46 my %descriptions = ();
48 Getopt::Long::Configure('bundling');
49 GetOptions(
51 'all|a' => \$Opt{'all'},
52 'help|h' => \$Opt{'help'},
53 'quiet|q+' => \$Opt{'quiet'},
54 'todo|t' => \$Opt{'todo'},
55 'valgrind' => \$Opt{'valgrind'},
56 'verbose|v+' => \$Opt{'verbose'},
57 'version' => \$Opt{'version'},
59 ) || die("$progname: Option error. Use -h for help.\n");
61 $Opt{'verbose'} -= $Opt{'quiet'};
62 $Opt{'help'} && usage(0);
63 if ($Opt{'version'}) {
64 print_version();
65 exit(0);
68 my $exec_version = `$CMD --version`;
69 my $UNUSED = ($exec_version =~ /has UNUSED/s) ? 1 : 0;
71 if ($Opt{'valgrind'}) {
72 $CMD = "valgrind -q --leak-check=full --show-leak-kinds=all --"
73 . " ../$CMDB";
76 exit(main());
78 sub main {
79 my $Retval = 0;
81 diag('========== BEGIN version info ==========');
82 diag($exec_version);
83 diag('=========== END version info ===========');
85 if ($Opt{'todo'} && !$Opt{'all'}) {
86 goto todo_section;
89 test_standard_options();
90 test_executable();
92 diag('========== BEGIN version info ==========');
93 diag($exec_version);
94 diag('=========== END version info ===========');
96 todo_section:
99 if ($Opt{'all'} || $Opt{'todo'}) {
100 diag('Running TODO tests...');
101 TODO: {
102 local $TODO = '';
103 # Insert TODO tests here.
107 diag('Testing finished.');
109 return $Retval;
112 sub test_standard_options {
113 diag('Testing -h (--help) option...');
114 likecmd("$CMD -h",
115 '/ Show this help/i',
116 '/^$/',
118 'Option -h prints help screen');
120 diag('Testing -v (--verbose) option...');
121 likecmd("$CMD -hv",
122 '/^\n\S+ \d+\.\d+\.\d+/s',
123 '/^$/',
125 'Option -v with -h returns version number and help screen');
126 testcmd("$CMD -vvv --verbose",
128 "../$CMDB: main(): Using verbose level 4\n"
129 . "../$CMDB: main(): argc = 3, optind = 3\n"
130 . "../$CMDB: Returning from main() with value 0\n",
132 '-vvv --verbose');
133 testcmd("$CMD -vvqv --verbose",
137 'One -q reduces the verbosity level');
139 diag('Testing --version option...');
140 likecmd("$CMD --version",
141 '/^\S+ \d+\.\d+\.\d+/',
142 '/^$/',
144 'Option --version returns version number');
145 likecmd("$CMD --version --quiet",
146 '/^\d+\.\d+\.\d+\S*$/',
147 '/^$/',
149 '--version with --quiet shows only the version number');
151 diag('--license option');
152 likecmd("$CMD --license",
153 '/GNU General Public License'
154 . '.*'
155 . 'either version 2 of the License/s',
156 '/^$/',
158 'Option --license displays the program license');
160 diag('Unknown option');
161 likecmd("$CMD --gurgle",
162 '/^$/',
163 "/\\.\\.\\/$CMDB: Option error\\n"
164 . "\\.\\.\\/$CMDB: Type \"\\.\\.\\/$CMDB --help\""
165 . " for help screen\\."
166 . " Returning with value 1\\.\\n/s",
168 'Unknown option specified');
170 return;
173 sub test_executable {
174 testcmd("$CMD -vvvv abc",
176 "../$CMDB: main(): Using verbose level 4\n"
177 . "../$CMDB: main(): argc = 3, optind = 2\n"
178 . "../$CMDB: main(): Non-option arg 2: abc\n"
179 . "../$CMDB: Returning from main() with value 0\n",
181 'One argument');
182 test_selftest();
184 return;
187 sub test_selftest {
188 diag("--selftest");
189 testcmd("$CMD --selftest",
190 <<END,
191 # myerror("errno is EACCES")
193 <<END,
194 ../$CMDB: errno is EACCES: Permission denied
197 '--selftest');
199 return;
202 sub testcmd {
203 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
204 defined($descriptions{$Desc})
205 && BAIL_OUT("testcmd(): '$Desc' description is used twice");
206 $descriptions{$Desc} = 1;
207 my $stderr_cmd = '';
208 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
209 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
210 my $TMP_STDERR = "$CMDB-stderr.tmp";
211 my $retval = 1;
213 if (defined($Exp_stderr)) {
214 $stderr_cmd = " 2>$TMP_STDERR";
216 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
217 my $ret_val = $?;
218 if (defined($Exp_stderr)) {
219 $retval &= is(file_data($TMP_STDERR),
220 $Exp_stderr, "$Txt (stderr)");
221 unlink($TMP_STDERR);
222 } else {
223 diag("Warning: stderr not defined for '$Txt'");
225 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
227 return $retval;
230 sub likecmd {
231 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
232 defined($descriptions{$Desc})
233 && BAIL_OUT("likecmd(): '$Desc' description is used twice");
234 $descriptions{$Desc} = 1;
235 my $stderr_cmd = '';
236 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
237 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
238 my $TMP_STDERR = "$CMDB-stderr.tmp";
239 my $retval = 1;
241 if (defined($Exp_stderr)) {
242 $stderr_cmd = " 2>$TMP_STDERR";
244 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
245 my $ret_val = $?;
246 if (defined($Exp_stderr)) {
247 $retval &= like(file_data($TMP_STDERR),
248 $Exp_stderr, "$Txt (stderr)");
249 unlink($TMP_STDERR);
250 } else {
251 diag("Warning: stderr not defined for '$Txt'");
253 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
255 return $retval;
258 sub file_data {
259 # Return file content as a string
260 my $File = shift;
261 my $Txt;
263 open(my $fp, '<', $File) or return undef;
264 local $/ = undef;
265 $Txt = <$fp>;
266 close($fp);
268 return $Txt;
271 sub create_file {
272 # Create new file and fill it with data
273 my ($file, $text) = @_;
274 my $retval = 0;
276 open(my $fp, ">", $file) or return 0;
277 print($fp $text);
278 close($fp);
279 $retval = is(file_data($file), $text,
280 "$file was successfully created");
282 return $retval; # 0 if error, 1 if ok
285 sub print_version {
286 # Print program version
287 print("$progname $VERSION\n");
289 return;
292 sub usage {
293 # Send the help message to stdout
294 my $Retval = shift;
296 if ($Opt{'verbose'}) {
297 print("\n");
298 print_version();
300 print(<<"END");
302 Usage: $progname [options]
304 Contains tests for the $CMDB(1) program.
306 Options:
308 -a, --all
309 Run all tests, also TODOs.
310 -h, --help
311 Show this help.
312 -q, --quiet
313 Be more quiet. Can be repeated to increase silence.
314 -t, --todo
315 Run only the TODO tests.
316 --valgrind
317 Run all tests under Valgrind to check for memory leaks and other
318 problems.
319 -v, --verbose
320 Increase level of verbosity. Can be repeated.
321 --version
322 Print version information.
325 exit($Retval);
328 sub msg {
329 # Print a status message to stderr based on verbosity level
330 my ($verbose_level, $Txt) = @_;
332 $verbose_level > $Opt{'verbose'} && return;
333 print(STDERR "$progname: $Txt\n");
335 return;
338 __END__
340 # This program is free software; you can redistribute it and/or modify it under
341 # the terms of the GNU General Public License as published by the Free Software
342 # Foundation; either version 2 of the License, or (at your option) any later
343 # version.
345 # This program is distributed in the hope that it will be useful, but WITHOUT
346 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
347 # FOR A PARTICULAR PURPOSE.
348 # See the GNU General Public License for more details.
350 # You should have received a copy of the GNU General Public License along with
351 # this program.
352 # If not, see L<http://www.gnu.org/licenses/>.
354 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :