installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / tests / git-scanrefs.t
blob96c580834abbb7ee4da988a426909e7e251cad5a
1 #!/usr/bin/env perl
3 #=======================================================================
4 # git-scanrefs.t
5 # File ID: 506b8efe-2825-11e5-bc20-000df06acc56
7 # Test suite for git-scanrefs(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 qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 local $| = 1;
27 our $CMDB = "git-scanrefs";
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 exit(main());
68 sub main {
69 # {{{
70 my $Retval = 0;
72 diag(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
76 goto todo_section;
79 =pod
81 testcmd("$CMD command", # {{{
82 <<'END',
83 [expected stdout]
84 END
85 '',
87 'description',
90 # }}}
92 =cut
94 diag('Testing -h (--help) option...');
95 likecmd("$CMD -h", # {{{
96 '/ Show this help/i',
97 '/^$/',
99 'Option -h prints help screen',
102 # }}}
103 diag('Testing -v (--verbose) option...');
104 likecmd("$CMD -hv", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
106 '/^$/',
108 'Option -v with -h returns version number and help screen',
111 # }}}
112 diag('Testing --version option...');
113 likecmd("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
115 '/^$/',
117 'Option --version returns version number',
120 # }}}
121 my $stdtxt = 'skdjfv151da78gbhs12345abower aaaabbbb01234 asdf 2345 6789';
122 testcmd("echo $stdtxt | $CMD -vvv", # {{{
123 <<'END',
124 151da78d6363d20e9a64e478835985ce378fc54f
126 "git-scanrefs: potential_refs('skdjfv151da78gbhs12345abower aaaabbbb01234 asdf 2345 6789') returns ['151da78', '12345ab', 'aaaabbbb01234']\n",
128 'List potential unchecked refs from line',
131 # }}}
132 testcmd("echo $stdtxt $stdtxt | $CMD", # {{{
133 <<'END',
134 151da78d6363d20e9a64e478835985ce378fc54f
138 'Don\'t list duplicate info',
141 # }}}
142 testcmd("echo $stdtxt 151da78d6363d20e $stdtxt | $CMD", # {{{
143 <<'END',
144 151da78d6363d20e9a64e478835985ce378fc54f
148 'Not even when the sha has different length',
151 # }}}
153 todo_section:
156 if ($Opt{'all'} || $Opt{'todo'}) {
157 diag('Running TODO tests...'); # {{{
159 TODO: {
161 local $TODO = '';
162 # Insert TODO tests here.
165 # TODO tests }}}
168 diag('Testing finished.');
169 return $Retval;
170 # }}}
171 } # main()
173 sub testcmd {
174 # {{{
175 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
176 defined($descriptions{$Desc}) &&
177 BAIL_OUT("testcmd(): '$Desc' description is used twice");
178 $descriptions{$Desc} = 1;
179 my $stderr_cmd = '';
180 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
181 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
182 my $TMP_STDERR = "$CMDB-stderr.tmp";
183 my $retval = 1;
185 if (defined($Exp_stderr)) {
186 $stderr_cmd = " 2>$TMP_STDERR";
188 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
189 my $ret_val = $?;
190 if (defined($Exp_stderr)) {
191 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
192 unlink($TMP_STDERR);
193 } else {
194 diag("Warning: stderr not defined for '$Txt'");
196 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
198 return $retval;
199 # }}}
200 } # testcmd()
202 sub likecmd {
203 # {{{
204 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
205 defined($descriptions{$Desc}) &&
206 BAIL_OUT("likecmd(): '$Desc' description is used twice");
207 $descriptions{$Desc} = 1;
208 my $stderr_cmd = '';
209 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
210 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
211 my $TMP_STDERR = "$CMDB-stderr.tmp";
212 my $retval = 1;
214 if (defined($Exp_stderr)) {
215 $stderr_cmd = " 2>$TMP_STDERR";
217 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
218 my $ret_val = $?;
219 if (defined($Exp_stderr)) {
220 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
221 unlink($TMP_STDERR);
222 } else {
223 diag("Warning: stderr not defined for '$Txt'");
225 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
227 return $retval;
228 # }}}
229 } # likecmd()
231 sub file_data {
232 # Return file content as a string {{{
233 my $File = shift;
234 my $Txt;
236 open(my $fp, '<', $File) or return undef;
237 local $/ = undef;
238 $Txt = <$fp>;
239 close($fp);
240 return $Txt;
241 # }}}
242 } # file_data()
244 sub print_version {
245 # Print program version {{{
246 print("$progname $VERSION\n");
247 return;
248 # }}}
249 } # print_version()
251 sub usage {
252 # Send the help message to stdout {{{
253 my $Retval = shift;
255 if ($Opt{'verbose'}) {
256 print("\n");
257 print_version();
259 print(<<"END");
261 Usage: $progname [options]
263 Contains tests for the $CMDB(1) program.
265 Options:
267 -a, --all
268 Run all tests, also TODOs.
269 -h, --help
270 Show this help.
271 -q, --quiet
272 Be more quiet. Can be repeated to increase silence.
273 -t, --todo
274 Run only the TODO tests.
275 -v, --verbose
276 Increase level of verbosity. Can be repeated.
277 --version
278 Print version information.
281 exit($Retval);
282 # }}}
283 } # usage()
285 sub msg {
286 # Print a status message to stderr based on verbosity level {{{
287 my ($verbose_level, $Txt) = @_;
289 $verbose_level > $Opt{'verbose'} && return;
290 print(STDERR "$progname: $Txt\n");
291 return;
292 # }}}
293 } # msg()
295 __END__
297 # This program is free software; you can redistribute it and/or modify
298 # it under the terms of the GNU General Public License as published by
299 # the Free Software Foundation; either version 2 of the License, or (at
300 # your option) any later version.
302 # This program is distributed in the hope that it will be useful, but
303 # WITHOUT ANY WARRANTY; without even the implied warranty of
304 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
305 # See the GNU General Public License for more details.
307 # You should have received a copy of the GNU General Public License
308 # along with this program.
309 # If not, see L<http://www.gnu.org/licenses/>.
311 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :