ly/: Create one .ly file for every .ily instrument file
[sunny256-utils.git] / tests / std.t
bloba6addc39a9a3b4d09f2d0d85db395f24fa1b228c
1 #!/usr/bin/env perl
3 #=======================================================================
4 # std.t
5 # File ID: 685626fa-f988-11dd-af37-000475e441b9
7 # Test suite for std(1).
9 # Character set: UTF-8
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 #=======================================================================
15 use strict;
16 use warnings;
18 BEGIN {
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
24 use Cwd;
25 use IPC::Open3;
27 local $| = 1;
29 our $CMDB = "std";
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}";
34 our %Opt = (
36 'all' => 0,
37 'help' => 0,
38 'quiet' => 0,
39 'todo' => 0,
40 'verbose' => 0,
41 'version' => 0,
45 our $progname = $0;
46 $progname =~ s/^.*\/(.*?)$/$1/;
47 our $VERSION = '0.0.0';
49 my %descriptions = ();
51 Getopt::Long::Configure('bundling');
52 GetOptions(
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'}) {
66 print_version();
67 exit(0);
70 exit(main());
72 sub main {
73 # {{{
74 my $Retval = 0;
75 my $re_create_tables =
76 'CREATE TABLE synced \(\n' .
77 ' file TEXT\n' .
78 ' CONSTRAINT synced_file_length\n' .
79 ' CHECK \(length\(file\) > 0\)\n' .
80 ' UNIQUE\n' .
81 ' NOT NULL\n' .
82 ' ,\n' .
83 ' orig TEXT\n' .
84 ' ,\n' .
85 ' rev TEXT\n' .
86 ' CONSTRAINT synced_rev_length\n' .
87 ' CHECK \(length\(rev\) = 40 OR rev = \'\'\)\n' .
88 ' ,\n' .
89 ' date TEXT\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' .
94 '\);\n' .
95 'CREATE TABLE todo \(\n' .
96 ' file TEXT\n' .
97 ' CONSTRAINT todo_file_length\n' .
98 ' CHECK\(length\(file\) > 0\)\n' .
99 ' UNIQUE\n' .
100 ' NOT NULL\n' .
101 ' ,\n' .
102 ' pri INTEGER\n' .
103 ' CONSTRAINT todo_pri_range\n' .
104 ' CHECK\(pri BETWEEN 1 AND 5\)\n' .
105 ' ,\n' .
106 ' comment TEXT\n' .
107 '\);\n';
109 diag(sprintf('========== Executing %s v%s ==========',
110 $progname, $VERSION));
112 if ($Opt{'todo'} && !$Opt{'all'}) {
113 goto todo_section;
116 =pod
118 testcmd("$CMD command", # {{{
119 <<'END',
120 [expected stdout]
124 'description',
127 # }}}
129 =cut
131 diag('Testing -h (--help) option...');
132 likecmd("$CMD -h", # {{{
133 '/ Show this help/i',
134 '/^$/',
136 'Option -h prints help screen',
139 # }}}
140 diag('Testing -v (--verbose) option...');
141 likecmd("$CMD -hv", # {{{
142 '/^\n\S+ \d+\.\d+\.\d+/s',
143 '/^$/',
145 'Option -v with -h returns version number and help screen',
148 # }}}
149 diag('Testing --version option...');
150 likecmd("$CMD --version", # {{{
151 '/^\S+ \d+\.\d+\.\d+/',
152 '/^$/',
154 'Option --version returns version number',
157 # }}}
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...");
163 mkdir($Tmptop) ||
164 die("$progname: $Tmptop: Cannot create directory: $!\n");
165 chdir($Tmptop) || die("$progname: $Tmptop: Cannot chdir(): $!");
166 mkdir("tmpuuids") ||
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",
175 # }}}
176 my $suuid_file = glob("tmpuuids/*");
177 ok(-e $suuid_file, "suuid log file exists");
178 likecmd("SUUID_LOGDIR=tmpuuids ../$CMD bash " .
179 "bashfile", # {{{
180 "/^$v1_templ\\n\$/s",
181 '/^$/',
183 "Create bash script",
185 # }}}
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",
192 '/^$/',
194 "Create std.h, uses STDU",
196 # }}}
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",
208 # }}}
209 diag("Testing -f (--force) option...");
210 likecmd("../$CMD bash bashfile", # {{{
211 '/^$/s',
212 '/^' .
213 'std: bashfile: File already exists, will not overwrite\n' .
214 '$/s',
216 "Create bash script, file already exists, don’t use --force",
219 # }}}
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",
227 # }}}
228 like(file_data("bashfile"), # {{{
229 qr/use Getopt::Long/s,
230 "Contents of bashfile is replaced"
233 # }}}
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",
242 # }}}
243 likecmd("SUUID_LOGDIR=tmpuuids ../$CMD -t exec=fname " . # {{{
244 "-T exec c/src/std.h",
245 '/STDUexecUDTS/s',
246 '/^std: Warning: Undefined tags: exec filename\n.*$/s',
248 "Send std.h to stdout, don’t expand uppercase tag",
251 # }}}
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",
258 '/^' .
259 '$/',
261 "Read dbname from stdrc with --rcfile",
264 # }}}
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])");
276 todo_section:
279 if ($Opt{'all'} || $Opt{'todo'}) {
280 diag('Running TODO tests...'); # {{{
282 TODO: {
284 local $TODO = '';
285 # Insert TODO tests here.
288 # TODO tests }}}
291 diag('Testing finished.');
292 return $Retval;
293 # }}}
294 } # main()
296 sub testcmd {
297 # {{{
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;
302 my $stderr_cmd = '';
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";
306 my $retval = 1;
308 if (defined($Exp_stderr)) {
309 $stderr_cmd = " 2>$TMP_STDERR";
311 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
312 my $ret_val = $?;
313 if (defined($Exp_stderr)) {
314 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
315 unlink($TMP_STDERR);
316 } else {
317 diag("Warning: stderr not defined for '$Txt'");
319 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
321 return $retval;
322 # }}}
323 } # testcmd()
325 sub likecmd {
326 # {{{
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;
331 my $stderr_cmd = '';
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";
335 my $retval = 1;
337 if (defined($Exp_stderr)) {
338 $stderr_cmd = " 2>$TMP_STDERR";
340 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
341 my $ret_val = $?;
342 if (defined($Exp_stderr)) {
343 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
344 unlink($TMP_STDERR);
345 } else {
346 diag("Warning: stderr not defined for '$Txt'");
348 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
350 return $retval;
351 # }}}
352 } # likecmd()
354 sub file_data {
355 # Return file content as a string {{{
356 my $File = shift;
357 my $Txt;
359 open(my $fp, '<', $File) or return undef;
360 local $/ = undef;
361 $Txt = <$fp>;
362 close($fp);
363 return $Txt;
364 # }}}
365 } # file_data()
367 sub create_file {
368 # Create new file and fill it with data {{{
369 my ($file, $text) = @_;
370 my $retval = 0;
371 if (open(my $fp, ">$file")) {
372 print($fp $text);
373 close($fp);
374 $retval = is(
375 file_data($file),
376 $text,
377 "$file was successfully created",
380 return($retval); # 0 if error, 1 if ok
381 # }}}
382 } # create_file()
384 sub print_version {
385 # Print program version {{{
386 print("$progname $VERSION\n");
387 return;
388 # }}}
389 } # print_version()
391 sub usage {
392 # Send the help message to stdout {{{
393 my $Retval = shift;
395 if ($Opt{'verbose'}) {
396 print("\n");
397 print_version();
399 print(<<"END");
401 Usage: $progname [options]
403 Contains tests for the $CMDB(1) program.
405 Options:
407 -a, --all
408 Run all tests, also TODOs.
409 -h, --help
410 Show this help.
411 -q, --quiet
412 Be more quiet. Can be repeated to increase silence.
413 -t, --todo
414 Run only the TODO tests.
415 -v, --verbose
416 Increase level of verbosity. Can be repeated.
417 --version
418 Print version information.
421 exit($Retval);
422 # }}}
423 } # usage()
425 sub msg {
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");
431 return;
432 # }}}
433 } # msg()
435 __END__
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 :