summaryrefslogtreecommitdiff
path: root/tests/ls/mk-script.pl
blob: 0a8778512494a21042d748c1a96b4d9df8b74644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! @PERL@ -w
# -*- perl -*-
# @configure_input@

require 5.002;
use strict;
use POSIX qw (assert);

BEGIN { push @INC, '@srcdir@' if '@srcdir@' ne '.'; }
use Test;

sub spec_to_list ($$$)
{
  my ($spec, $test_name, $type) = @_;

  assert ($type eq 'in' || $type eq 'exp');

  my @all_file;
  my @gen_file;
  my @content_string;
  if (ref $spec)
    {
      assert (ref $spec eq 'ARRAY');
      my $file_spec;
      foreach $file_spec (@$spec)
	{
	  # A file spec may be a string or a reference.
	  # If it's a string, that string is to be the contents of a
	  # generated (by this script) file with name derived from the
	  # name of this test.
	  # If it's a reference, then it must be the name of an existing
	  # file.
	  if (ref $file_spec)
	    {
	      assert (ref $file_spec eq 'SCALAR');
	      my $existing_file = $$file_spec;
	      # FIXME: make sure $existing_file exists somewhere.
	      push (@all_file, $existing_file);
	    }
	  else
	    {
	      push (@content_string, $file_spec);
	    }
	}
    }
  else
    {
      push (@content_string, $spec);
    }

  my $i = 1;
  my $file_contents;
  foreach $file_contents (@content_string)
    {
      my $suffix = (@content_string > 1 ? $i : '');
      my $gen_file = "t$test_name.$type$suffix";
      push (@all_file, $gen_file);
      push (@gen_file, $gen_file);
      open (F, ">$gen_file") || die "$0: $gen_file: $!\n";
      print F $file_contents;
      close (F) || die "$0: $gen_file: $!\n";
      ++$i;
    }

  # GEN_FILES is currently unused.  FIXME
  my %h = (
    ALL_FILES => \@all_file,
    GEN_FILES => \@gen_file
  );

  return \%h;
}

$| = 1;

my $xx = $ARGV[0];

print <<EOF;
#! /bin/sh
# This script was generated automatically by build-script.
case \$# in
  0) xx='$xx';;
  *) xx="\$1";;
esac
test "\$VERBOSE" && echo=echo || echo=:
\$echo testing program: \$xx
errors=0
test "\$srcdir" || srcdir=.
test "\$VERBOSE" && \$xx --version 2> /dev/null
EOF

{
  my %seen;

  my $test_vector;

  foreach $test_vector (@Test::t)
    {
      my ($test_name, $flags, $in_spec, $expected, $e_ret_code, $rest) =
	@{$test_vector};
      assert (defined $e_ret_code && !defined $rest);
      assert (!ref $test_name);
      assert (!ref $flags);
      assert (!ref $expected);
      assert (!ref $e_ret_code);

      die "$0: $.: duplicate test name \`$test_name'\n"
	if (defined $seen{$test_name});
      $seen{$test_name} = 1;
    }
}

my $test_vector;
foreach $test_vector (@Test::t)
  {
    my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
	= @{$test_vector};

    my $in = spec_to_list ($in_spec, $test_name, 'in');

    my @srcdir_rel_in_file;
    my $f;
    foreach $f (@{ $in->{ALL_FILES} })
      {
	push (@srcdir_rel_in_file, "\$srcdir/$f")
      }

    my $Exp = spec_to_list ($exp_spec, $test_name, 'exp');
    assert (@{ $Exp->{ALL_FILES} } == 1);
    my $exp_name = "\$srcdir/$Exp->{ALL_FILES}->[0]";
    my $out = "t$test_name.out";
    my $err_output = "t$test_name.err";

    my $cmd = "\$xx $flags " . join (' ', @srcdir_rel_in_file)
      . " > $out 2> $err_output";
    print <<EOF ;
$cmd
code=\$?
if test \$code != $e_ret_code ; then
  \$echo Test $test_name failed: $xx return code \$code differs from expected value $e_ret_code 1>&2
  errors=`expr \$errors + 1`
else
  cmp $out $exp_name
  case \$? in
    0) if test "\$VERBOSE" ; then \$echo passed $test_name; fi ;; # equal files
    1) \$echo Test $test_name failed: files $out and $exp_name differ 1>&2;
       errors=`expr \$errors + 1` ;;
    2) \$echo Test $test_name may have failed. 1>&2;
       \$echo The command \"cmp $out $exp_name\" failed. 1>&2 ;
       errors=`expr \$errors + 1` ;;
  esac
fi
test -s $err_output || rm -f $err_output
EOF
  }
print <<EOF2 ;
if test \$errors = 0 ; then
  \$echo Passed all tests. 1>&2
else
  \$echo Failed \$errors tests. 1>&2
fi
test \$errors = 0 || errors=1
exit \$errors
EOF2