File: runtests

package info (click to toggle)
surfraw 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,624 kB
  • sloc: sh: 7,977; perl: 824; makefile: 277
file content (194 lines) | stat: -rwxr-xr-x 4,192 bytes parent folder | download | duplicates (4)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl -w
# $Id$
# Ian Beckwith <ianb@erislabs.net>
#

eval "require LWP::UserAgent" or die("runtests: libwww-perl not found, aborting tests.\n");
use Cwd;
use strict;
use vars qw($me);
$me=($0=~/(?:.*\/)?(.*)/)[0];

our($OK,$FAIL,$SKIP)=(0,1,2);
our $ua;

my $scriptpath=Cwd::abs_path($0);
# drop scriptname and last path component to get srcdir
my $srcdir= ($scriptpath=~/(.*)\/.*\/.*/)[0];
my @testfiles;
if(@ARGV) {
    @testfiles=@ARGV;
} else {
    @testfiles=<$srcdir/test/*.test>;
}

die("No tests found\n") unless(@testfiles);

$ua=LWP::UserAgent->new;
#$ua->env_proxy;
$ua->cookie_jar({file => "COOKIES", autosave => 0});
# Google & CNN dont like LWP::UserAgent's UserAgent string, nicking iceweasel's
$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20070508 Iceweasel/2.0.0.4 (Debian-2.0.0.4-1)");

my %results;

my @testnamelengths= map { my $base=(/(?:.*\/)?(.*)/)[0]; length($base); } @testfiles;
my $longestname=(sort { $a <=> $b } @testnamelengths)[$#testnamelengths];
$longestname -= length ".test";

for my $testfile (@testfiles)
{
    my ($elvi)=($testfile=~/(?:.*\/)?(.*)\.test/);
    unless(defined($elvi)) { $elvi=$testfile; }
    print $elvi;
    my $space=($longestname+1)-(length $elvi);
    if($space<1) { $space=1; }
    print " " x $space;
    $results{$elvi}=[  runtests($testfile) ];
}

my @failedelvi=grep { grep { $_ == $FAIL; } @{$results{$_}};  } sort keys %results;
if(@failedelvi)
{
    my $total=keys %results;
    my $failed=@failedelvi;
    printf "Failed $failed/$total (%d%%) elvi: ", ($failed/$total) * 100;
    for my $badelvis (@failedelvi)
    {
	my @thisresults=@{$results{$badelvis}};
	if($#thisresults)
	{
	    my $first=1;
	    print "${badelvis}[";
	    for(my $i=0;$i<=$#thisresults;$i++)
	    {
		if($thisresults[$i] == $FAIL)
		{
		    if(!$first) { print ","; }
		    $first=0;
		    print $i+1;
		}
	    }
	    print "] ";
	}
	else
	{
	    print "$badelvis ";
	}
    }
    print "\n";
}
else
{
    print "All tests passed.\n";
}

sub runtests
{
    my $testfile=shift;
    my $testnum=0;
    my @results=();
    unless(open(TEST,$testfile))
    {
	print "FAIL: cannot open $testfile: $!\n";
	return $FAIL;
    }
    my($cmd,$match);
    my @tests;
    my $count=0;
    while($cmd=<TEST>)
    {
	$testnum++;
	chomp($cmd);
	next if($cmd=~/^\s*$/); # ignore blank lines
	if($cmd=~/^SKIP\s*(.*)/)
	{
	    print "Test manually (see test/README)";
	    if(defined($1) && $1 ne "")
	    {
		print ": $1";
	    }
	    print "\n";
	    return $SKIP;
	}
	$count++;
	$match=<TEST>;
	unless(defined($match))
	{
	    print "FAIL: no match string defined for test $count\n";
	    close TEST;
	    return $FAIL;
	}
	chomp($match);
	push(@tests, { $cmd => $match });
    }
    close(TEST);
    my @ret=testelvis(@tests);
    print "\n";
    return @ret;
}


sub testelvis
{
    my(@tests)=@_;
    my @results=();
    for(my $count=0; $count <= $#tests; $count++)
    {
	if($count)  { print " "; }
	if($#tests) { print($count+1,": "); }
	my $hash=$tests[$count];
	my $cmd  =(keys  (%$hash))[0];
	my $match=(values(%$hash))[0];
	unless(open(ELVI,"SURFRAW_lang= SURFRAW_browser=echo SURFRAW_global_conf=\"$srcdir/surfraw.conf\" PATH=\"$srcdir/elvi:$srcdir:\$PATH\" $cmd|"))
	{
	    print "FAIL: cannot execute \"$cmd\": $!";
	    push(@results,$FAIL);
	    next;
	}
	my $url=<ELVI>;
	unless(defined($url))
	{
	    print "FAIL: elvi \"$cmd\" did not produce a URL";
	    push(@results,$FAIL);
	    close ELVI;
	    next;
	}
	close ELVI;
	chomp $url;

	my $response=$ua->get($url);
	unless($response->is_success)
	{
	    print "FAIL: Get failed: ",$response->status_line;
	    if($response->code =~/^[45]/)
	    {
		push(@results,$FAIL);
	    }
	    else
	    {
		print "Internal Error: Code ",$response->code," not handled";
		push(@results,$FAIL);
	    }
	    next;
	}
	my $content=$response->decoded_content || $response->content;
	if(!defined($content))
	{
	    print "FAIL: Result Page Empty\n";
	    push(@results,$FAIL);
	}
	elsif($content =~ /$match/)
	{
	    print "OK";
	    push(@results,$OK);
	}
	else
	{
	    print "FAIL: No Match";
#	    print $content;
	    push(@results,$FAIL);
	}
    }
    return @results;
}