installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / tests / gotexp.t
blobb3a0a1783513c0c46ecbae40e35f1199d0d53e7d
1 #!/usr/bin/env perl
3 #=======================================================================
4 # gotexp.t
5 # File ID: 1af95184-2988-11e5-a2f9-000df06acc56
7 # Test suite for gotexp(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 = "gotexp";
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 # }}}
122 ok(chdir('gotexp-files'), 'chdir gotexp-files') || BAIL_OUT('Could not chdir');
123 testcmd("../$CMD <output1.txt", # {{{
127 'Use output1.txt as stdin',
130 # }}}
132 my $op1_1_got = <<END;
133 not ok 11 - "../../conv-suuid test.xml" - Read test.xml (stdout)
134 '<suuid t="2015-06-14T02:34:41.5608070Z" u="e8f90906-123d-11e5-81a8-000df06acc56"> <tag>std</tag> <txt>std -l python suuids-to-postgres.py</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/4</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
135 # <suuid t="2015-06-14T02:51:50.4477750Z" u="4e3cba36-1240-11e5-ab4e-000df06acc56"> <tag>ti</tag> <tag>another</tag> <txt>Yo mainn.</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/13</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
136 # <suuid t="2015-06-21T10:49:19.2036620Z" u="2b1e350c-1803-11e5-9c66-000df06acc56"> <txt>Weird characters: \\ ' ; &lt; &gt; "</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/tests</cwd> <user>sunny</user> <tty>/dev/pts/15</tty> <sess desc="xterm">edcbd7d8-16ca-11e5-9739-000df06acc56</sess> <sess desc="logging">03a706ae-16cb-11e5-becb-000df06acc56</sess> <sess desc="screen">088f9e56-16cb-11e5-a56c-000df06acc56</sess> </suuid>
137 # <suuid t="2015-07-14T02:07:50.9817960Z" u="2162ae68-29cd-11e5-aa3e-000df06acc56"> </suuid>
140 my $op1_1_exp = <<END;
141 not ok 11 - "../../conv-suuid test.xml" - Read test.xml (stdout)
142 '<suuid t="2015-06-14T02:34:41.5608070Z" u="e8f90906-123d-11e5-81a8-000df06acc56"> <tag>std</tag> <txt>std -l python suuids-to-postgres.py</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/4</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
143 # <suuid t="2015-06-14T02:51:50.4477750Z" u="4e3cba36-1240-11e5-ab4e-000df06acc56"> <tag>ti</tag> <tag>another</tag> <txt>Yo mainn.</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/13</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
144 # <suuid t="2015-06-21T10:49:19.2036620Z" u="2b1e350c-1803-11e5-9c66-000df06acc56"> <txt>Weird characters: \\\\ '' ; &lt; &gt; "</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/tests</cwd> <user>sunny</user> <tty>/dev/pts/15</tty> <sess desc="xterm">edcbd7d8-16ca-11e5-9739-000df06acc56</sess> <sess desc="logging">03a706ae-16cb-11e5-becb-000df06acc56</sess> <sess desc="screen">088f9e56-16cb-11e5-a56c-000df06acc56</sess> </suuid>
145 # <suuid t="2015-07-14T02:07:50.9817960Z" u="2162ae68-29cd-11e5-aa3e-000df06acc56"> </suuid>
148 my $op1_2_got = <<END;
149 not ok 14 - "../../conv-suuid -o xml test.xml" - Output XML format (stdout)
150 '<suuid t="2015-06-14T02:34:41.5608070Z" u="e8f90906-123d-11e5-81a8-000df06acc56"> <tag>std</tag> <txt>std -l python suuids-to-postgres.py</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/4</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
151 # <suuid t="2015-06-14T02:51:50.4477750Z" u="4e3cba36-1240-11e5-ab4e-000df06acc56"> <tag>ti</tag> <tag>another</tag> <txt>Yo mainn.</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/13</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
152 # <suuid t="2015-06-21T10:49:19.2036620Z" u="2b1e350c-1803-11e5-9c66-000df06acc56"> <txt>Weird characters: \\ ' ; &lt; &gt; "</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/tests</cwd> <user>sunny</user> <tty>/dev/pts/15</tty> <sess desc="xterm">edcbd7d8-16ca-11e5-9739-000df06acc56</sess> <sess desc="logging">03a706ae-16cb-11e5-becb-000df06acc56</sess> <sess desc="screen">088f9e56-16cb-11e5-a56c-000df06acc56</sess> </suuid>
153 # <suuid t="2015-07-14T02:07:50.9817960Z" u="2162ae68-29cd-11e5-aa3e-000df06acc56"> </suuid>
156 my $op1_2_exp = <<END;
157 not ok 14 - "../../conv-suuid -o xml test.xml" - Output XML format (stdout)
158 '<suuid t="2015-06-14T02:34:41.5608070Z" u="e8f90906-123d-11e5-81a8-000df06acc56"> <tag>std</tag> <txt>std -l python suuids-to-postgres.py</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/4</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
159 # <suuid t="2015-06-14T02:51:50.4477750Z" u="4e3cba36-1240-11e5-ab4e-000df06acc56"> <tag>ti</tag> <tag>another</tag> <txt>Yo mainn.</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/postgres</cwd> <user>sunny</user> <tty>/dev/pts/13</tty> <sess desc="xterm">f923e8fc-11e6-11e5-913a-000df06acc56</sess> <sess desc="logging">09733f50-11e7-11e5-a1ac-000df06acc56</sess> <sess>0bb564f0-11e7-11e5-bc0c-000df06acc56</sess> </suuid>
160 # <suuid t="2015-06-21T10:49:19.2036620Z" u="2b1e350c-1803-11e5-9c66-000df06acc56"> <txt>Weird characters: \\\\ '' ; &lt; &gt; "</txt> <host>bellmann</host> <cwd>/home/sunny/src/git/.er_ikke_i_bellmann/utils.dev/Git/suuid/tests</cwd> <user>sunny</user> <tty>/dev/pts/15</tty> <sess desc="xterm">edcbd7d8-16ca-11e5-9739-000df06acc56</sess> <sess desc="logging">03a706ae-16cb-11e5-becb-000df06acc56</sess> <sess desc="screen">088f9e56-16cb-11e5-a56c-000df06acc56</sess> </suuid>
161 # <suuid t="2015-07-14T02:07:50.9817960Z" u="2162ae68-29cd-11e5-aa3e-000df06acc56"> </suuid>
165 is(file_data('got'), $op1_1_got, "Check contents of 'got'");
166 is(file_data('exp'), $op1_1_exp, "Check contents of 'exp'");
167 testcmd("../$CMD -n 2 output1.txt", # {{{
171 'Use second error in output1.txt',
174 # }}}
175 is(file_data('got'), $op1_2_got, "Second check of 'got'");
176 is(file_data('exp'), $op1_2_exp, "'exp' checked the second time");
178 ok(unlink('got'), "Delete 'got'");
179 ok(unlink('exp'), "Delete 'exp'");
181 todo_section:
184 if ($Opt{'all'} || $Opt{'todo'}) {
185 diag('Running TODO tests...'); # {{{
187 TODO: {
189 local $TODO = '';
190 # Insert TODO tests here.
193 # TODO tests }}}
196 diag('Testing finished.');
197 return $Retval;
198 # }}}
199 } # main()
201 sub testcmd {
202 # {{{
203 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
204 defined($descriptions{$Desc}) &&
205 BAIL_OUT("testcmd(): '$Desc' description is used twice");
206 $descriptions{$Desc} = 1;
207 my $stderr_cmd = '';
208 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
209 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
210 my $TMP_STDERR = "$CMDB-stderr.tmp";
211 my $retval = 1;
213 if (defined($Exp_stderr)) {
214 $stderr_cmd = " 2>$TMP_STDERR";
216 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
217 my $ret_val = $?;
218 if (defined($Exp_stderr)) {
219 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
220 unlink($TMP_STDERR);
221 } else {
222 diag("Warning: stderr not defined for '$Txt'");
224 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
226 return $retval;
227 # }}}
228 } # testcmd()
230 sub likecmd {
231 # {{{
232 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
233 defined($descriptions{$Desc}) &&
234 BAIL_OUT("likecmd(): '$Desc' description is used twice");
235 $descriptions{$Desc} = 1;
236 my $stderr_cmd = '';
237 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
238 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
239 my $TMP_STDERR = "$CMDB-stderr.tmp";
240 my $retval = 1;
242 if (defined($Exp_stderr)) {
243 $stderr_cmd = " 2>$TMP_STDERR";
245 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
246 my $ret_val = $?;
247 if (defined($Exp_stderr)) {
248 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
249 unlink($TMP_STDERR);
250 } else {
251 diag("Warning: stderr not defined for '$Txt'");
253 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
255 return $retval;
256 # }}}
257 } # likecmd()
259 sub file_data {
260 # Return file content as a string {{{
261 my $File = shift;
262 my $Txt;
264 open(my $fp, '<', $File) or return undef;
265 local $/ = undef;
266 $Txt = <$fp>;
267 close($fp);
268 return $Txt;
269 # }}}
270 } # file_data()
272 sub print_version {
273 # Print program version {{{
274 print("$progname $VERSION\n");
275 return;
276 # }}}
277 } # print_version()
279 sub usage {
280 # Send the help message to stdout {{{
281 my $Retval = shift;
283 if ($Opt{'verbose'}) {
284 print("\n");
285 print_version();
287 print(<<"END");
289 Usage: $progname [options]
291 Contains tests for the $CMDB(1) program.
293 Options:
295 -a, --all
296 Run all tests, also TODOs.
297 -h, --help
298 Show this help.
299 -q, --quiet
300 Be more quiet. Can be repeated to increase silence.
301 -t, --todo
302 Run only the TODO tests.
303 -v, --verbose
304 Increase level of verbosity. Can be repeated.
305 --version
306 Print version information.
309 exit($Retval);
310 # }}}
311 } # usage()
313 sub msg {
314 # Print a status message to stderr based on verbosity level {{{
315 my ($verbose_level, $Txt) = @_;
317 $verbose_level > $Opt{'verbose'} && return;
318 print(STDERR "$progname: $Txt\n");
319 return;
320 # }}}
321 } # msg()
323 __END__
325 # This program is free software; you can redistribute it and/or modify
326 # it under the terms of the GNU General Public License as published by
327 # the Free Software Foundation; either version 2 of the License, or (at
328 # your option) any later version.
330 # This program is distributed in the hope that it will be useful, but
331 # WITHOUT ANY WARRANTY; without even the implied warranty of
332 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
333 # See the GNU General Public License for more details.
335 # You should have received a copy of the GNU General Public License
336 # along with this program.
337 # If not, see L<http://www.gnu.org/licenses/>.
339 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :