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";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0'; # Not used here, $CMD decides
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
50 'all|a' => \
$Opt{'all'},
51 'help|h' => \
$Opt{'help'},
52 'quiet|q+' => \
$Opt{'quiet'},
53 'todo|t' => \
$Opt{'todo'},
54 'verbose|v+' => \
$Opt{'verbose'},
55 'version' => \
$Opt{'version'},
57 ) || die("$progname: Option error. Use -h for help.\n");
59 $Opt{'verbose'} -= $Opt{'quiet'};
60 $Opt{'help'} && usage
(0);
61 if ($Opt{'version'}) {
66 my $exec_version = `$CMD --version`;
73 diag
('========== BEGIN version info ==========');
75 diag
('=========== END version info ===========');
77 if ($Opt{'todo'} && !$Opt{'all'}) {
81 test_standard_options
();
84 diag
('========== BEGIN version info ==========');
86 diag
('=========== END version info ===========');
91 if ($Opt{'all'} || $Opt{'todo'}) {
92 diag
('Running TODO tests...');
95 # Insert TODO tests here.
99 diag
('Testing finished.');
104 sub test_standard_options
{
105 diag
('Testing -h (--help) option...');
107 '/ Show this help/i',
110 'Option -h prints help screen');
112 diag
('Testing -v (--verbose) option...');
114 '/^\n\S+ \d+\.\d+\.\d+/s',
117 'Option -v with -h returns version number and help screen');
119 diag
('Testing --version option...');
120 likecmd
("$CMD --version",
121 '/^\S+ \d+\.\d+\.\d+/',
124 'Option --version returns version number');
129 sub test_executable
{
134 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
135 defined($descriptions{$Desc})
136 && BAIL_OUT
("testcmd(): '$Desc' description is used twice");
137 $descriptions{$Desc} = 1;
139 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
140 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
141 my $TMP_STDERR = "$CMDB-stderr.tmp";
144 if (defined($Exp_stderr)) {
145 $stderr_cmd = " 2>$TMP_STDERR";
147 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
149 if (defined($Exp_stderr)) {
150 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
153 diag
("Warning: stderr not defined for '$Txt'");
155 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
161 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
162 defined($descriptions{$Desc})
163 && BAIL_OUT
("likecmd(): '$Desc' description is used twice");
164 $descriptions{$Desc} = 1;
166 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
167 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
168 my $TMP_STDERR = "$CMDB-stderr.tmp";
171 if (defined($Exp_stderr)) {
172 $stderr_cmd = " 2>$TMP_STDERR";
174 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
176 if (defined($Exp_stderr)) {
177 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
180 diag
("Warning: stderr not defined for '$Txt'");
182 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
188 # Return file content as a string
192 open(my $fp, '<', $File) or return undef;
201 # Create new file and fill it with data
202 my ($file, $text) = @_;
205 open(my $fp, ">", $file) or return 0;
208 $retval = is
(file_data
($file), $text,
209 "$file was successfully created");
211 return $retval; # 0 if error, 1 if ok
215 # Print program version
216 print("$progname $VERSION\n");
222 # Send the help message to stdout
225 if ($Opt{'verbose'}) {
231 Usage: $progname [options]
233 Contains tests for the $CMDB(1) program.
238 Run all tests, also TODOs.
242 Be more quiet. Can be repeated to increase silence.
244 Run only the TODO tests.
246 Increase level of verbosity. Can be repeated.
248 Print version information.
255 # Print a status message to stderr based on verbosity level
256 my ($verbose_level, $Txt) = @_;
258 $verbose_level > $Opt{'verbose'} && return;
259 print(STDERR
"$progname: $Txt\n");
266 # This program is free software; you can redistribute it and/or modify it under
267 # the terms of the GNU General Public License as published by the Free Software
268 # Foundation; either version 2 of the License, or (at your option) any later
271 # This program is distributed in the hope that it will be useful, but WITHOUT
272 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
273 # FOR A PARTICULAR PURPOSE.
274 # See the GNU General Public License for more details.
276 # You should have received a copy of the GNU General Public License along with
278 # If not, see L<http://www.gnu.org/licenses/>.
280 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :