test-ly-files: Display symlink creation, add `-v` to `ln`
[sunny256-utils.git] / tests / dbk.t
blob7996309484aa16cf97cf712d2ecf89438e4b37bc
1 #!/usr/bin/env perl
3 #==============================================================================
4 # dbk.t
5 # File ID: 7da4e4fc-8016-11eb-841f-4f45262dc9b5
7 # Test suite for dbk(1).
9 # Character set: UTF-8
10 # ©opyleft 2021– Ø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;
24 use Time::Local;
26 local $| = 1;
28 our $CMDB = "dbk";
29 our $CMD = "../$CMDB";
31 our %Opt = (
33 'all' => 0,
34 'help' => 0,
35 'quiet' => 0,
36 'todo' => 0,
37 'verbose' => 0,
38 'version' => 0,
42 our $progname = $0;
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = '0.0.0';
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 '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'}) {
63 print_version();
64 exit(0);
67 exit(main());
69 sub main {
70 my $Retval = 0;
72 diag(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
76 goto todo_section;
79 test_standard_options();
80 test_executable();
82 todo_section:
85 if ($Opt{'all'} || $Opt{'todo'}) {
86 diag('Running TODO tests...');
87 TODO: {
88 local $TODO = '';
89 # Insert TODO tests here.
93 diag('Testing finished.');
95 return $Retval;
98 sub test_standard_options {
99 diag('Testing -h (--help) option...');
100 likecmd("$CMD -h",
101 '/ Show this help/i',
102 '/^$/',
104 'Option -h prints help screen');
106 diag('Testing -v (--verbose) option...');
107 likecmd("$CMD -hv",
108 '/^\n\S+ \d+\.\d+\.\d+/s',
109 '/^$/',
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+/',
116 '/^$/',
118 'Option --version returns version number');
119 return;
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");
136 testcmd($CMD,
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]");
153 sub curr_dbk_path {
154 my $epoch = time();
155 my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst)
156 = localtime($epoch);
157 $Year += 1900;
158 $Mon += 1;
159 $Mon = sprintf("%02u", $Mon);
160 $Day = sprintf("%02u", $Day);
162 return "$ENV{'DBK_DIR'}/$Year/$Year$Mon$Day.html";
165 sub testcmd {
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;
170 my $stderr_cmd = '';
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";
174 my $retval = 1;
176 if (defined($Exp_stderr)) {
177 $stderr_cmd = " 2>$TMP_STDERR";
179 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
180 my $ret_val = $?;
181 if (defined($Exp_stderr)) {
182 $retval &= is(file_data($TMP_STDERR),
183 $Exp_stderr, "$Txt (stderr)");
184 unlink($TMP_STDERR);
185 } else {
186 diag("Warning: stderr not defined for '$Txt'");
188 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
190 return $retval;
193 sub likecmd {
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;
198 my $stderr_cmd = '';
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";
202 my $retval = 1;
204 if (defined($Exp_stderr)) {
205 $stderr_cmd = " 2>$TMP_STDERR";
207 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
208 my $ret_val = $?;
209 if (defined($Exp_stderr)) {
210 $retval &= like(file_data($TMP_STDERR),
211 $Exp_stderr, "$Txt (stderr)");
212 unlink($TMP_STDERR);
213 } else {
214 diag("Warning: stderr not defined for '$Txt'");
216 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
218 return $retval;
221 sub file_data {
222 # Return file content as a string
223 my $File = shift;
224 my $Txt;
226 open(my $fp, '<', $File) or return undef;
227 local $/ = undef;
228 $Txt = <$fp>;
229 close($fp);
230 return $Txt;
233 sub create_file {
234 # Create new file and fill it with data
235 my ($file, $text) = @_;
236 my $retval = 0;
238 open(my $fp, ">$file") or return 0;
239 print($fp $text);
240 close($fp);
241 $retval = is(file_data($file), $text,
242 "$file was successfully created");
244 return $retval; # 0 if error, 1 if ok
247 sub print_version {
248 # Print program version
249 print("$progname $VERSION\n");
250 return;
253 sub usage {
254 # Send the help message to stdout
255 my $Retval = shift;
257 if ($Opt{'verbose'}) {
258 print("\n");
259 print_version();
261 print(<<"END");
263 Usage: $progname [options]
265 Contains tests for the $CMDB(1) program.
267 Options:
269 -a, --all
270 Run all tests, also TODOs.
271 -h, --help
272 Show this help.
273 -q, --quiet
274 Be more quiet. Can be repeated to increase silence.
275 -t, --todo
276 Run only the TODO tests.
277 -v, --verbose
278 Increase level of verbosity. Can be repeated.
279 --version
280 Print version information.
283 exit($Retval);
286 sub msg {
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");
292 return;
295 __END__
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
300 # version.
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
308 # this program.
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 :