datefmt: Define 1 year as 365.2425 days instead of 365.25
[sunny256-utils.git] / tests / sident.t
blob1b4765b41892d25c12d09369a8cd4ecfc9f3bc9a
1 #!/usr/bin/env perl
3 #=======================================================================
4 # sident.t
5 # File ID: 9e63b5fe-f989-11dd-9357-000475e441b9
7 # Test suite for sident(1).
9 # Character set: UTF-8
10 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for 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 = "sident";
28 our $CMD = "../$CMDB";
30 our %Opt = (
32 'all' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'todo' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt::Long::Configure('bundling');
48 GetOptions(
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'}) {
62 print_version();
63 exit(0);
66 exit(main());
68 sub main {
69 # {{{
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 =pod
81 testcmd("$CMD command", # {{{
82 <<'END',
83 [expected stdout]
84 END
85 '',
87 'description',
90 # }}}
92 =cut
94 diag('Testing -h (--help) option...');
95 likecmd("$CMD -h", # {{{
96 '/ Show this help/i',
97 '/^$/',
99 'Option -h prints help screen',
102 # }}}
103 diag('Testing -v (--verbose) option...');
104 likecmd("$CMD -hv", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
106 '/^$/',
108 'Option -v with -h returns version number and help screen',
111 # }}}
112 diag('Testing --version option...');
113 likecmd("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
115 '/^$/',
117 'Option --version returns version number',
120 # }}}
121 diag("Testing without options...");
122 testcmd("$CMD sident-files/textfile", # {{{
123 <<'END',
125 sident-files/textfile:
126 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
127 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
128 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
129 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
130 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
131 $Date: 1999/12/23 21:59:22 $
132 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
133 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
134 $Weirdo: blah blah $
138 "Read textfile, no arguments",
141 # }}}
142 testcmd("$CMD sident-files/random", # {{{
143 <<'END',
145 sident-files/random:
146 $Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $
150 "Read random binary data, no arguments",
153 # }}}
154 diag("Testing stdin...");
155 testcmd("$CMD - <sident-files/random", # {{{
156 <<'END',
159 $Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $
163 "Read random binary data from stdin with hyphen as filename",
166 # }}}
167 testcmd("cat sident-files/random | $CMD -", # {{{
168 <<'END',
171 $Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $
175 "Read random binary through pipe, hyphen filename",
178 # }}}
179 diag("Testing -e (--expanded-only) option...");
180 testcmd("$CMD -e sident-files/unexpanded sident-files/textfile", # {{{
181 <<'END',
183 sident-files/textfile:
184 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
185 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
186 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
187 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
188 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
189 $Date: 1999/12/23 21:59:22 $
190 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
191 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
192 $Weirdo: blah blah $
196 "List only expanded keywords",
199 # }}}
200 testcmd("$CMD -ev sident-files/unexpanded sident-files/textfile", # {{{
201 <<'END',
203 sident-files/unexpanded:
205 sident-files/textfile:
206 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
207 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
208 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
209 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
210 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
211 $Date: 1999/12/23 21:59:22 $
212 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
213 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
214 $Weirdo: blah blah $
218 "List only expanded keywords, plus list filename without expanded kw",
221 # }}}
222 diag("Testing -f (--filenames-from) option...");
223 testcmd("$CMD -f sident-files/filenames", # {{{
224 <<'END',
226 sident-files/random:
227 $Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $
229 sident-files/textfile:
230 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
231 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
232 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
233 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
234 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
235 $Date: 1999/12/23 21:59:22 $
236 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
237 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
238 $Weirdo: blah blah $
240 sident-files/unexpanded:
241 $URL$
242 $HeadURL$
243 $LastChangedBy$
244 $Date$
245 $LastChangedDate$
246 $Rev$
247 $Revision$
248 $LastChangedRevision$
249 $Id$
250 $RealLyuNKoWN$
254 "Read filenames from file",
257 # }}}
258 diag("Testing -k (--known-keywords-only) option...");
259 testcmd("$CMD -k sident-files/textfile", # {{{
260 <<'END',
262 sident-files/textfile:
263 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
264 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
265 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
266 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
267 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
268 $Date: 1999/12/23 21:59:22 $
269 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
270 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
274 "List only known keywords",
277 # }}}
278 diag("Testing -l (--filenames-only) option...");
279 testcmd("$CMD -le sident-files/*", # {{{
280 <<'END',
281 sident-files/random
282 sident-files/textfile
286 "Only list names of files with expanded keywords",
289 # }}}
290 diag("Testing -u (--unique-keywords) option...");
291 testcmd("$CMD -u sident-files/textfile", # {{{
292 <<'END',
294 sident-files/textfile:
295 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
296 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
297 $Date: 1999/12/23 21:59:22 $
298 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
299 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
300 $Weirdo: blah blah $
304 "Remove duplicates from textfile",
307 # }}}
308 testcmd("$CMD -v sident-files/*", # {{{
309 <<'END',
311 sident-files/filenames:
313 sident-files/nothing_here:
315 sident-files/random:
316 $Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $
318 sident-files/textfile:
319 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
320 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
321 $Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $
322 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
323 $Id: yeh 1234 2008-09-22 12:25:05Z sunny $
324 $Date: 1999/12/23 21:59:22 $
325 $Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
326 $Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $
327 $Weirdo: blah blah $
329 sident-files/unexpanded:
330 $URL$
331 $HeadURL$
332 $LastChangedBy$
333 $Date$
334 $LastChangedDate$
335 $Rev$
336 $Revision$
337 $LastChangedRevision$
338 $Id$
339 $RealLyuNKoWN$
343 "Also list files without keywords",
346 # }}}
347 testcmd("$CMD -vx sident-files/*", # {{{
348 <<'END',
349 <?xml version="1.0"?>
350 <sident>
351 <file>
352 <filename>sident-files/filenames</filename>
353 </file>
354 <file>
355 <filename>sident-files/nothing_here</filename>
356 </file>
357 <file>
358 <filename>sident-files/random</filename>
359 <keywords>
360 <keyword>$Id: randomstuff 314159 1969-01-21 17:12:16Z sunny $</keyword>
361 </keywords>
362 </file>
363 <file>
364 <filename>sident-files/textfile</filename>
365 <keywords>
366 <keyword>$Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $</keyword>
367 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
368 <keyword>$Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $</keyword>
369 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
370 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
371 <keyword>$Date: 1999/12/23 21:59:22 $</keyword>
372 <keyword>$Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
373 <keyword>$Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
374 <keyword>$Weirdo: blah blah $</keyword>
375 </keywords>
376 </file>
377 <file>
378 <filename>sident-files/unexpanded</filename>
379 <keywords>
380 <keyword>$URL$</keyword>
381 <keyword>$HeadURL$</keyword>
382 <keyword>$LastChangedBy$</keyword>
383 <keyword>$Date$</keyword>
384 <keyword>$LastChangedDate$</keyword>
385 <keyword>$Rev$</keyword>
386 <keyword>$Revision$</keyword>
387 <keyword>$LastChangedRevision$</keyword>
388 <keyword>$Id$</keyword>
389 <keyword>$RealLyuNKoWN$</keyword>
390 </keywords>
391 </file>
392 </sident>
396 "Output XML, including files without keywords",
399 # }}}
400 diag("Testing -x (--xml) option...");
401 testcmd("$CMD -x sident-files/textfile", # {{{
402 <<'END',
403 <?xml version="1.0"?>
404 <sident>
405 <file>
406 <filename>sident-files/textfile</filename>
407 <keywords>
408 <keyword>$Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $</keyword>
409 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
410 <keyword>$Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $</keyword>
411 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
412 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
413 <keyword>$Date: 1999/12/23 21:59:22 $</keyword>
414 <keyword>$Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
415 <keyword>$Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
416 <keyword>$Weirdo: blah blah $</keyword>
417 </keywords>
418 </file>
419 </sident>
423 "Output XML from textfile",
426 # }}}
427 testcmd("$CMD -ux sident-files/textfile", # {{{
428 <<'END',
429 <?xml version="1.0"?>
430 <sident>
431 <file>
432 <filename>sident-files/textfile</filename>
433 <keywords>
434 <keyword>$Id: plain_old_textfile 93653 2008-09-22 14:15:10Z sunny $</keyword>
435 <keyword>$Id: yeh 1234 2008-09-22 12:25:05Z sunny $</keyword>
436 <keyword>$Date: 1999/12/23 21:59:22 $</keyword>
437 <keyword>$Header: /cvsweb/cvs-guide/keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
438 <keyword>$Id: keyword.html,v 1.3 1999/12/23 21:59:22 markd Exp $</keyword>
439 <keyword>$Weirdo: blah blah $</keyword>
440 </keywords>
441 </file>
442 </sident>
446 "Output XML, remove duplicates",
449 # }}}
450 diag("Error conditions...");
451 testcmd("$CMD sident-files", # {{{
455 "Ignore directories",
458 # }}}
459 testcmd("$CMD -v sident-files", # {{{
463 "Ignore directories, even with --verbose",
466 # }}}
467 likecmd("$CMD sident-files/shbvkdsvsdfv", # {{{
468 '/^$/',
469 '/^sident: sident-files/shbvkdsvsdfv: .*$/',
471 "File not found",
474 # }}}
475 likecmd("$CMD -x sident-files/shbvkdsvsdfv", # {{{
476 '/^<\?xml version="1\.0"\?>\n<sident>\n<\/sident>$/',
477 '/^sident: sident-files/shbvkdsvsdfv: .*$/',
479 "File not found, don’t break the XML",
482 # }}}
483 diag("Validate POD (Plain Old Documentation)");
484 testcmd("podchecker $CMD", # {{{
486 "$CMD pod syntax OK.\n",
488 "$CMD contains valid POD",
491 # }}}
493 todo_section:
496 if ($Opt{'all'} || $Opt{'todo'}) {
497 diag('Running TODO tests...'); # {{{
499 TODO: {
501 local $TODO = '';
502 # Insert TODO tests here.
505 # TODO tests }}}
508 diag('Testing finished.');
509 return $Retval;
510 # }}}
511 } # main()
513 sub testcmd {
514 # {{{
515 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
516 defined($descriptions{$Desc}) &&
517 BAIL_OUT("testcmd(): '$Desc' description is used twice");
518 $descriptions{$Desc} = 1;
519 my $stderr_cmd = '';
520 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
521 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
522 my $TMP_STDERR = "$CMDB-stderr.tmp";
523 my $retval = 1;
525 if (defined($Exp_stderr)) {
526 $stderr_cmd = " 2>$TMP_STDERR";
528 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
529 my $ret_val = $?;
530 if (defined($Exp_stderr)) {
531 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
532 unlink($TMP_STDERR);
533 } else {
534 diag("Warning: stderr not defined for '$Txt'");
536 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
538 return $retval;
539 # }}}
540 } # testcmd()
542 sub likecmd {
543 # {{{
544 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
545 defined($descriptions{$Desc}) &&
546 BAIL_OUT("likecmd(): '$Desc' description is used twice");
547 $descriptions{$Desc} = 1;
548 my $stderr_cmd = '';
549 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
550 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
551 my $TMP_STDERR = "$CMDB-stderr.tmp";
552 my $retval = 1;
554 if (defined($Exp_stderr)) {
555 $stderr_cmd = " 2>$TMP_STDERR";
557 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
558 my $ret_val = $?;
559 if (defined($Exp_stderr)) {
560 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
561 unlink($TMP_STDERR);
562 } else {
563 diag("Warning: stderr not defined for '$Txt'");
565 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
567 return $retval;
568 # }}}
569 } # likecmd()
571 sub file_data {
572 # Return file content as a string {{{
573 my $File = shift;
574 my $Txt;
576 open(my $fp, '<', $File) or return undef;
577 local $/ = undef;
578 $Txt = <$fp>;
579 close($fp);
580 return $Txt;
581 # }}}
582 } # file_data()
584 sub print_version {
585 # Print program version {{{
586 print("$progname $VERSION\n");
587 return;
588 # }}}
589 } # print_version()
591 sub usage {
592 # Send the help message to stdout {{{
593 my $Retval = shift;
595 if ($Opt{'verbose'}) {
596 print("\n");
597 print_version();
599 print(<<"END");
601 Usage: $progname [options]
603 Contains tests for the $CMDB(1) program.
605 Options:
607 -a, --all
608 Run all tests, also TODOs.
609 -h, --help
610 Show this help.
611 -q, --quiet
612 Be more quiet. Can be repeated to increase silence.
613 -t, --todo
614 Run only the TODO tests.
615 -v, --verbose
616 Increase level of verbosity. Can be repeated.
617 --version
618 Print version information.
621 exit($Retval);
622 # }}}
623 } # usage()
625 sub msg {
626 # Print a status message to stderr based on verbosity level {{{
627 my ($verbose_level, $Txt) = @_;
629 $verbose_level > $Opt{'verbose'} && return;
630 print(STDERR "$progname: $Txt\n");
631 return;
632 # }}}
633 } # msg()
635 __END__
637 # This program is free software; you can redistribute it and/or modify
638 # it under the terms of the GNU General Public License as published by
639 # the Free Software Foundation; either version 2 of the License, or (at
640 # your option) any later version.
642 # This program is distributed in the hope that it will be useful, but
643 # WITHOUT ANY WARRANTY; without even the implied warranty of
644 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
645 # See the GNU General Public License for more details.
647 # You should have received a copy of the GNU General Public License
648 # along with this program.
649 # If not, see L<http://www.gnu.org/licenses/>.
651 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :