create-ly: band: Create .ly files for all instruments
[sunny256-utils.git] / tests / git-dangling.t
blob3f04d50cbb8a452e506f86bc35c310addc24c5f8
1 #!/usr/bin/env perl
3 #=======================================================================
4 # git-dangling.t
5 # File ID: 54f4f91e-573c-11e1-b91f-915288b515ae
7 # Test suite for git-dangling(1).
9 # Character set: UTF-8
10 # ©opyleft 2012– Ø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 = "git-dangling";
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 my $Tmptop = "tmp-git-dangling-t-$$-" . substr(rand, 2, 8);
68 exit(main());
70 sub main {
71 # {{{
72 my $Retval = 0;
74 diag(sprintf('========== Executing %s v%s ==========',
75 $progname, $VERSION));
77 if ($Opt{'todo'} && !$Opt{'all'}) {
78 goto todo_section;
81 =pod
83 testcmd("$CMD command", # {{{
84 <<'END',
85 [expected stdout]
86 END
87 '',
89 'description',
92 # }}}
94 =cut
96 diag('Testing -h (--help) option...');
97 likecmd("$CMD -h", # {{{
98 '/ Show this help/i',
99 '/^$/',
101 'Option -h prints help screen',
104 # }}}
105 diag('Testing -v (--verbose) option...');
106 likecmd("$CMD -h -v", # {{{
107 '/^\n\S+ \d+\.\d+\.\d+/s',
108 '/^$/',
110 'Option -v with -h returns version number and help screen',
113 # }}}
114 diag('Testing --version option...');
115 likecmd("$CMD --version", # {{{
116 '/^\S+ \d+\.\d+\.\d+/',
117 '/^$/',
119 'Option --version returns version number',
122 # }}}
123 my $repo = "$Tmptop/repo";
124 chomp(my $origdir = `pwd`);
126 ok(mkdir($Tmptop), "mkdir \$Tmptop") || die("$progname: Unable to continue\n");
127 likecmd("git clone git-dangling-files/repo.bundle $repo", # {{{
128 '/.*/',
129 '/.*/',
131 'Clone repo.bundle',
134 # }}}
135 ok(chdir($repo), "chdir \$repo") || die("$progname: Unable to continue\n");
136 $CMD = "../../$CMD";
137 testcmd('git log --format=format:%H -1', # {{{
138 'd48c5ed0264a0384b135273e08159c1a4bd80a4b',
141 'master is where it should be',
144 # }}}
145 testcmd('git log --format=format:%H -1 origin/expbranch', # {{{
146 'd5d64eb0e240a25134a2222586d0c76252e89d8c',
149 'origin/expbranch is where it should be',
152 # }}}
153 likecmd("git remote rm origin", # {{{
154 '/^$/',
155 '/^$/',
157 'Delete origin remote',
160 # }}}
161 testcmd("$CMD", # {{{
162 <<END,
163 git-dangling: Creating commit-d5d64eb0e240a25134a2222586d0c76252e89d8c
167 'Restore origin/expbranch',
170 # }}}
171 likecmd("git branch", # {{{
172 '/^.*commit-d5d64eb0e240a25134a2222586d0c76252e89d8c.*$/s',
173 '/^$/',
175 'expbranch was in fact recreated',
178 # }}}
179 ok(chdir($origdir), "chdir \$origdir");
180 testcmd("rm -rf $Tmptop", # {{{
184 'Remove tempdir',
187 # }}}
188 ok(!-e $Tmptop, 'Tempdir is gone');
190 todo_section:
193 if ($Opt{'all'} || $Opt{'todo'}) {
194 diag('Running TODO tests...'); # {{{
196 TODO: {
198 local $TODO = '';
199 # Insert TODO tests here.
202 # TODO tests }}}
205 diag('Testing finished.');
206 return $Retval;
207 # }}}
208 } # main()
210 sub testcmd {
211 # {{{
212 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
213 defined($descriptions{$Desc}) &&
214 BAIL_OUT("testcmd(): '$Desc' description is used twice");
215 $descriptions{$Desc} = 1;
216 my $stderr_cmd = '';
217 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
218 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
219 my $TMP_STDERR = "$CMDB-stderr.tmp";
220 my $retval = 1;
222 if (defined($Exp_stderr)) {
223 $stderr_cmd = " 2>$TMP_STDERR";
225 $Txt =~ s/$Tmptop/[Tmptop]/g; # Remove unique variations from output
226 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
227 my $ret_val = $?;
228 if (defined($Exp_stderr)) {
229 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
230 unlink($TMP_STDERR);
231 } else {
232 diag("Warning: stderr not defined for '$Txt'");
234 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
236 return $retval;
237 # }}}
238 } # testcmd()
240 sub likecmd {
241 # {{{
242 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
243 defined($descriptions{$Desc}) &&
244 BAIL_OUT("likecmd(): '$Desc' description is used twice");
245 $descriptions{$Desc} = 1;
246 my $stderr_cmd = '';
247 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
248 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
249 my $TMP_STDERR = "$CMDB-stderr.tmp";
250 my $retval = 1;
252 if (defined($Exp_stderr)) {
253 $stderr_cmd = " 2>$TMP_STDERR";
255 $Txt =~ s/$Tmptop/[Tmptop]/g; # Remove unique variations from output
256 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
257 my $ret_val = $?;
258 if (defined($Exp_stderr)) {
259 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
260 unlink($TMP_STDERR);
261 } else {
262 diag("Warning: stderr not defined for '$Txt'");
264 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
266 return $retval;
267 # }}}
268 } # likecmd()
270 sub file_data {
271 # Return file content as a string {{{
272 my $File = shift;
273 my $Txt;
275 open(my $fp, '<', $File) or return undef;
276 local $/ = undef;
277 $Txt = <$fp>;
278 close($fp);
279 return $Txt;
280 # }}}
281 } # file_data()
283 sub print_version {
284 # Print program version {{{
285 print("$progname $VERSION\n");
286 return;
287 # }}}
288 } # print_version()
290 sub usage {
291 # Send the help message to stdout {{{
292 my $Retval = shift;
294 if ($Opt{'verbose'}) {
295 print("\n");
296 print_version();
298 print(<<"END");
300 Usage: $progname [options]
302 Contains tests for the $CMDB(1) program.
304 Options:
306 -a, --all
307 Run all tests, also TODOs.
308 -h, --help
309 Show this help.
310 -q, --quiet
311 Be more quiet. Can be repeated to increase silence.
312 -t, --todo
313 Run only the TODO tests.
314 -v, --verbose
315 Increase level of verbosity. Can be repeated.
316 --version
317 Print version information.
320 exit($Retval);
321 # }}}
322 } # usage()
324 sub msg {
325 # Print a status message to stderr based on verbosity level {{{
326 my ($verbose_level, $Txt) = @_;
328 $verbose_level > $Opt{'verbose'} && return;
329 print(STDERR "$progname: $Txt\n");
330 return;
331 # }}}
332 } # msg()
334 __END__
336 # This program is free software; you can redistribute it and/or modify
337 # it under the terms of the GNU General Public License as published by
338 # the Free Software Foundation; either version 2 of the License, or (at
339 # your option) any later version.
341 # This program is distributed in the hope that it will be useful, but
342 # WITHOUT ANY WARRANTY; without even the implied warranty of
343 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
344 # See the GNU General Public License for more details.
346 # You should have received a copy of the GNU General Public License
347 # along with this program.
348 # If not, see L<http://www.gnu.org/licenses/>.
350 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :