From ee2d9a88945c05febd9f6f2aef840f1098f96389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 4 Apr 2024 18:31:37 +0200 Subject: [PATCH] Adapt test_ambiguities printing (#281) --- CHANGELOG.md | 7 +++++++ src/ambiguities.jl | 35 ++++++++++++++++++++--------------- test/test_ambiguities.jl | 1 - 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0cb2988..b95352b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- The output of `test_ambiguities` now gets printed to stderr instead of stdout. ([#281]) + + ## Version [v0.8.5] - 2024-04-03 ### Changed diff --git a/src/ambiguities.jl b/src/ambiguities.jl index d973a837..00b26457 100644 --- a/src/ambiguities.jl +++ b/src/ambiguities.jl @@ -117,12 +117,12 @@ function _find_ambiguities( num_ambiguities = if succ 0 else - reg_match = match(r"(\d+) ambiguities found", strout) + reg_match = match(r"(\d+) ambiguities found", strerr) reg_match === nothing && error( - "Failed to parse output of `detect_ambiguities`. The stdout was:\n" * + "Failed to parse output of `detect_ambiguities`.\nThe stdout was:\n" * strout * - "\n\n The stderr was:\n" * + "\n\nThe stderr was:\n" * strerr, ) @@ -205,25 +205,29 @@ function test_ambiguities_impl( sort!(ambiguities, by = (ms -> (ms[1].name, ms[2].name))) if !isempty(ambiguities) - printstyled("$(length(ambiguities)) ambiguities found.\n", color = :red) - println() + printstyled( + stderr, + "$(length(ambiguities)) ambiguities found. To get a list, set `broken = false`.\n"; + bold = true, + color = Base.error_color(), + ) end if !skipdetails for (i, (m1, m2)) in enumerate(ambiguities) - println("Ambiguity #", i) - println(m1) - println(m2) + println(stderr, "Ambiguity #", i) + println(stderr, m1) + println(stderr, m2) @static if isdefined(Base, :morespecific) - ambiguity_hint(m1, m2) - println() + ambiguity_hint(stderr, m1, m2) + println(stderr) end - println() + println(stderr) end end return isempty(ambiguities) end -function ambiguity_hint(m1::Method, m2::Method) +function ambiguity_hint(io::IO, m1::Method, m2::Method) # based on base/errorshow.jl#showerror_ambiguous # https://github.com/JuliaLang/julia/blob/v1.7.2/base/errorshow.jl#L327-L353 sigfix = Any @@ -232,11 +236,12 @@ function ambiguity_hint(m1::Method, m2::Method) if isa(Base.unwrap_unionall(sigfix), DataType) && sigfix <: Tuple let sigfix = sigfix if all(m -> Base.morespecific(sigfix, m.sig), [m1, m2]) - print("\nPossible fix, define\n ") - Base.show_tuple_as_call(stdout, :function, sigfix) + print(io, "\nPossible fix, define\n ") + Base.show_tuple_as_call(io, :function, sigfix) else - println() + println(io) print( + io, """To resolve the ambiguity, try making one of the methods more specific, or adding a new method more specific than any of the existing applicable methods.""", ) diff --git a/test/test_ambiguities.jl b/test/test_ambiguities.jl index 05f2fb33..f9173b55 100644 --- a/test/test_ambiguities.jl +++ b/test/test_ambiguities.jl @@ -32,7 +32,6 @@ include("preamble.jl") else @test num_ambiguities_ == num_ambiguities end - @test isempty(strerr) end check_testcase([], total)