3 #==============================================================================
5 # File ID: 7da4e4fc-8016-11eb-841f-4f45262dc9b5
7 # Test suite for dbk(1).
10 # ©opyleft 2021– Ø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
};
29 our $CMD = "../$CMDB";
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = '0.0.0';
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 'verbose|v+' => \
$Opt{'verbose'},
56 'version' => \
$Opt{'version'},
58 ) || die("$progname: Option error. Use -h for help.\n");
60 $Opt{'verbose'} -= $Opt{'quiet'};
61 $Opt{'help'} && usage
(0);
62 if ($Opt{'version'}) {
72 diag
(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
79 test_standard_options
();
85 if ($Opt{'all'} || $Opt{'todo'}) {
86 diag
('Running TODO tests...');
89 # Insert TODO tests here.
93 diag
('Testing finished.');
98 sub test_standard_options
{
99 diag
('Testing -h (--help) option...');
101 '/ Show this help/i',
104 'Option -h prints help screen');
106 diag
('Testing -v (--verbose) option...');
108 '/^\n\S+ \d+\.\d+\.\d+/s',
111 'Option -v with -h returns version number and help screen');
113 diag
('Testing --version option...');
114 likecmd
("$CMD --version",
115 '/^\S+ \d+\.\d+\.\d+/',
118 'Option --version returns version number');
122 sub test_executable
{
123 my $Tmptop = "tmp-$CMDB-t-$$-" . substr(rand, 2, 8);
124 $ENV{'DBK_VIEWER'} = "echo Viewer:";
125 my $dbkdir = "$Tmptop/dbk";
126 $ENV{'DBK_DIR'} = $dbkdir;
127 my $suuid_logdir = "$Tmptop/uuids";
128 $ENV{'SUUID_LOGDIR'} = $suuid_logdir;
129 my $currfile = curr_dbk_path
();
130 my $curryeardir = $currfile;
131 $curryeardir =~ s!^(.*)/.*?$!$1!;
133 ok
(!-d
$Tmptop, "[Tmptop] doesn't exist");
134 ok
(mkdir($Tmptop), "mkdir [Tmptop]");
135 ok
(mkdir($suuid_logdir), "Create suuid log directory");
138 . "Viewer: $currfile\n"
139 . "Ingenting ble skrevet, så jeg sletter driten.\n",
142 "$CMD without args");
143 my $suuid_file = glob("$suuid_logdir/*.xml");
144 like
($suuid_file, qr/\/.*\
.xml
$/, "[suuid_file] contains filename");
145 ok
(-f
$suuid_file, "[suuid_file] exists and is a file");
146 ok
(unlink($suuid_file), "Delete suuid file");
147 ok
(rmdir($suuid_logdir), "Delete suuid log directory");
148 ok
(rmdir($curryeardir), "rmdir [Tmptop]/YEAR/");
149 ok
(rmdir($dbkdir), "rmdir dbk directory");
150 ok
(rmdir($Tmptop), "rmdir [Tmptop]");
155 my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst)
159 $Mon = sprintf("%02u", $Mon);
160 $Day = sprintf("%02u", $Day);
162 return "$ENV{'DBK_DIR'}/$Year/$Year$Mon$Day.html";
166 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
167 defined($descriptions{$Desc}) &&
168 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
169 $descriptions{$Desc} = 1;
171 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
172 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
173 my $TMP_STDERR = "$CMDB-stderr.tmp";
176 if (defined($Exp_stderr)) {
177 $stderr_cmd = " 2>$TMP_STDERR";
179 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
181 if (defined($Exp_stderr)) {
182 $retval &= is
(file_data
($TMP_STDERR),
183 $Exp_stderr, "$Txt (stderr)");
186 diag
("Warning: stderr not defined for '$Txt'");
188 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
194 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
195 defined($descriptions{$Desc}) &&
196 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
197 $descriptions{$Desc} = 1;
199 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
200 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
201 my $TMP_STDERR = "$CMDB-stderr.tmp";
204 if (defined($Exp_stderr)) {
205 $stderr_cmd = " 2>$TMP_STDERR";
207 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
209 if (defined($Exp_stderr)) {
210 $retval &= like
(file_data
($TMP_STDERR),
211 $Exp_stderr, "$Txt (stderr)");
214 diag
("Warning: stderr not defined for '$Txt'");
216 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
222 # Return file content as a string
226 open(my $fp, '<', $File) or return undef;
234 # Create new file and fill it with data
235 my ($file, $text) = @_;
238 open(my $fp, ">$file") or return 0;
241 $retval = is
(file_data
($file), $text,
242 "$file was successfully created");
244 return $retval; # 0 if error, 1 if ok
248 # Print program version
249 print("$progname $VERSION\n");
254 # Send the help message to stdout
257 if ($Opt{'verbose'}) {
263 Usage: $progname [options]
265 Contains tests for the $CMDB(1) program.
270 Run all tests, also TODOs.
274 Be more quiet. Can be repeated to increase silence.
276 Run only the TODO tests.
278 Increase level of verbosity. Can be repeated.
280 Print version information.
287 # Print a status message to stderr based on verbosity level
288 my ($verbose_level, $Txt) = @_;
290 $verbose_level > $Opt{'verbose'} && return;
291 print(STDERR
"$progname: $Txt\n");
297 # This program is free software; you can redistribute it and/or modify it under
298 # the terms of the GNU General Public License as published by the Free Software
299 # Foundation; either version 2 of the License, or (at your option) any later
302 # This program is distributed in the hope that it will be useful, but WITHOUT
303 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
304 # FOR A PARTICULAR PURPOSE.
305 # See the GNU General Public License for more details.
307 # You should have received a copy of the GNU General Public License along with
309 # If not, see L<http://www.gnu.org/licenses/>.
311 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :