3 #=======================================================================
5 # File ID: 685626fa-f988-11dd-af37-000475e441b9
7 # Test suite for std(1).
10 # ©opyleft 2008– Ø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
};
30 our $CMD = "../$CMDB";
31 my $Lh = "[0-9a-fA-F]";
32 my $v1_templ = "$Lh\{8}-$Lh\{4}-1$Lh\{3}-$Lh\{4}-$Lh\{12}";
46 $progname =~ s/^.*\/(.*?)$/$1/;
47 our $VERSION = '0.0.0';
49 my %descriptions = ();
51 Getopt
::Long
::Configure
('bundling');
54 'all|a' => \
$Opt{'all'},
55 'help|h' => \
$Opt{'help'},
56 'quiet|q+' => \
$Opt{'quiet'},
57 'todo|t' => \
$Opt{'todo'},
58 'verbose|v+' => \
$Opt{'verbose'},
59 'version' => \
$Opt{'version'},
61 ) || die("$progname: Option error. Use -h for help.\n");
63 $Opt{'verbose'} -= $Opt{'quiet'};
64 $Opt{'help'} && usage
(0);
65 if ($Opt{'version'}) {
75 my $re_create_tables =
76 'CREATE TABLE synced \(\n' .
78 ' CONSTRAINT synced_file_length\n' .
79 ' CHECK \(length\(file\) > 0\)\n' .
86 ' CONSTRAINT synced_rev_length\n' .
87 ' CHECK \(length\(rev\) = 40 OR rev = \'\'\)\n' .
90 ' CONSTRAINT synced_date_length\n' .
91 ' CHECK \(date IS NULL OR length\(date\) = 19\)\n' .
92 ' CONSTRAINT synced_date_valid\n' .
93 ' CHECK \(date IS NULL OR datetime\(date\) IS NOT NULL\)\n' .
95 'CREATE TABLE todo \(\n' .
97 ' CONSTRAINT todo_file_length\n' .
98 ' CHECK\(length\(file\) > 0\)\n' .
103 ' CONSTRAINT todo_pri_range\n' .
104 ' CHECK\(pri BETWEEN 1 AND 5\)\n' .
109 diag
(sprintf('========== Executing %s v%s ==========',
110 $progname, $VERSION));
112 if ($Opt{'todo'} && !$Opt{'all'}) {
118 testcmd("$CMD command", # {{{
131 diag
('Testing -h (--help) option...');
132 likecmd
("$CMD -h", # {{{
133 '/ Show this help/i',
136 'Option -h prints help screen',
140 diag
('Testing -v (--verbose) option...');
141 likecmd
("$CMD -hv", # {{{
142 '/^\n\S+ \d+\.\d+\.\d+/s',
145 'Option -v with -h returns version number and help screen',
149 diag
('Testing --version option...');
150 likecmd
("$CMD --version", # {{{
151 '/^\S+ \d+\.\d+\.\d+/',
154 'Option --version returns version number',
159 # FIXME: Create Git repostory to test things properly, for example
160 # that --dbname=none doesn't create a database.
161 my $Tmptop = "tmp-std-t-$$-" . substr(rand, 2, 8);
162 diag
("Creating tempdir...");
164 die("$progname: $Tmptop: Cannot create directory: $!\n");
165 chdir($Tmptop) || die("$progname: $Tmptop: Cannot chdir(): $!");
167 die("$progname: $Tmptop/tmpuuids: Cannot mkdir(): $!");
168 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD bash", # {{{
169 '/GNU General Public License/s',
170 '/^std: Warning: Undefined tags: filename\n$/s',
172 "One argument sends file to stdout",
176 my $suuid_file = glob("tmpuuids/*");
177 ok
(-e
$suuid_file, "suuid log file exists");
178 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD bash " .
180 "/^$v1_templ\\n\$/s",
183 "Create bash script",
186 ok
(-e
"bashfile", "bashfile exists");
187 ok
(-x
"bashfile", "bashfile is executable");
188 diag
("Test uppercase tags...");
189 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD -t exec=fname " .
190 "c/src/std.h std.h", # {{{
191 "/^$v1_templ\\n\$/s",
194 "Create std.h, uses STDU",
197 ok
(-e
"std.h", "std.h exists");
198 like
(file_data
("std.h"), qr/ifndef _FNAME_H/,
199 "Tags in std.h are upper case");
200 diag
("Check for unused tags...");
201 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD perl-tests", # {{{
202 '/^.*Contains tests for the.*$/s',
203 '/^std: Warning: Undefined tags: filename progname\n$/s',
205 "Report unused tags",
209 diag
("Testing -f (--force) option...");
210 likecmd
("../$CMD bash bashfile", # {{{
213 'std: bashfile: File already exists, will not overwrite\n' .
216 "Create bash script, file already exists, don’t use --force",
220 likecmd
("LC_ALL=C SUUID_LOGDIR=tmpuuids ../$CMD -fv " .
221 "perl bashfile", # {{{
222 "/^$v1_templ\\n\$/s",
223 '/^std: Overwriting \'bashfile\'\.\.\.\n/s',
225 "Overwrite bashfile with perl script using --force",
228 like
(file_data
("bashfile"), # {{{
229 qr/use Getopt::Long/s,
230 "Contents of bashfile is replaced"
234 diag
("Testing -T (--notag) option...");
235 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD -T uuid,year perl", # {{{
236 '/STDuuidDTS.*STDyearDTS/s',
237 '/^std: Warning: Undefined tags: filename uuid year\n.*$/s',
239 "Send perl script to stdout, don’t expand uuid and year tag",
243 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD -t exec=fname " . # {{{
244 "-T exec c/src/std.h",
246 '/^std: Warning: Undefined tags: exec filename\n.*$/s',
248 "Send std.h to stdout, don’t expand uppercase tag",
252 diag
("Test --rcfile option...");
253 create_file
("stdrc", <<END);
255 likecmd
("SUUID_LOGDIR=tmpuuids ../$CMD --force --rcfile stdrc " .
256 "bash bashfile", # {{{
257 "/^$v1_templ\\n\$/s",
261 "Read dbname from stdrc with --rcfile",
266 chdir("..") || die("$progname: Cannot 'chdir ..': $!");
267 diag
("Cleaning up temp files...");
268 ok
(unlink(glob "$Tmptop/tmpuuids/*"),
269 "unlink('glob [Tmptop]/tmpuuids/*')");
270 ok
(rmdir("$Tmptop/tmpuuids"), "rmdir([Tmptop]/tmpuuids)");
271 ok
(unlink("$Tmptop/bashfile"), "unlink('[Tmptop]/bashfile')");
272 ok
(unlink("$Tmptop/std.h"), "unlink('[Tmptop]/std.h')");
273 ok
(unlink("$Tmptop/stdrc"), "unlink('[Tmptop]/stdrc");
274 ok
(rmdir($Tmptop), "rmdir([Tmptop])");
279 if ($Opt{'all'} || $Opt{'todo'}) {
280 diag
('Running TODO tests...'); # {{{
285 # Insert TODO tests here.
291 diag
('Testing finished.');
298 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
299 defined($descriptions{$Desc}) &&
300 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
301 $descriptions{$Desc} = 1;
303 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
304 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
305 my $TMP_STDERR = "$CMDB-stderr.tmp";
308 if (defined($Exp_stderr)) {
309 $stderr_cmd = " 2>$TMP_STDERR";
311 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
313 if (defined($Exp_stderr)) {
314 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
317 diag
("Warning: stderr not defined for '$Txt'");
319 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
327 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
328 defined($descriptions{$Desc}) &&
329 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
330 $descriptions{$Desc} = 1;
332 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
333 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
334 my $TMP_STDERR = "$CMDB-stderr.tmp";
337 if (defined($Exp_stderr)) {
338 $stderr_cmd = " 2>$TMP_STDERR";
340 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
342 if (defined($Exp_stderr)) {
343 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
346 diag
("Warning: stderr not defined for '$Txt'");
348 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
355 # Return file content as a string {{{
359 open(my $fp, '<', $File) or return undef;
368 # Create new file and fill it with data {{{
369 my ($file, $text) = @_;
371 if (open(my $fp, ">$file")) {
377 "$file was successfully created",
380 return($retval); # 0 if error, 1 if ok
385 # Print program version {{{
386 print("$progname $VERSION\n");
392 # Send the help message to stdout {{{
395 if ($Opt{'verbose'}) {
401 Usage: $progname [options]
403 Contains tests for the $CMDB(1) program.
408 Run all tests, also TODOs.
412 Be more quiet. Can be repeated to increase silence.
414 Run only the TODO tests.
416 Increase level of verbosity. Can be repeated.
418 Print version information.
426 # Print a status message to stderr based on verbosity level {{{
427 my ($verbose_level, $Txt) = @_;
429 $verbose_level > $Opt{'verbose'} && return;
430 print(STDERR
"$progname: $Txt\n");
437 # This program is free software; you can redistribute it and/or modify
438 # it under the terms of the GNU General Public License as published by
439 # the Free Software Foundation; either version 2 of the License, or (at
440 # your option) any later version.
442 # This program is distributed in the hope that it will be useful, but
443 # WITHOUT ANY WARRANTY; without even the implied warranty of
444 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
445 # See the GNU General Public License for more details.
447 # You should have received a copy of the GNU General Public License
448 # along with this program.
449 # If not, see L<http://www.gnu.org/licenses/>.
451 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :