installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / tests / age.t
blob9aad689935cbb47fd746fb9cc7e023625cbcf60a
1 #!/usr/bin/env perl
3 #==============================================================================
4 # age.t
5 # File ID: 294ca6f6-7396-11eb-bf84-5582e081d110
7 # Test suite for age(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;
25 local $| = 1;
27 our $CMDB = "age";
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'; # Not used here, $CMD decides
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 my $exec_version = `$CMD --version`;
68 exit(main());
70 sub main {
71 my $Retval = 0;
73 diag('========== BEGIN version info ==========');
74 diag($exec_version);
75 diag('=========== END version info ===========');
77 if ($Opt{'todo'} && !$Opt{'all'}) {
78 goto todo_section;
81 test_standard_options();
82 test_executable();
84 diag('========== BEGIN version info ==========');
85 diag($exec_version);
86 diag('=========== END version info ===========');
88 todo_section:
91 if ($Opt{'all'} || $Opt{'todo'}) {
92 diag('Running TODO tests...');
93 TODO: {
94 local $TODO = '';
95 # Insert TODO tests here.
99 diag('Testing finished.');
101 return $Retval;
104 sub test_standard_options {
105 diag('Testing -h (--help) option...');
106 likecmd("$CMD -h",
107 '/ Show this help/i',
108 '/^$/',
110 'Option -h prints help screen');
112 diag('Testing -v (--verbose) option...');
113 likecmd("$CMD -h -v",
114 '/^\n\S+ \d+\.\d+\.\d+/s',
115 '/^$/',
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+/',
122 '/^$/',
124 'Option --version returns version number');
126 return;
129 sub test_executable {
130 my $h = 3600;
131 my $d = 24 * $h;
132 my $y = 365 * $d;
133 my $ly = $y + $d;
135 test_range("1970-01-01", "1979-01-01", 9 * $y + 2 * $d, "");
136 test_range("1995-02-02 12:00:00", "1995-07-12 12:00:00",
137 160 * $d, "");
138 test_range("1995-07-12 12:00:00", "1995-02-02 12:00:00",
139 -160 * $d, "");
140 test_range("1814-05-17", "1815-05-17", $y, "");
141 test_range("0765-07-08", "0766-07-08", $y, "");
142 test_range("0765-07-08 17:13:13", "0766-07-08 17:13:13", $y, "");
143 test_range("0765-07-08 17:13:13", "0766-07-08 17:13:14", $y + 1, "");
144 test_range("7777-07-08 17:13:13", "7778-07-08 17:13:13", $y, "");
145 test_range("7777-07-08 17:13:13", "7778-07-08 17:13:14", $y + 1, "");
146 test_range("1900-02-28", "1900-03-01", $d, "");
147 test_range("1904-02-28", "1904-03-01", 2 * $d, "");
148 test_range("1976-02-28", "1976-03-01", 2 * $d, "");
149 test_range("2000-02-28", "2000-03-01", 2 * $d, "");
151 return;
154 sub test_range {
155 my ($begin, $end, $secs, $opts) = @_;
157 length($opts) && ($opts = " $opts");
158 chomp (my $dfmt = `echo $secs | datefmt`);
160 testcmd("$CMD$opts \"$begin\" \"$end\"",
161 "$dfmt\n",
164 "$CMD$opts \"$begin\" \"$end\"");
166 for my $o ('-s', '--seconds') {
167 testcmd("$CMD $o$opts \"$begin\" \"$end\"",
168 "$secs\n",
171 "$CMD $o$opts \"$begin\" \"$end\"");
174 return;
177 sub testcmd {
178 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
179 defined($descriptions{$Desc})
180 && BAIL_OUT("testcmd(): '$Desc' description is used twice");
181 $descriptions{$Desc} = 1;
182 my $stderr_cmd = '';
183 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
184 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
185 my $TMP_STDERR = "$CMDB-stderr.tmp";
186 my $retval = 1;
188 if (defined($Exp_stderr)) {
189 $stderr_cmd = " 2>$TMP_STDERR";
191 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
192 my $ret_val = $?;
193 if (defined($Exp_stderr)) {
194 $retval &= is(file_data($TMP_STDERR),
195 $Exp_stderr, "$Txt (stderr)");
196 unlink($TMP_STDERR);
197 } else {
198 diag("Warning: stderr not defined for '$Txt'");
200 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
202 return $retval;
205 sub likecmd {
206 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
207 defined($descriptions{$Desc})
208 && BAIL_OUT("likecmd(): '$Desc' description is used twice");
209 $descriptions{$Desc} = 1;
210 my $stderr_cmd = '';
211 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
212 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
213 my $TMP_STDERR = "$CMDB-stderr.tmp";
214 my $retval = 1;
216 if (defined($Exp_stderr)) {
217 $stderr_cmd = " 2>$TMP_STDERR";
219 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
220 my $ret_val = $?;
221 if (defined($Exp_stderr)) {
222 $retval &= like(file_data($TMP_STDERR),
223 $Exp_stderr, "$Txt (stderr)");
224 unlink($TMP_STDERR);
225 } else {
226 diag("Warning: stderr not defined for '$Txt'");
228 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
230 return $retval;
233 sub file_data {
234 # Return file content as a string
235 my $File = shift;
236 my $Txt;
238 open(my $fp, '<', $File) or return undef;
239 local $/ = undef;
240 $Txt = <$fp>;
241 close($fp);
243 return $Txt;
246 sub create_file {
247 # Create new file and fill it with data
248 my ($file, $text) = @_;
249 my $retval = 0;
251 open(my $fp, ">", $file) or return 0;
252 print($fp $text);
253 close($fp);
254 $retval = is(file_data($file), $text,
255 "$file was successfully created");
257 return $retval; # 0 if error, 1 if ok
260 sub print_version {
261 # Print program version
262 print("$progname $VERSION\n");
264 return;
267 sub usage {
268 # Send the help message to stdout
269 my $Retval = shift;
271 if ($Opt{'verbose'}) {
272 print("\n");
273 print_version();
275 print(<<"END");
277 Usage: $progname [options]
279 Contains tests for the $CMDB(1) program.
281 Options:
283 -a, --all
284 Run all tests, also TODOs.
285 -h, --help
286 Show this help.
287 -q, --quiet
288 Be more quiet. Can be repeated to increase silence.
289 -t, --todo
290 Run only the TODO tests.
291 -v, --verbose
292 Increase level of verbosity. Can be repeated.
293 --version
294 Print version information.
297 exit($Retval);
300 sub msg {
301 # Print a status message to stderr based on verbosity level
302 my ($verbose_level, $Txt) = @_;
304 $verbose_level > $Opt{'verbose'} && return;
305 print(STDERR "$progname: $Txt\n");
307 return;
310 __END__
312 # This program is free software; you can redistribute it and/or modify it under
313 # the terms of the GNU General Public License as published by the Free Software
314 # Foundation; either version 2 of the License, or (at your option) any later
315 # version.
317 # This program is distributed in the hope that it will be useful, but WITHOUT
318 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
319 # FOR A PARTICULAR PURPOSE.
320 # See the GNU General Public License for more details.
322 # You should have received a copy of the GNU General Public License along with
323 # this program.
324 # If not, see L<http://www.gnu.org/licenses/>.
326 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :