Repository navigation
Expand file tree
/
Copy pathrm_broken_tests.pl
More file actions
executable file
·334 lines (274 loc) · 11 KB
/
Copy pathrm_broken_tests.pl
File metadata and controls
executable file
·334 lines (274 loc) · 11 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env perl
#
#-------------------------------------------------------------------------------
# Copyright (c) 2014-2019 René Just, Darioush Jalali, and Defects4J contributors.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-------------------------------------------------------------------------------
=pod
=head1 NAME
rm_broken_tests.pl -- remove broken test methods from a set of test classes.
=head1 SYNOPSIS
rm_broken_tests.pl log_file src_dir [except]
=head1 DESCRIPTION
Parses the file F<log_file> and fixes failing test methods by replacing each broken test
method with a dummy test method in the source file of the corresponding test class. The
source file of the test class is backed up prior to the first modification. If except is
provided, then this test is maintained even if it appears in the log file.
=cut
#
# TODO: This file needs a thorough overhaul and its command-line interface is not
# Defects4J standard!
#
use IO::File;
use File::Copy;
use Text::Balanced qw (extract_bracketed);
use warnings;
use strict;
($#ARGV==1 || $#ARGV==2) or die "usage: $0 log_file src_dir [except]";
my $log_file = shift @ARGV;
my $base_dir = shift @ARGV;
my $except = shift @ARGV;
my $verbose = 0;
-e $log_file or die "Cannot open log file: $!";
=pod
The log file may contain arbitrary lines -- the script only considers lines that
match the pattern: B<C</--- ([^:]*)(::(.*))?/>>.
=head2 Example entries in the log file
=over 4
=item *
Failing test class: C<--- package.Class>
=item *
Failing test method: C<--- package.Class::method>
=back
All lines matching the pattern are sorted, such that a failing test class in the
list will appear before any of its failing methods.
=cut
my @list = `grep -B1 "<error" $log_file | grep -P -o '(?<=classname=").*?(?=").(?<=name=").*?(?=")' | sed 's/" name="/::/g' | sed 's/^/--- /'`;
my $counter=0;
my @tests;
# Check all entries in the log file
for (@list) {
/--- ([^:]+)(::([^:]+))?/ or die "Corrupted log file: $_";
}
my %buffers;
# Cache the entire log file for stack trace analysis
open FILE, $log_file or die "Cannot open log file ($log_file): $!";
my @log_lines = <FILE>;
close FILE;
# TODO: Remove this hack once the command-line interface has been updated.
my $RM_ASSERTS = (defined $ENV{D4J_RM_ASSERTS} && $ENV{D4J_RM_ASSERTS} == 1) ? 1 : 0;
# This variable is used to keep track of whether this program uses
# junit 4 styles. This is important, since the @Test annotation
# must not be added on empty method additions for superclass
# failing methods
# This hash will map a filename to 1 in case it is junit 4.
my %is_buffer_junit4 = ();
my @method_list=();
for (@list){
chomp;
/--- ([^:]+)(::([^:]+))?/;
my $class = $1;
my $method = $3;
_exclude_test_class($class) unless defined $method;
if ($except) {
next if "$class::$method" eq $except; # skip the excepted test.
}
my $file = $class;
$file =~ s/\./\//g;
$file = "$base_dir/$file.java";
# Skip non-existing files
if (! -e $file) {
print STDERR "$0: $file does not exist -> SKIP ($method)\n" if $verbose;
next;
}
# Backup file if necessary
if (! -e "$file.bak") {
copy("$file","$file.bak") or die "Cannot backup file ($file): $!";
}
# Buffer file for modifications
_buffer_file($file);
if (defined $method) {
# Check wether removing failing assertions is enabled and successful
unless ($RM_ASSERTS && _remove_assertion($class, $method)) {
push(@method_list, $_);
}
}
}
# Remove the remaining test methods -- the ones for which we couldn't remove the failing
# assertion.
for (@method_list) {
/--- ([^:]+)(::([^:]+))?/;
my $class = $1;
my $method = $3;
_remove_test_method($class, $method);
}
# Write file buffers
_write_buffers();
0;
sub _exclude_test_class {
my $class = shift;
# We do not remove broken test classes as
# this might cause compilation issues
}
# TODO: Use Utils::get_failing_tests to obtain information about failing assertions
sub _remove_assertion {
my ($class, $method) = @_;
my $file = $class;
$file =~ s/\./\//g;
$file = "$base_dir/$file.java";
for (my $i=0; $i <= $#log_lines; ++$i) {
local $_ = $log_lines[$i];
chomp;
/--- $class::$method/ or next;
# Read first line of stack trace to determine the failure reason.
my $reason = $log_lines[$i+1];
if (defined $reason and $reason =~ /junit.framework.AssertionFailedError/) {
$class =~ /(.*\.)?([^.]+)/ or die "Couldn't determine class name: $class!";
my $classname = $2;
++$i;
while ($log_lines[$i] !~ /---/) {
if ($log_lines[$i] =~ /junit\./) {
# Skip junit entries in the stack trace
++$i;
} elsif ($log_lines[$i] =~ /$classname\.java:(\d+)/) {
# We found the right entry in the stack trace
my $line = $1;
# Check whether this line looks like an assertion, if so remove it.
if ($buffers{$file}->[$line-1] =~ /assert.*\(.*\)/) {
$buffers{$file}->[$line-1] = "// Remove failing test when running the first time --> " . $buffers{$file}->[$line-1];
return 1;
}
last;
} else {
# The stack trace isn't what we expected -- give up and continue
# with the next triggering test
last;
}
}
}
}
return 0;
}
sub _remove_test_method {
my ($class, $method) = @_;
my $file = $class;
$file =~ s/\./\//g;
$file = "$base_dir/$file.java";
my @lines=@{$buffers{$file}};
# Line buffer for the fixed source file
my @buffer;
for (my $i=0; $i<=$#lines; ++$i) {
if ($lines[$i] =~ /^([^\/]*)public.+$method\(\)/) {
my $index = $i;
# Found the test to exclude
my $space = $1;
# Dummy test
my $dummy = "// Remove failing test when run for the first time\n";
# Check whether JUnit4 annotation is present
if ($lines[$i-1] =~ /\@Test/) {
$dummy = "${space}$dummy";
--$index;
}
# Remove all String/Character literals and comments from the
# temporary buffer as they may contain unbalanced delimiters or
# brackets.
#
# TODO: This is a rather hacky solution. We should think about a
# proper parser that computes a line-number table for all methods.
my @tmp = @lines[$index..$#lines];
foreach (@tmp) {
# Special case on escaped backslashes -- Strings and chars.
s/"\\\\"/""/g;
s/'\\\\'/''/g;
# This captures String literals -- accounting for escaped quotes
# (\") and non-escaped quotes (" and \\")
s/([\"'])(?:\\(\\\\)*\1|.)*?\1/$1$1/g;
s/\/\/.*/\/\//;
}
my @result = extract_bracketed(join("", @tmp), '{"\'}', '[^\{]*');
die "Could not extract method body for $class::$method" unless defined $result[0];
my $len = scalar(split("\n", $result[2].$result[0]));
# Add everything before broken method
push(@buffer, @lines[0..($index-1)]);
# Add dummy method
push(@buffer, $dummy);
# Comment out broken method
foreach (@lines[$index..($index+$len-1)]) {
push(@buffer, "// $_");
}
# Add everything after broken method
push(@buffer, @lines[($index+$len)..$#lines]);
last;
}
}
if (@buffer) {
# Update file buffer
$buffers{$file} = \@buffer;
} else {
# Override failing test method if it was implemented in super class
my $override = ###### " \@Override\n" .
###### TODO: There is a problem with adding the @Override annotation
# it is that when we are dealing with e.g, broken_tests
# and specify them project wide, it may be that
# the file exists but not the method, so we
# don't find it and assume it was in the super class
# This is different than the case when we know it is
# failing for this particular revision.
" public void $method() {} // Fails in super class\n";
# Only add @Test annotation if we are using Junit 4
$override = " \@Test\n" . $override if $is_buffer_junit4{$file};
# Read file buffer, determine closing curly brace of test class,
# and insert test method before the brace.
# TODO: This is probably not the most elegant solution.
my @buffer = @{$buffers{$file}};
for (my $index=$#buffer; $index>=0; --$index) {
# Find closing curly
next unless $buffer[$index] =~ /}/;
# Insert dummy (empty) test method
$buffer[$index] =~ s/^(.*)}([^}])$/$1\n$override}$2/;
$buffers{$file} = \@buffer;
last;
}
}
}
sub _buffer_file {
my $file = shift;
if (!defined($buffers{$file})) {
# Read the entire file
my $in = IO::File->new("<$file") or die "Cannot open source file: $file!";
my @data = <$in>;
$in->close();
$buffers{$file}=\@data;
# Check for junit 4
$is_buffer_junit4{$file} = 0;
$is_buffer_junit4{$file} = 1 if grep {/import org\.junit\.Test/} @data;
}
}
sub _write_buffers {
sleep(1);
foreach my $file (keys %buffers) {
my @buffer = @{$buffers{$file}};
unlink($file);
my $fix = IO::File->new(">$file") or die "Cannot write source file: $file!";
print $fix @buffer;
$fix->flush();
$fix->close();
}
}