create-ly: band: Create .ly files for all instruments
[sunny256-utils.git] / tests / ga-sumsize.t
blobc68d9f42fe739eea0f32875c0a743b8716cebcc5
1 #!/usr/bin/env perl
3 #=======================================================================
4 # ga-sumsize.t
5 # File ID: e64cce20-5619-11e5-a28a-000df06acc56
7 # Test suite for ga-sumsize(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-sumsize";
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 testcmd("$CMD </dev/null", # {{{
129 "\nga-sumsize: Total size of keys: 0 (0)\n",
132 'Read empty input from stdin',
135 # }}}
136 testcmd("$CMD /dev/null", # {{{
137 "\nga-sumsize: Total size of keys: 0 (0)\n",
140 'Read directly from /dev/null',
143 # }}}
144 testcmd("$CMD ga-sumsize-files/1.txt", # {{{
145 <<END,
146 unused . (checking for unused data...) (checking master...)
147 Some annexed data is no longer used by any files:
148 NUMBER KEY
149 1 SHA256-s487883366--2125edd12f347e19dc9d5c2c2f4cee14b44f9cbba1ea46ff8af54ae020c58563
150 2 SHA256-s484307143--049e14e3af3bf9aece17ddab008b3cb9be6ab0fb42c91e6ad383364d26b3ffa7
151 3 SHA256-s490478481--12453004cbf74f56adf115f9da32d2b783c9c967469668dbf0f88b85efc856b8
152 4 SHA256-s485358886--bf6c061f378e04d6f45a95e13412aabc4c420a714cfebe7ab70034b72605bc47
153 (To see where data was previously used, try: git log --stat -S'KEY')
155 To remove unwanted data: git-annex dropunused NUMBER
159 ga-sumsize: Total size of keys: 1948027876 (2.0G)
163 'Read from 1.txt',
166 # }}}
167 testcmd("$CMD <ga-sumsize-files/1.txt", # {{{
168 <<END,
169 unused . (checking for unused data...) (checking master...)
170 Some annexed data is no longer used by any files:
171 NUMBER KEY
172 1 SHA256-s487883366--2125edd12f347e19dc9d5c2c2f4cee14b44f9cbba1ea46ff8af54ae020c58563
173 2 SHA256-s484307143--049e14e3af3bf9aece17ddab008b3cb9be6ab0fb42c91e6ad383364d26b3ffa7
174 3 SHA256-s490478481--12453004cbf74f56adf115f9da32d2b783c9c967469668dbf0f88b85efc856b8
175 4 SHA256-s485358886--bf6c061f378e04d6f45a95e13412aabc4c420a714cfebe7ab70034b72605bc47
176 (To see where data was previously used, try: git log --stat -S'KEY')
178 To remove unwanted data: git-annex dropunused NUMBER
182 ga-sumsize: Total size of keys: 1948027876 (2.0G)
186 'Read from 1.txt via stdin',
189 # }}}
190 testcmd("$CMD --display ga-sumsize-files/1.txt", # {{{
191 <<END,
192 0 unused . (checking for unused data...) (checking master...)
193 0 Some annexed data is no longer used by any files:
194 0 NUMBER KEY
195 487883366 1 SHA256-s487883366--2125edd12f347e19dc9d5c2c2f4cee14b44f9cbba1ea46ff8af54ae020c58563
196 972190509 2 SHA256-s484307143--049e14e3af3bf9aece17ddab008b3cb9be6ab0fb42c91e6ad383364d26b3ffa7
197 1462668990 3 SHA256-s490478481--12453004cbf74f56adf115f9da32d2b783c9c967469668dbf0f88b85efc856b8
198 1948027876 4 SHA256-s485358886--bf6c061f378e04d6f45a95e13412aabc4c420a714cfebe7ab70034b72605bc47
199 1948027876 (To see where data was previously used, try: git log --stat -S'KEY')
200 1948027876
201 1948027876 To remove unwanted data: git-annex dropunused NUMBER
202 1948027876
203 1948027876 ok
205 ga-sumsize: Total size of keys: 1948027876 (2.0G)
209 'Test --display option',
212 # }}}
213 testcmd("$CMD -d ga-sumsize-files/1.txt", # {{{
214 <<END,
215 0 unused . (checking for unused data...) (checking master...)
216 0 Some annexed data is no longer used by any files:
217 0 NUMBER KEY
218 487883366 1 SHA256-s487883366--2125edd12f347e19dc9d5c2c2f4cee14b44f9cbba1ea46ff8af54ae020c58563
219 972190509 2 SHA256-s484307143--049e14e3af3bf9aece17ddab008b3cb9be6ab0fb42c91e6ad383364d26b3ffa7
220 1462668990 3 SHA256-s490478481--12453004cbf74f56adf115f9da32d2b783c9c967469668dbf0f88b85efc856b8
221 1948027876 4 SHA256-s485358886--bf6c061f378e04d6f45a95e13412aabc4c420a714cfebe7ab70034b72605bc47
222 1948027876 (To see where data was previously used, try: git log --stat -S'KEY')
223 1948027876
224 1948027876 To remove unwanted data: git-annex dropunused NUMBER
225 1948027876
226 1948027876 ok
228 ga-sumsize: Total size of keys: 1948027876 (2.0G)
232 'Test -d option',
235 # }}}
237 if (`git-annex version 2>/dev/null` !~ /^git-annex version/) {
238 diag("git-annex is not installed here, skipping tests");
239 return 0;
242 ok(chdir("ga-sumsize-files"), "chdir ga-sumsize-files");
243 testcmd("tar xzf ga-sumsize-repo.tar.gz", # {{{
247 "Untar ga-sumsize-repo.tar.gz",
250 # }}}
251 ok(chdir("ga-sumsize-repo"), "chdir ga-sumsize-repo");
252 likecmd("ga unused", # {{{
253 '/^.+' .
254 '(' .
255 'SHA256-s1000--541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53\n' .
256 '.+' .
257 'SHA256-s123--409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49\n' .
258 '|' .
259 'SHA256-s123--409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49\n' .
260 '.+' .
261 'SHA256-s1000--541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53\n' .
262 ')' .
263 '.+' .
264 'ga-sumsize: Total size of keys: 1123 \(1\.2k\)\n' .
265 '$/s',
266 '/^$/s',
268 "ga unused",
271 # }}}
272 ok(chdir(".."), "chdir ..");
273 testcmd("chmod -R +w \"ga-sumsize-repo\"", # {{{
277 "chmod -R +w ga-sumsize-repo"
280 # }}}
281 testcmd("rm -rf \"ga-sumsize-repo\"", # {{{
285 "rm -rf ga-sumsize-repo",
288 # }}}
290 todo_section:
293 if ($Opt{'all'} || $Opt{'todo'}) {
294 diag('Running TODO tests...'); # {{{
296 TODO: {
298 local $TODO = '';
299 # Insert TODO tests here.
302 # TODO tests }}}
305 diag('Testing finished.');
306 return $Retval;
307 # }}}
308 } # main()
310 sub testcmd {
311 # {{{
312 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
313 defined($descriptions{$Desc}) &&
314 BAIL_OUT("testcmd(): '$Desc' description is used twice");
315 $descriptions{$Desc} = 1;
316 my $stderr_cmd = '';
317 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
318 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
319 my $TMP_STDERR = "$CMDB-stderr.tmp";
320 my $retval = 1;
322 if (defined($Exp_stderr)) {
323 $stderr_cmd = " 2>$TMP_STDERR";
325 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
326 my $ret_val = $?;
327 if (defined($Exp_stderr)) {
328 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
329 unlink($TMP_STDERR);
330 } else {
331 diag("Warning: stderr not defined for '$Txt'");
333 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
335 return $retval;
336 # }}}
337 } # testcmd()
339 sub likecmd {
340 # {{{
341 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
342 defined($descriptions{$Desc}) &&
343 BAIL_OUT("likecmd(): '$Desc' description is used twice");
344 $descriptions{$Desc} = 1;
345 my $stderr_cmd = '';
346 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
347 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
348 my $TMP_STDERR = "$CMDB-stderr.tmp";
349 my $retval = 1;
351 if (defined($Exp_stderr)) {
352 $stderr_cmd = " 2>$TMP_STDERR";
354 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
355 my $ret_val = $?;
356 if (defined($Exp_stderr)) {
357 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
358 unlink($TMP_STDERR);
359 } else {
360 diag("Warning: stderr not defined for '$Txt'");
362 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
364 return $retval;
365 # }}}
366 } # likecmd()
368 sub file_data {
369 # Return file content as a string {{{
370 my $File = shift;
371 my $Txt;
373 open(my $fp, '<', $File) or return undef;
374 local $/ = undef;
375 $Txt = <$fp>;
376 close($fp);
377 return $Txt;
378 # }}}
379 } # file_data()
381 sub print_version {
382 # Print program version {{{
383 print("$progname $VERSION\n");
384 return;
385 # }}}
386 } # print_version()
388 sub usage {
389 # Send the help message to stdout {{{
390 my $Retval = shift;
392 if ($Opt{'verbose'}) {
393 print("\n");
394 print_version();
396 print(<<"END");
398 Usage: $progname [options]
400 Contains tests for the $CMDB(1) program.
402 Options:
404 -a, --all
405 Run all tests, also TODOs.
406 -h, --help
407 Show this help.
408 -q, --quiet
409 Be more quiet. Can be repeated to increase silence.
410 -t, --todo
411 Run only the TODO tests.
412 -v, --verbose
413 Increase level of verbosity. Can be repeated.
414 --version
415 Print version information.
418 exit($Retval);
419 # }}}
420 } # usage()
422 sub msg {
423 # Print a status message to stderr based on verbosity level {{{
424 my ($verbose_level, $Txt) = @_;
426 $verbose_level > $Opt{'verbose'} && return;
427 print(STDERR "$progname: $Txt\n");
428 return;
429 # }}}
430 } # msg()
432 __END__
434 # This program is free software; you can redistribute it and/or modify
435 # it under the terms of the GNU General Public License as published by
436 # the Free Software Foundation; either version 2 of the License, or (at
437 # your option) any later version.
439 # This program is distributed in the hope that it will be useful, but
440 # WITHOUT ANY WARRANTY; without even the implied warranty of
441 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
442 # See the GNU General Public License for more details.
444 # You should have received a copy of the GNU General Public License
445 # along with this program.
446 # If not, see L<http://www.gnu.org/licenses/>.
448 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :