Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ambiguities.jl #143

Merged
merged 3 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/ambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ aspkgids(packages) = mapfoldl(aspkgid, push!, packages, init = PkgId[])
aspkgid(pkg::PkgId) = pkg
function aspkgid(m::Module)
if !ispackage(m)
error("Non-package (non-toplevel) module is not supported.", " Got: $m")
error("Non-package (non-toplevel) module is not supported. Got: $m")
end
return PkgId(m)
end
Expand Down Expand Up @@ -72,7 +72,7 @@ function normalize_and_check_exclude(exclude::AbstractVector)
exspecs = mapfoldl(normalize_exclude, push!, exclude, init = ExcludeSpec[])
for (spec, obj) in zip(exspecs, exclude)
if getobj(spec) !== obj
error("Name `$str` is resolved to a different object.")
error("Name `$(spec[2])` is resolved to a different object.")
end
end
return exspecs::Vector{ExcludeSpec}
Expand All @@ -85,11 +85,23 @@ function reprexclude(exspecs::Vector{ExcludeSpec})
return string("Aqua.ExcludeSpec[", join(itemreprs, ", "), "]")
end

function _test_ambiguities(
function _test_ambiguities(packages::Vector{PkgId}; broken::Bool = false, kwargs...)
num_ambiguities, strout, strerr = _find_ambiguities(packages; kwargs...)

println(stderr, strerr)
println(stdout, strout)

if broken
@test_broken num_ambiguities == 0
else
@test num_ambiguities == 0
end
end

function _find_ambiguities(
packages::Vector{PkgId};
color::Union{Bool,Nothing} = nothing,
exclude::AbstractVector = [],
broken::Bool = false,
# Options to be passed to `Test.detect_ambiguities`:
detect_ambiguities_options...,
)
Expand All @@ -113,11 +125,21 @@ function _test_ambiguities(
cmd = `$cmd --color=yes`
end
cmd = `$cmd --startup-file=no -e $code`
if broken
@test_broken success(pipeline(cmd; stdout = stdout, stderr = stderr))

out = Pipe()
err = Pipe()
succ = success(pipeline(cmd; stdout = out, stderr = err))
close(out.in)
close(err.in)
strout = String(read(out))
strerr = String(read(err))
num_ambiguities = if succ
0
else
@test success(pipeline(cmd; stdout = stdout, stderr = stderr))
parse(Int, match(r"(\d+) ambiguities found", strout).captures[1])
end

return num_ambiguities, strout, strerr
end

function reprpkgids(packages::Vector{PkgId})
Expand Down Expand Up @@ -222,10 +244,8 @@ function ambiguity_hint(m1::Method, m2::Method)
else
println()
print(
"To resolve the ambiguity, try making one of the methods more specific, or ",
)
print(
"adding a new method more specific than any of the existing applicable methods.",
"""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.""",
)
end
end
Expand Down
41 changes: 19 additions & 22 deletions test/test_ambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,28 @@ using PkgWithAmbiguities

@testset begin
@static if VERSION >= v"1.3-"
results = @testtestset begin
@info "↓↓↓ Following failures are expected. ↓↓↓"
Aqua.test_ambiguities(PkgWithAmbiguities)
num_ambiguities, strout, strerr =
Aqua._find_ambiguities(Aqua.aspkgids(PkgWithAmbiguities))
@test num_ambiguities == 2
@test isempty(strerr)

# exclude just anything irrelevant, see #49
Aqua.test_ambiguities(PkgWithAmbiguities; exclude = [convert])
# exclude just anything irrelevant, see #49
num_ambiguities, strout, strerr =
Aqua._find_ambiguities(Aqua.aspkgids(PkgWithAmbiguities); exclude = [convert])
@test num_ambiguities == 2
@test isempty(strerr)

Aqua.test_ambiguities(
PkgWithAmbiguities;
exclude = [PkgWithAmbiguities.f, PkgWithAmbiguities.AbstractType],
)
@info "↑↑↑ Above failures are expected. ↑↑↑" # move above once broken test fixed
end
@test length(results) == 3
@test results[1] isa Test.Fail
@test results[2] isa Test.Fail
@test_broken results[3] isa Test.Pass
num_ambiguities, strout, strerr = Aqua._find_ambiguities(
Aqua.aspkgids(PkgWithAmbiguities);
exclude = [PkgWithAmbiguities.f, PkgWithAmbiguities.AbstractType],
)
@test_broken num_ambiguities == 0
@test isempty(strerr)
else
results = @testtestset begin
@info "↓↓↓ Following failures are expected. ↓↓↓"
Aqua.test_ambiguities(PkgWithAmbiguities)
@info "↑↑↑ Above failures are expected. ↑↑↑"
end
@test length(results) == 1
@test results[1] isa Test.Fail
num_ambiguities, strout, strerr =
Aqua._find_ambiguities(Aqua.aspkgids(PkgWithAmbiguities))
@test num_ambiguities == 1
@test isempty(strerr)
end

# It works with other tests:
Expand Down