Skip to content

Commit bd19475

Browse files
authored
Automatically reduce test failures General registry (#229)
1 parent 56c33b2 commit bd19475

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

test/fuzz_test.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -900,24 +900,24 @@ Reduce test case via combination of bisection and random deletion.
900900
This is suited to randomly generated strings, but it's surprisingly effective
901901
for code-like strings as well.
902902
"""
903-
function rand_reduce(str)
903+
function rand_reduce(str, parse_failure=parser_throws_exception)
904904
while true
905905
if length(str) <= 1
906906
return str
907907
end
908908
m1 = thisind(str, length(str)÷2)
909909
m2 = nextind(str, m1)
910-
if parser_throws_exception(str[1:m1])
910+
if parse_failure(str[1:m1])
911911
str = str[1:m1]
912-
elseif parser_throws_exception(str[m2:end])
912+
elseif parse_failure(str[m2:end])
913913
str = str[m2:end]
914914
else
915915
chunklen = clamp(length(str)÷10, 1, 10)
916916
reduced = false
917917
for i = 1:100
918918
m = thisind(str, rand(1:length(str)-chunklen))
919919
s = str[1:m]*str[nextind(str, m+chunklen):end]
920-
if parser_throws_exception(s)
920+
if parse_failure(s)
921921
str = s
922922
reduced = true
923923
break

tools/check_all_packages.jl

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using JuliaSyntax, Logging, TerminalLoggers, ProgressLogging, Serialization
77

88
include("../test/test_utils.jl")
9+
include("../test/fuzz_test.jl")
910

1011
pkgspath = joinpath(@__DIR__, "pkgs")
1112
source_paths = find_source_in_path(pkgspath)
@@ -16,6 +17,24 @@ mismatch_count = 0
1617
t0 = time()
1718
exceptions = []
1819

20+
function parsers_disagree(text::AbstractString)
21+
fl_ex = fl_parseall(text, filename="none")
22+
if Meta.isexpr(fl_ex, (:error,:incomplete)) ||
23+
(Meta.isexpr(fl_ex, :toplevel) && length(fl_ex.args) >= 1 &&
24+
Meta.isexpr(fl_ex.args[end], (:error,:incomplete)))
25+
return false
26+
end
27+
try
28+
ex = parseall(Expr, text, filename="none", ignore_errors=true)
29+
return !exprs_roughly_equal(fl_ex, ex)
30+
catch
31+
@error "Reduction failed" text
32+
return false
33+
end
34+
end
35+
36+
all_reduced_failures = String[]
37+
1938
Logging.with_logger(TerminalLogger()) do
2039
global exception_count, mismatch_count, t0
2140
@withprogress for (ifile, fpath) in enumerate(source_paths)
@@ -29,16 +48,19 @@ Logging.with_logger(TerminalLogger()) do
2948
e1 = JuliaSyntax.parseall(Expr, text, filename=fpath, ignore_warnings=true)
3049
if !exprs_roughly_equal(e2, e1)
3150
mismatch_count += 1
32-
reduced_chunks = sprint(context=:color=>true) do io
51+
failing_source = sprint(context=:color=>true) do io
3352
for c in reduce_test(text)
3453
JuliaSyntax.highlight(io, c.source, range(c), context_lines_inner=5)
3554
println(io, "\n")
3655
end
3756
end
57+
reduced_failures = rand_reduce.(sourcetext.(reduce_test(text)),
58+
parsers_disagree)
59+
append!(all_reduced_failures, reduced_failures)
3860
@error("Parsers succeed but disagree",
3961
fpath,
40-
reduced_chunks=Text(reduced_chunks),
41-
# diff=Text(sprint(show_expr_text_diff, show, e1, e2)),
62+
failing_source=Text(failing_source),
63+
reduced_failures,
4264
)
4365
end
4466
catch err
@@ -75,3 +97,14 @@ println()
7597
$(exception_count) failures compared to reference parser
7698
$(mismatch_count) Expr mismatches
7799
$(t_avg)ms per file"""
100+
101+
open(joinpath(@__DIR__, "reduced_failures.jl"), write=true) do io
102+
for str in all_reduced_failures
103+
println(io, repr(str))
104+
end
105+
for str in all_reduced_failures
106+
println(io, "#------------------------------")
107+
println(io, str)
108+
println(io)
109+
end
110+
end

0 commit comments

Comments
 (0)