std.c: Don't use `EXIT_SUCCESS`/`EXIT_FAILURE` as function return values
[sunny256-utils.git] / tests / ga-fsck-size.t
blobb4f7608b398eb802cfbf1d5e627e3c0edf2a4c42
1 #!/usr/bin/env perl
3 #=======================================================================
4 # ga-fsck-size.t
5 # File ID: 4ac10522-2719-11e5-85fa-000df06acc56
7 # Test suite for ga-fsck-size(1).
9 # Character set: UTF-8
10 # ©opyleft 2015– Ø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;
20 if (eval { require Number::Bytes::Human; }) {
21 plan "no_plan";
22 } else {
23 plan "skip_all" => "Number::Bytes::Human is not installed,"
24 . " skipping tests";
26 # use_ok() goes here
29 use Getopt::Long;
31 local $| = 1;
33 our $CMDB = "ga-fsck-size";
34 our $CMD = "../$CMDB";
36 our %Opt = (
38 'all' => 0,
39 'help' => 0,
40 'quiet' => 0,
41 'todo' => 0,
42 'verbose' => 0,
43 'version' => 0,
47 our $progname = $0;
48 $progname =~ s/^.*\/(.*?)$/$1/;
49 our $VERSION = '0.0.0';
51 my %descriptions = ();
53 Getopt::Long::Configure('bundling');
54 GetOptions(
56 'all|a' => \$Opt{'all'},
57 'help|h' => \$Opt{'help'},
58 'quiet|q+' => \$Opt{'quiet'},
59 'todo|t' => \$Opt{'todo'},
60 'verbose|v+' => \$Opt{'verbose'},
61 'version' => \$Opt{'version'},
63 ) || die("$progname: Option error. Use -h for help.\n");
65 $Opt{'verbose'} -= $Opt{'quiet'};
66 $Opt{'help'} && usage(0);
67 if ($Opt{'version'}) {
68 print_version();
69 exit(0);
72 exit(main());
74 sub main {
75 # {{{
76 my $Retval = 0;
78 diag(sprintf('========== Executing %s v%s ==========',
79 $progname, $VERSION));
81 if ($Opt{'todo'} && !$Opt{'all'}) {
82 goto todo_section;
85 =pod
87 testcmd("$CMD command", # {{{
88 <<'END',
89 [expected stdout]
90 END
91 '',
93 'description',
96 # }}}
98 =cut
100 diag('Testing -h (--help) option...');
101 likecmd("$CMD -h", # {{{
102 '/ Show this help/i',
103 '/^$/',
105 'Option -h prints help screen',
108 # }}}
109 diag('Testing -v (--verbose) option...');
110 likecmd("$CMD -hv", # {{{
111 '/^\n\S+ \d+\.\d+\.\d+/s',
112 '/^$/',
114 'Option -v with -h returns version number and help screen',
117 # }}}
118 diag('Testing --version option...');
119 likecmd("$CMD --version", # {{{
120 '/^\S+ \d+\.\d+\.\d+/',
121 '/^$/',
123 'Option --version returns version number',
126 # }}}
128 if (`git-annex version 2>/dev/null` !~ /^git-annex version/) {
129 diag("git-annex is not installed here, skipping tests");
130 return 0;
133 diag("Initialise temprepo...");
134 ok(chdir('ga-fsck-size-files'), 'chdir ga-fsck-size-files');
135 testcmd('tar xzf annex-backends.tar.gz', '', '', 0, 'Untar annex-backends.tar.gz');
137 ok(chdir('annex-backends'), 'chdir annex-backends');
139 testcmd("git annex fsck 2>&1 | grep -v ^fsck | ../../$CMD", # {{{
140 <<END,
141 (recording state in git...)
143 Total size of files that need more copies: 0
144 Total space needed to get enough copies : 0
148 "Filter 'git annex fsck' through $CMD"
151 # }}}
152 likecmd("git annex fsck --numcopies=2 2>&1 | ../../$CMD", # {{{
154 '/^' .
155 'fsck MD5.txt \(checksum...\)' .
156 '.+' .
157 'Only 1 of 2 trustworthy copies exist of MD5\.txt \(4(\.0)? bytes\)\n' .
158 'failed\n' .
159 '.+' .
160 'Only 1 of 2 trustworthy copies exist of SHA256\.txt \(7(\.0)? bytes\)\n' .
161 'failed\n' .
162 '.+' .
163 'fsck: 25 failed\n' .
164 '\n' .
165 'Total size of files that need more copies: 199\n' .
166 'Total space needed to get enough copies : 199\n' .
167 '$/s'
169 '/^$/',
171 "Copies missing when numcopies=2",
174 # }}}
176 ok(chdir('..'), 'chdir ..');
178 diag('Clean up temporary files...');
179 testcmd('chmod -R +w annex-backends', '', '', 0, 'chmod everything under annex-backends/ to +write');
180 testcmd('rm -rf annex-backends', '', '', 0, 'Delete temp repo');
181 ok(!-e 'annex-backends', 'annex-backends is gone');
183 todo_section:
186 if ($Opt{'all'} || $Opt{'todo'}) {
187 diag('Running TODO tests...'); # {{{
189 TODO: {
191 local $TODO = '';
192 # Insert TODO tests here.
195 # TODO tests }}}
198 diag('Testing finished.');
199 return $Retval;
200 # }}}
201 } # main()
203 sub testcmd {
204 # {{{
205 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
206 defined($descriptions{$Desc}) &&
207 BAIL_OUT("testcmd(): '$Desc' description is used twice");
208 $descriptions{$Desc} = 1;
209 my $stderr_cmd = '';
210 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
211 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
212 my $TMP_STDERR = "$CMDB-stderr.tmp";
213 my $retval = 1;
215 if (defined($Exp_stderr)) {
216 $stderr_cmd = " 2>$TMP_STDERR";
218 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
219 my $ret_val = $?;
220 if (defined($Exp_stderr)) {
221 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
222 unlink($TMP_STDERR);
223 } else {
224 diag("Warning: stderr not defined for '$Txt'");
226 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
228 return $retval;
229 # }}}
230 } # testcmd()
232 sub likecmd {
233 # {{{
234 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
235 defined($descriptions{$Desc}) &&
236 BAIL_OUT("likecmd(): '$Desc' description is used twice");
237 $descriptions{$Desc} = 1;
238 my $stderr_cmd = '';
239 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
240 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
241 my $TMP_STDERR = "$CMDB-stderr.tmp";
242 my $retval = 1;
244 if (defined($Exp_stderr)) {
245 $stderr_cmd = " 2>$TMP_STDERR";
247 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
248 my $ret_val = $?;
249 if (defined($Exp_stderr)) {
250 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
251 unlink($TMP_STDERR);
252 } else {
253 diag("Warning: stderr not defined for '$Txt'");
255 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
257 return $retval;
258 # }}}
259 } # likecmd()
261 sub file_data {
262 # Return file content as a string {{{
263 my $File = shift;
264 my $Txt;
266 open(my $fp, '<', $File) or return undef;
267 local $/ = undef;
268 $Txt = <$fp>;
269 close($fp);
270 return $Txt;
271 # }}}
272 } # file_data()
274 sub print_version {
275 # Print program version {{{
276 print("$progname $VERSION\n");
277 return;
278 # }}}
279 } # print_version()
281 sub usage {
282 # Send the help message to stdout {{{
283 my $Retval = shift;
285 if ($Opt{'verbose'}) {
286 print("\n");
287 print_version();
289 print(<<"END");
291 Usage: $progname [options]
293 Contains tests for the $CMDB(1) program.
295 Options:
297 -a, --all
298 Run all tests, also TODOs.
299 -h, --help
300 Show this help.
301 -q, --quiet
302 Be more quiet. Can be repeated to increase silence.
303 -t, --todo
304 Run only the TODO tests.
305 -v, --verbose
306 Increase level of verbosity. Can be repeated.
307 --version
308 Print version information.
311 exit($Retval);
312 # }}}
313 } # usage()
315 sub msg {
316 # Print a status message to stderr based on verbosity level {{{
317 my ($verbose_level, $Txt) = @_;
319 $verbose_level > $Opt{'verbose'} && return;
320 print(STDERR "$progname: $Txt\n");
321 return;
322 # }}}
323 } # msg()
325 __END__
327 # This program is free software; you can redistribute it and/or modify
328 # it under the terms of the GNU General Public License as published by
329 # the Free Software Foundation; either version 2 of the License, or (at
330 # your option) any later version.
332 # This program is distributed in the hope that it will be useful, but
333 # WITHOUT ANY WARRANTY; without even the implied warranty of
334 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
335 # See the GNU General Public License for more details.
337 # You should have received a copy of the GNU General Public License
338 # along with this program.
339 # If not, see L<http://www.gnu.org/licenses/>.
341 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :