From b33c216b6ad087be0979a323d049de021a157151 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 6 Feb 2026 10:22:29 +1300 Subject: [PATCH 1/3] Add an explicit test for method redefinitions instead of parsing logs --- test/General/test_errors.jl | 25 +++++++++++++++++++++++ test/runtests.jl | 40 ++++++------------------------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/test/General/test_errors.jl b/test/General/test_errors.jl index 0c1b85b5c8..6c796ed448 100644 --- a/test/General/test_errors.jl +++ b/test/General/test_errors.jl @@ -427,6 +427,31 @@ function test_GetAttributeNotAllowed_showerror() return end +function _test_method_redefinition(filename) + contents = read(filename, String) + functions = Set{String}() + for regex in (r"^function (test\_.+?)\(.*?\)"m, r"^(test\_.+?)\(.*?\) \= "m) + for m in eachmatch(regex, contents) + fn_name = String(m[1]) + if fn_name in functions + error("In $filename: overwritten method: $fn_name") + end + push!(functions, fn_name) + end + end + return +end + +# This function tests that all files in `/test` do not have redefined methods. +function test_method_redefinition() + for (root, dirs, files) in walkdir("test") + for file in files + _test_method_redefinition(joinpath(root, file)) + end + end + return +end + end # module TestErrors.runtests() diff --git a/test/runtests.jl b/test/runtests.jl index 31ebfd8c3a..5188f5870f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,45 +31,17 @@ MODULES_TO_TEST = get( "General;Benchmarks;Bridges/General;Bridges/Constraint;Bridges/Objective;Bridges/Variable;FileFormats;Nonlinear;Test;Utilities", ) -""" - include_with_method_redefinition_check(jl_filename) - -This function runs `include(jl_filename)` with an additional check that there -are no `WARNING: Method definition foo in module Foo overwritten` warnings. - -It does this by piping `stderr` to a file, and then parsing the file. - -One consequence is that `stderr` is not printed until the _end_ of this -function. Thus, some warnings may appear in the wrong place. +is_test_file(f) = startswith(f, "test_") && endswith(f, ".jl") -This function requires Julia to be started with `--warn-overwrite=true`. -""" -const init_code = quote - function include_with_method_redefinition_check(jl_filename) - stderr_filename = tempname() - open(stderr_filename, "w") do io - return redirect_stderr(() -> include(jl_filename), io) - end - contents = read(stderr_filename, String) - print(stderr, contents) - regex = - r"WARNING: Method definition (.+?) in module (.+?) at (.+?) overwritten at (.+?)\n" - if match(regex, contents) !== nothing - error("Found overwritten method in $jl_filename") +testsuite = Dict{String,Expr}() +for submodule in split(MODULES_TO_TEST, ";") + for (root, dirs, files) in walkdir(submodule) + for file in joinpath.(root, filter(is_test_file, files)) + testsuite[file] = :(include($file)) end - return end end -is_test_file(f) = startswith(f, "test_") && endswith(f, ".jl") - -testsuite = Dict{String,Expr}( - file => :(include_with_method_redefinition_check($file)) for - submodule in split(MODULES_TO_TEST, ";") for - (root, dirs, files) in walkdir(submodule) for - file in joinpath.(root, filter(is_test_file, files)) -) - import MathOptInterface import ParallelTestRunner import Test From 794e866b3119192645ca7394270f97e487d01a0a Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 6 Feb 2026 11:36:38 +1300 Subject: [PATCH 2/3] Remove init_code from runtests call for 64-bit --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 5188f5870f..0bfe64cf5e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -47,7 +47,7 @@ import ParallelTestRunner import Test if Sys.WORD_SIZE == 64 - ParallelTestRunner.runtests(MathOptInterface, ARGS; testsuite, init_code) + ParallelTestRunner.runtests(MathOptInterface, ARGS; testsuite) else Test.@testset "$filename" for filename in keys(testsuite) include(filename) From 624a2cba3d1f610961d1560e3e482db2c8c471d0 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 6 Feb 2026 11:45:41 +1300 Subject: [PATCH 3/3] Update test/General/test_errors.jl --- test/General/test_errors.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/General/test_errors.jl b/test/General/test_errors.jl index 6c796ed448..2f3da89a08 100644 --- a/test/General/test_errors.jl +++ b/test/General/test_errors.jl @@ -444,7 +444,7 @@ end # This function tests that all files in `/test` do not have redefined methods. function test_method_redefinition() - for (root, dirs, files) in walkdir("test") + for (root, dirs, files) in walkdir(dirname(@__DIR__)) for file in files _test_method_redefinition(joinpath(root, file)) end