3 #=======================================================================
5 # File ID: b5f7d80e-70ff-11e5-96fa-fefdb24f8e10
7 # Test suite for sort-sqlite(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 use Test
::More
qw{no_plan
};
28 our $CMDB = "sort-sqlite";
29 our $CMD = "../$CMDB";
30 our $SQLITE = "sqlite3";
44 $progname =~ s/^.*\/(.*?)$/$1/;
45 our $VERSION = '0.0.0';
47 my %descriptions = ();
49 Getopt
::Long
::Configure
('bundling');
52 'all|a' => \
$Opt{'all'},
53 'help|h' => \
$Opt{'help'},
54 'quiet|q+' => \
$Opt{'quiet'},
55 'todo|t' => \
$Opt{'todo'},
56 'verbose|v+' => \
$Opt{'verbose'},
57 'version' => \
$Opt{'version'},
59 ) || die("$progname: Option error. Use -h for help.\n");
61 $Opt{'verbose'} -= $Opt{'quiet'};
62 $Opt{'help'} && usage
(0);
63 if ($Opt{'version'}) {
76 diag
(sprintf('========== Executing %s v%s ==========',
77 $progname, $VERSION));
79 if ($Opt{'todo'} && !$Opt{'all'}) {
85 testcmd("$CMD command", # {{{
98 diag
('Testing -h (--help) option...');
99 likecmd
("$CMD -h", # {{{
100 '/ Show this help/i',
103 'Option -h prints help screen',
107 diag
('Testing -v (--verbose) option...');
108 likecmd
("$CMD -hv", # {{{
109 '/^\n\S+ \d+\.\d+\.\d+/s',
112 'Option -v with -h returns version number and help screen',
116 diag
('Testing --version option...');
117 likecmd
("$CMD --version", # {{{
118 '/^\S+ \d+\.\d+\.\d+/',
121 'Option --version returns version number',
125 ok
(chdir("sort-sqlite-files"), "chdir sort-sqlite-files");
127 testcmd
("tar xzf sqlite-dbs.tar.gz", # {{{
131 "Untar sqlite-dbs.tar.gz",
135 ok
(chdir("sqlite-dbs"), "chdir sqlite-dbs");
137 testcmd
("$CMD -c abc.def non-existing.sqlite", # {{{
139 "sort-sqlite: non-existing.sqlite: File is not readable by you or is not a regular file\n",
141 "Try to open a non-existing file",
145 testcmd
("$CMD unsorted1.sqlite", # {{{
147 "sort-sqlite: Missing -c/--column option\n",
149 "Missing -c/--column option",
153 likecmd
("$CMD -c non.existing unsorted1.sqlite", # {{{
155 '/^.*sort-sqlite: unsorted1.sqlite: sqlite3 error, aborting\n$/s',
157 "Try to sort unsorted1.sqlite with unknown table and column",
161 is
(sqlite_dump
("unsorted1.sqlite"), # {{{
171 "unsorted1.sqlite is not modified",
175 testcmd
("$CMD -c t.a unsorted1.sqlite", # {{{
179 "Sort unsorted1.sqlite",
183 is
(sqlite_dump
("unsorted1.sqlite"), # {{{
193 "unsorted1.sqlite looks ok",
197 ok
(-f
"unsorted1.sqlite.20151012T164244Z.bck", "Backup file 1 exists");
198 testcmd
("$CMD -v --column t.a -c u.a unsorted2.sqlite " .
199 "unsorted3.sqlite", # {{{
201 "sort-sqlite: Sorting unsorted2.sqlite\n" .
202 "sort-sqlite: Sorting unsorted3.sqlite\n",
204 "Sort several tables in unsorted2.sqlite",
208 is
(sqlite_dump
("unsorted2.sqlite"), # {{{
226 "unsorted2.sqlite looks ok",
230 is
(sqlite_dump
("unsorted3.sqlite"), # {{{
252 "unsorted3.sqlite looks ok",
256 testcmd
("$CMD -c t.a unsorted4.sqlite --column u.a", # {{{
260 "Sort unsorted4.sqlite, entries have several lines",
264 ok
(-f
"unsorted4.sqlite.20161103T235439Z.bck", "Backup file 4 exists");
265 is
(sqlite_dump
("unsorted4.sqlite"), # {{{
273 t|\n\nanother\n\nmulti\nline\n
283 "unsorted4.sqlite looks ok",
287 ok
(-f
"unsorted2.sqlite.20151012T164437Z.bck", "Backup file 2 exists");
288 ok
(-f
"unsorted3.sqlite.20151012T181141Z.bck", "Backup file 3 exists");
289 ok
(unlink("unsorted1.sqlite"), "Delete unsorted1.sqlite");
290 ok
(unlink("unsorted2.sqlite"), "Delete unsorted2.sqlite");
291 ok
(unlink("unsorted3.sqlite"), "Delete unsorted3.sqlite");
292 ok
(unlink("unsorted4.sqlite"), "Delete unsorted4.sqlite");
293 ok
(unlink("unsorted1.sqlite.20151012T164244Z.bck"), "Delete backup 1");
294 ok
(unlink("unsorted2.sqlite.20151012T164437Z.bck"), "Delete backup 2");
295 ok
(unlink("unsorted3.sqlite.20151012T181141Z.bck"), "Delete backup 3");
296 ok
(unlink("unsorted4.sqlite.20161103T235439Z.bck"), "Delete backup 4");
297 ok
(chdir(".."), "chdir ..");
298 ok
(rmdir("sqlite-dbs"), "rmdir sqlite-dbs");
303 if ($Opt{'all'} || $Opt{'todo'}) {
304 diag
('Running TODO tests...'); # {{{
309 # Insert TODO tests here.
315 diag
('Testing finished.');
325 msg
(5, "sql(): db = '$db'");
326 local(*CHLD_IN
, *CHLD_OUT
, *CHLD_ERR
);
329 my $pid = open3
(*CHLD_IN
, *CHLD_OUT
, *CHLD_ERR
, $SQLITE, $db) or (
331 msg
(0, "sql(): open3() error: $!"),
332 return("sql() error"),
334 msg
(5, "sql(): sql = '$sql'");
335 print(CHLD_IN
"$sql\n") or msg
(0, "sql(): print CHLD_IN error: $!");
337 @retval = <CHLD_OUT
>;
338 msg
(5, "sql(): retval = '" . join('|', @retval) . "'");
339 my @child_stderr = <CHLD_ERR
>;
340 if (scalar(@child_stderr)) {
341 msg
(1, "$SQLITE error: " . join('', @child_stderr));
344 return(join('', @retval));
349 # Return contents of database file {{{
352 return sql
($File, <<END);
355 SELECT 'one', * FROM one;
356 SELECT 't', * FROM t;
357 SELECT 'u', * FROM u;
364 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
365 defined($descriptions{$Desc}) &&
366 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
367 $descriptions{$Desc} = 1;
369 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
370 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
371 my $TMP_STDERR = "$CMDB-stderr.tmp";
374 if (defined($Exp_stderr)) {
375 $stderr_cmd = " 2>$TMP_STDERR";
377 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
379 if (defined($Exp_stderr)) {
380 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
383 diag
("Warning: stderr not defined for '$Txt'");
385 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
393 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
394 defined($descriptions{$Desc}) &&
395 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
396 $descriptions{$Desc} = 1;
398 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
399 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
400 my $TMP_STDERR = "$CMDB-stderr.tmp";
403 if (defined($Exp_stderr)) {
404 $stderr_cmd = " 2>$TMP_STDERR";
406 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
408 if (defined($Exp_stderr)) {
409 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
412 diag
("Warning: stderr not defined for '$Txt'");
414 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
421 # Return file content as a string {{{
425 open(my $fp, '<', $File) or return undef;
434 # Print program version {{{
435 print("$progname $VERSION\n");
441 # Send the help message to stdout {{{
444 if ($Opt{'verbose'}) {
450 Usage: $progname [options]
452 Contains tests for the $CMDB(1) program.
457 Run all tests, also TODOs.
461 Be more quiet. Can be repeated to increase silence.
463 Run only the TODO tests.
465 Increase level of verbosity. Can be repeated.
467 Print version information.
475 # Print a status message to stderr based on verbosity level {{{
476 my ($verbose_level, $Txt) = @_;
478 $verbose_level > $Opt{'verbose'} && return;
479 print(STDERR
"$progname: $Txt\n");
486 # This program is free software; you can redistribute it and/or modify
487 # it under the terms of the GNU General Public License as published by
488 # the Free Software Foundation; either version 2 of the License, or (at
489 # your option) any later version.
491 # This program is distributed in the hope that it will be useful, but
492 # WITHOUT ANY WARRANTY; without even the implied warranty of
493 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
494 # See the GNU General Public License for more details.
496 # You should have received a copy of the GNU General Public License
497 # along with this program.
498 # If not, see L<http://www.gnu.org/licenses/>.
500 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :