3 #=======================================================================
5 # File ID: 54f4f91e-573c-11e1-b91f-915288b515ae
7 # Test suite for git-dangling(1).
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 #=======================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMDB = "git-dangling";
28 our $CMD = "../$CMDB";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
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'}) {
66 my $Tmptop = "tmp-git-dangling-t-$$-" . substr(rand, 2, 8);
74 diag
(sprintf('========== Executing %s v%s ==========',
75 $progname, $VERSION));
77 if ($Opt{'todo'} && !$Opt{'all'}) {
83 testcmd("$CMD command", # {{{
96 diag
('Testing -h (--help) option...');
97 likecmd
("$CMD -h", # {{{
101 'Option -h prints help screen',
105 diag
('Testing -v (--verbose) option...');
106 likecmd
("$CMD -h -v", # {{{
107 '/^\n\S+ \d+\.\d+\.\d+/s',
110 'Option -v with -h returns version number and help screen',
114 diag
('Testing --version option...');
115 likecmd
("$CMD --version", # {{{
116 '/^\S+ \d+\.\d+\.\d+/',
119 'Option --version returns version number',
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", # {{{
135 ok
(chdir($repo), "chdir \$repo") || die("$progname: Unable to continue\n");
137 testcmd
('git log --format=format:%H -1', # {{{
138 'd48c5ed0264a0384b135273e08159c1a4bd80a4b',
141 'master is where it should be',
145 testcmd
('git log --format=format:%H -1 origin/expbranch', # {{{
146 'd5d64eb0e240a25134a2222586d0c76252e89d8c',
149 'origin/expbranch is where it should be',
153 likecmd
("git remote rm origin", # {{{
157 'Delete origin remote',
161 testcmd
("$CMD", # {{{
163 git-dangling: Creating commit-d5d64eb0e240a25134a2222586d0c76252e89d8c
167 'Restore origin/expbranch',
171 likecmd
("git branch", # {{{
172 '/^.*commit-d5d64eb0e240a25134a2222586d0c76252e89d8c.*$/s',
175 'expbranch was in fact recreated',
179 ok
(chdir($origdir), "chdir \$origdir");
180 testcmd
("rm -rf $Tmptop", # {{{
188 ok
(!-e
$Tmptop, 'Tempdir is gone');
193 if ($Opt{'all'} || $Opt{'todo'}) {
194 diag
('Running TODO tests...'); # {{{
199 # Insert TODO tests here.
205 diag
('Testing finished.');
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;
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";
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)");
228 if (defined($Exp_stderr)) {
229 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
232 diag
("Warning: stderr not defined for '$Txt'");
234 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
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;
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";
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)");
258 if (defined($Exp_stderr)) {
259 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
262 diag
("Warning: stderr not defined for '$Txt'");
264 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
271 # Return file content as a string {{{
275 open(my $fp, '<', $File) or return undef;
284 # Print program version {{{
285 print("$progname $VERSION\n");
291 # Send the help message to stdout {{{
294 if ($Opt{'verbose'}) {
300 Usage: $progname [options]
302 Contains tests for the $CMDB(1) program.
307 Run all tests, also TODOs.
311 Be more quiet. Can be repeated to increase silence.
313 Run only the TODO tests.
315 Increase level of verbosity. Can be repeated.
317 Print version information.
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");
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 :