Improve test-harnesses and their associated scripts.
[cql-java-moved-to-github.git] / test / mkanswers
1 #!/usr/bin/perl -w
2
3 use IO::File;
4 use strict;
5
6 if (@ARGV == 0) {
7     print STDERR "Usage: $0 <trusted-CQL-compiler>\n";
8     exit(1);
9 }
10 my $compiler = $ARGV[0];
11
12 while (<sections/*>) {
13     my $sdir = $_;
14     s@sections/@@;
15     print "answering section $_ - ", read_file("$sdir/name"), "\n";
16
17     while (<$sdir/*>) {
18         next if /\/name$/;
19         my $qfile = $_;
20         s@sections/([0-9]+/.*)\.cql@$1@;
21         my $query = read_file($qfile);
22         my $afile = $qfile;
23         $afile =~ s/\.cql$/.cxql/;
24         print "  query $_ - $query\n";
25         my $fh = new IO::File("| $compiler > $afile")
26             or die "can't run compiler '$compiler': $!";
27         print $fh $query;
28         $fh->close();
29     }
30 }
31
32 sub read_file {
33     my($name) = @_;
34
35     my $fh = new IO::File("<$name")
36         or die "can't read '$name': $!";
37     my $contents = join('', <$fh>);
38     $fh->close();
39     return $contents;
40 }
41
42 sub write_file {
43     my($name, $contents) = @_;
44
45     my $fh = new IO::File(">$name")
46         or die "can't create '$name': $!";
47     $fh->print($contents);
48     $fh->close();
49 }