std/c/src/: Fix hardcoded year in std.1.man + fix alignment in main()
[sunny256-utils.git] / tests / sumdup.t
blob898b78d5dcf3c33f571b6fb9832acd6550f49f0b
1 #!/usr/bin/env perl
3 #=======================================================================
4 # sumdup.t
5 # File ID: 72d68c8c-f988-11dd-989d-000475e441b9
7 # Test suite for sumdup(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 = "sumdup";
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 sorted input...');
122 testcmd("sort sumdup-files/files.md5 | $CMD", # {{{
123 <<'END',
125 99754106633f94d350db34d548d6091a file2
126 99754106633f94d350db34d548d6091a file4
127 99754106633f94d350db34d548d6091a file5
129 ab4e78bf3b9d8b1aa18537c3f780245c file3
130 ab4e78bf3b9d8b1aa18537c3f780245c file6
134 'Finds duplicates in sorted output from sumdup-files/files.md5',
137 # }}}
138 testcmd("sort sumdup-files/files.sha1 | $CMD", # {{{
139 <<'END',
141 38d0f91a99c57d189416439ce377ccdcd92639d0 file2
142 38d0f91a99c57d189416439ce377ccdcd92639d0 file4
143 38d0f91a99c57d189416439ce377ccdcd92639d0 file5
144 38d0f91a99c57d189416439ce377ccdcd92639d0 file6
145 38d0f91a99c57d189416439ce377ccdcd92639d0 file7
147 ad2aeab58d53f21a7004250c49fa0a6bff59477d file0
148 ad2aeab58d53f21a7004250c49fa0a6bff59477d file3
150 f62e5bcda4fae4f82370da0c6f20697b8f8447ef file1
151 f62e5bcda4fae4f82370da0c6f20697b8f8447ef file8
152 f62e5bcda4fae4f82370da0c6f20697b8f8447ef file9
156 'Finds duplicates in sorted output from sumdup-files/files.sha1',
159 # }}}
160 testcmd("sort sumdup-files/files.sha256 | $CMD", # {{{
161 <<'END',
163 6ac3c336e4094835293a3fed8a4b5fedde1b5e2626d9838fed50693bba00af0e file2
164 6ac3c336e4094835293a3fed8a4b5fedde1b5e2626d9838fed50693bba00af0e file6
166 dfc7027894e168c3292c50b5461ef8ec635a5e04874b1e13b5ad7596a98f3bda file1
167 dfc7027894e168c3292c50b5461ef8ec635a5e04874b1e13b5ad7596a98f3bda file4
168 dfc7027894e168c3292c50b5461ef8ec635a5e04874b1e13b5ad7596a98f3bda file5
170 e2c4470a88f236a403c49d4be7c8f2f9e5e54f914e0d342ff24ee707ca85f070 file3
171 e2c4470a88f236a403c49d4be7c8f2f9e5e54f914e0d342ff24ee707ca85f070 file7
175 'Finds duplicates in sorted output from sumdup-files/files.sha256',
178 # }}}
180 todo_section:
183 if ($Opt{'all'} || $Opt{'todo'}) {
184 diag('Running TODO tests...'); # {{{
186 TODO: {
188 local $TODO = '';
189 # Insert TODO tests here.
192 # TODO tests }}}
195 diag('Testing finished.');
196 return $Retval;
197 # }}}
198 } # main()
200 sub testcmd {
201 # {{{
202 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
203 defined($descriptions{$Desc}) &&
204 BAIL_OUT("testcmd(): '$Desc' description is used twice");
205 $descriptions{$Desc} = 1;
206 my $stderr_cmd = '';
207 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
208 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
209 my $TMP_STDERR = "$CMDB-stderr.tmp";
210 my $retval = 1;
212 if (defined($Exp_stderr)) {
213 $stderr_cmd = " 2>$TMP_STDERR";
215 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
216 my $ret_val = $?;
217 if (defined($Exp_stderr)) {
218 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
219 unlink($TMP_STDERR);
220 } else {
221 diag("Warning: stderr not defined for '$Txt'");
223 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
225 return $retval;
226 # }}}
227 } # testcmd()
229 sub likecmd {
230 # {{{
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;
235 my $stderr_cmd = '';
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";
239 my $retval = 1;
241 if (defined($Exp_stderr)) {
242 $stderr_cmd = " 2>$TMP_STDERR";
244 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
245 my $ret_val = $?;
246 if (defined($Exp_stderr)) {
247 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
248 unlink($TMP_STDERR);
249 } else {
250 diag("Warning: stderr not defined for '$Txt'");
252 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
254 return $retval;
255 # }}}
256 } # likecmd()
258 sub file_data {
259 # Return file content as a string {{{
260 my $File = shift;
261 my $Txt;
263 open(my $fp, '<', $File) or return undef;
264 local $/ = undef;
265 $Txt = <$fp>;
266 close($fp);
267 return $Txt;
268 # }}}
269 } # file_data()
271 sub print_version {
272 # Print program version {{{
273 print("$progname $VERSION\n");
274 return;
275 # }}}
276 } # print_version()
278 sub usage {
279 # Send the help message to stdout {{{
280 my $Retval = shift;
282 if ($Opt{'verbose'}) {
283 print("\n");
284 print_version();
286 print(<<"END");
288 Usage: $progname [options]
290 Contains tests for the $CMDB(1) program.
292 Options:
294 -a, --all
295 Run all tests, also TODOs.
296 -h, --help
297 Show this help.
298 -q, --quiet
299 Be more quiet. Can be repeated to increase silence.
300 -t, --todo
301 Run only the TODO tests.
302 -v, --verbose
303 Increase level of verbosity. Can be repeated.
304 --version
305 Print version information.
308 exit($Retval);
309 # }}}
310 } # usage()
312 sub msg {
313 # Print a status message to stderr based on verbosity level {{{
314 my ($verbose_level, $Txt) = @_;
316 $verbose_level > $Opt{'verbose'} && return;
317 print(STDERR "$progname: $Txt\n");
318 return;
319 # }}}
320 } # msg()
322 __END__
324 # This program is free software; you can redistribute it and/or modify
325 # it under the terms of the GNU General Public License as published by
326 # the Free Software Foundation; either version 2 of the License, or (at
327 # your option) any later version.
329 # This program is distributed in the hope that it will be useful, but
330 # WITHOUT ANY WARRANTY; without even the implied warranty of
331 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
332 # See the GNU General Public License for more details.
334 # You should have received a copy of the GNU General Public License
335 # along with this program.
336 # If not, see L<http://www.gnu.org/licenses/>.
338 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :