Skip to content

Commit 2c1b560

Browse files
authored
Add a check that methods are not overwritten in test modules (#2805)
1 parent 78e7db2 commit 2c1b560

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

test/FileFormats/NL/read.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ function test_binary_parse_x()
955955
return
956956
end
957957

958-
function test_parse_d()
958+
function test_binary_parse_d()
959959
model = NL._CacheModel()
960960
model.is_binary = true
961961
io = IOBuffer()

test/runtests.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,36 @@ MODULES_TO_TEST = get(
3131
"General;Benchmarks;Bridges;Bridges/Constraint;Bridges/Objective;Bridges/Variable;FileFormats;Nonlinear;Test;Utilities",
3232
)
3333

34+
"""
35+
include_with_method_redefinition_check(jl_filename)
36+
37+
This function runs `include(jl_filename)` with an additional check that there
38+
are no `WARNING: Method definition foo in module Foo overwritten` warnings.
39+
40+
It does this by piping `stderr` to a file, and then parsing the file.
41+
42+
One consequence is that `stderr` is not printed until the _end_ of this
43+
function. Thus, some warnings may appear in the wrong place.
44+
45+
This function requires Julia to be started with `--warn-overwrite=true`.
46+
"""
47+
function include_with_method_redefinition_check(jl_filename)
48+
stderr_filename = tempname()
49+
open(stderr_filename, "w") do io
50+
return redirect_stderr(() -> include(jl_filename), io)
51+
end
52+
contents = read(stderr_filename, String)
53+
print(stderr, contents)
54+
regex =
55+
r"WARNING: Method definition (.+?) in module (.+?) at (.+?) overwritten at (.+?)\n"
56+
if match(regex, contents) !== nothing
57+
error("Found overwritten method")
58+
end
59+
return
60+
end
61+
3462
for submodule in split(MODULES_TO_TEST, ";")
35-
include("$(submodule)/runtests.jl")
63+
include_with_method_redefinition_check("$(submodule)/runtests.jl")
3664
GC.gc() # Force GC run here to reduce memory pressure
3765
end
3866

0 commit comments

Comments
 (0)