3 #==============================================================================
7 # Test suite for STDprognameDTS(1).
10 # ©opyleft STDyearDTS– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
13 #==============================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMDB = "STDprognameDTS";
28 our $CMD = "../$CMDB";
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = '0.0.0'; # Not used here, $CMD decides
46 my %descriptions = ();
48 Getopt
::Long
::Configure
('bundling');
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'}) {
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 --"
81 diag
('========== BEGIN version info ==========');
83 diag
('=========== END version info ===========');
85 if ($Opt{'todo'} && !$Opt{'all'}) {
89 test_standard_options
();
92 diag
('========== BEGIN version info ==========');
94 diag
('=========== END version info ===========');
99 if ($Opt{'all'} || $Opt{'todo'}) {
100 diag
('Running TODO tests...');
103 # Insert TODO tests here.
107 diag
('Testing finished.');
112 sub test_standard_options
{
113 diag
('Testing -h (--help) option...');
115 '/ Show this help/i',
118 'Option -h prints help screen');
120 diag
('Testing -v (--verbose) option...');
122 '/^\n\S+ \d+\.\d+\.\d+/s',
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",
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+/',
144 'Option --version returns version number');
145 likecmd
("$CMD --version --quiet",
146 '/^\d+\.\d+\.\d+\S*$/',
149 '--version with --quiet shows only the version number');
151 diag
('--license option');
152 likecmd
("$CMD --license",
153 '/GNU General Public License'
155 . 'either version 2 of the License/s',
158 'Option --license displays the program license');
160 diag
('Unknown option');
161 likecmd
("$CMD --gurgle",
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');
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",
189 testcmd
("$CMD --selftest",
191 # myerror("errno is EACCES")
194 ../$CMDB: errno is EACCES: Permission denied
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;
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";
213 if (defined($Exp_stderr)) {
214 $stderr_cmd = " 2>$TMP_STDERR";
216 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
218 if (defined($Exp_stderr)) {
219 $retval &= is
(file_data
($TMP_STDERR),
220 $Exp_stderr, "$Txt (stderr)");
223 diag
("Warning: stderr not defined for '$Txt'");
225 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
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;
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";
241 if (defined($Exp_stderr)) {
242 $stderr_cmd = " 2>$TMP_STDERR";
244 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
246 if (defined($Exp_stderr)) {
247 $retval &= like
(file_data
($TMP_STDERR),
248 $Exp_stderr, "$Txt (stderr)");
251 diag
("Warning: stderr not defined for '$Txt'");
253 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
259 # Return file content as a string
263 open(my $fp, '<', $File) or return undef;
272 # Create new file and fill it with data
273 my ($file, $text) = @_;
276 open(my $fp, ">", $file) or return 0;
279 $retval = is
(file_data
($file), $text,
280 "$file was successfully created");
282 return $retval; # 0 if error, 1 if ok
286 # Print program version
287 print("$progname $VERSION\n");
293 # Send the help message to stdout
296 if ($Opt{'verbose'}) {
302 Usage: $progname [options]
304 Contains tests for the $CMDB(1) program.
309 Run all tests, also TODOs.
313 Be more quiet. Can be repeated to increase silence.
315 Run only the TODO tests.
317 Run all tests under Valgrind to check for memory leaks and other
320 Increase level of verbosity. Can be repeated.
322 Print version information.
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");
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
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
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 :