3 #=======================================================================
5 # File ID: 6c8dbc38-3b85-11e5-9db6-000df06acc56
7 # Test suite for postgres(1).
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 #=======================================================================
19 if (`createdb --version 2>/dev/null` !~ /PostgreSQL/) {
20 plan skip_all
=> "Postgres is not installed here";
34 our $CMD = "../$CMDB";
48 $progname =~ s/^.*\/(.*?)$/$1/;
49 our $VERSION = '0.0.0';
51 my %descriptions = ();
53 Getopt
::Long
::Configure
('bundling');
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'}) {
72 my $tmpdb = "tmp-postgres-t-$$-" . substr(rand, 2, 8);
73 my $tmp_stdout = '.tmp-postgres-t-stdout.tmp';
74 my $tmp_stderr = '.tmp-postgres-t-stderr.tmp';
82 diag
(sprintf('========== Executing %s v%s ==========',
83 $progname, $VERSION));
85 if ($Opt{'todo'} && !$Opt{'all'}) {
91 testcmd("$CMD command", # {{{
104 testcmd
("createdb \"$tmpdb\"", # {{{
108 'Create temporary database',
109 ) || BAIL_OUT
("Cannot create temporary database, " .
110 "not much point in going on, then");
113 diag
('Make sure the db sorting uses C locale');
114 psql_cmd
($tmpdb, # {{{
116 CREATE TABLE t (s varchar);
152 'Insert unsorted text into db',
156 psql_cmd
($tmpdb, # {{{
157 'COPY (SELECT * FROM t ORDER BY s) TO stdout;',
188 'Text sorting follows the Unicode table',
192 diag
('Cleaning up...');
193 ok
(unlink($tmp_stdout), 'Delete stdout tmpfile');
194 ok
(unlink($tmp_stderr), 'Delete stderr tmpfile');
195 testcmd
("dropdb \"$tmpdb\"", # {{{
199 'Drop temporary database',
207 if ($Opt{'all'} || $Opt{'todo'}) {
208 diag
('Running TODO tests...'); # {{{
213 # Insert TODO tests here.
219 diag
('Testing finished.');
226 my ($db, $sql, $exp_stdout, $exp_stderr, $desc) = @_;
227 ok
(open(my $dbpipe, "| psql -X -d \"$tmpdb\" >$tmp_stdout 2>$tmp_stderr"),
228 "Open db pipe ($desc)");
229 ok
(print($dbpipe $sql), "Print to pipe ($desc)");
230 ok
(close($dbpipe), "Close db pipe ($desc)");
231 like
(file_data
($tmp_stdout), $exp_stdout, "$desc (stdout)");
232 like
(file_data
($tmp_stderr), $exp_stderr, "$desc (stderr)");
239 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
240 defined($descriptions{$Desc}) &&
241 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
242 $descriptions{$Desc} = 1;
244 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
245 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
246 my $TMP_STDERR = "$CMDB-stderr.tmp";
249 if (defined($Exp_stderr)) {
250 $stderr_cmd = " 2>$TMP_STDERR";
252 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
254 if (defined($Exp_stderr)) {
255 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
258 diag
("Warning: stderr not defined for '$Txt'");
260 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
268 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
269 defined($descriptions{$Desc}) &&
270 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
271 $descriptions{$Desc} = 1;
273 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
274 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
275 my $TMP_STDERR = "$CMDB-stderr.tmp";
278 if (defined($Exp_stderr)) {
279 $stderr_cmd = " 2>$TMP_STDERR";
281 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
283 if (defined($Exp_stderr)) {
284 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
287 diag
("Warning: stderr not defined for '$Txt'");
289 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
296 # Return file content as a string {{{
300 open(my $fp, '<', $File) or return undef;
309 # Print program version {{{
310 print("$progname $VERSION\n");
316 # Send the help message to stdout {{{
319 if ($Opt{'verbose'}) {
325 Usage: $progname [options]
327 Contains tests for the $CMDB(1) program.
332 Run all tests, also TODOs.
336 Be more quiet. Can be repeated to increase silence.
338 Run only the TODO tests.
340 Increase level of verbosity. Can be repeated.
342 Print version information.
350 # Print a status message to stderr based on verbosity level {{{
351 my ($verbose_level, $Txt) = @_;
353 $verbose_level > $Opt{'verbose'} && return;
354 print(STDERR
"$progname: $Txt\n");
361 # This program is free software; you can redistribute it and/or modify
362 # it under the terms of the GNU General Public License as published by
363 # the Free Software Foundation; either version 2 of the License, or (at
364 # your option) any later version.
366 # This program is distributed in the hope that it will be useful, but
367 # WITHOUT ANY WARRANTY; without even the implied warranty of
368 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
369 # See the GNU General Public License for more details.
371 # You should have received a copy of the GNU General Public License
372 # along with this program.
373 # If not, see L<http://www.gnu.org/licenses/>.
375 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :