Skip to content

Commit

Permalink
Wrap ambiguities.jl into module
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Jun 26, 2023
1 parent 6b61322 commit 069972e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
67 changes: 24 additions & 43 deletions src/ambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@ false-positive.
- Other keyword arguments such as `imported` and `ambiguous_bottom`
are passed to `Test.detect_ambiguities` as-is.
"""
test_ambiguities(packages; kwargs...) = _test_ambiguities(aspkgids(packages); kwargs...)
function test_ambiguities(packages; broken::Bool = false, kwargs...)
num_ambiguities, strout, strerr = Ambiguities.find_ambiguities(packages; kwargs...)

const ExcludeSpec = Pair{Base.PkgId,String}

aspkgids(pkg::Union{Module,PkgId}) = aspkgids([pkg])
aspkgids(packages) = mapfoldl(aspkgid, push!, packages, init = PkgId[])
println(stderr, strerr)
println(stdout, strout)

aspkgid(pkg::PkgId) = pkg
function aspkgid(m::Module)
if !ispackage(m)
error("Non-package (non-toplevel) module is not supported. Got: $m")
if broken
@test_broken num_ambiguities == 0
else
@test num_ambiguities == 0
end
return PkgId(m)
end
function aspkgid(name::Symbol)
# Maybe `Base.depwarn()`
return Base.identify_package(String(name))::PkgId
end

ispackage(m::Module) =
if m in (Base, Core)
true
else
parentmodule(m) == m
end
module Ambiguities

using Base: PkgId
using Test: detect_ambiguities

using ..Aqua: aspkgids, checked_repr, reprpkgid

const ExcludeSpec = Pair{Base.PkgId,String}

strnameof(x) = string(x)
strnameof(x::Type) = string(nameof(x))
Expand Down Expand Up @@ -81,23 +77,15 @@ function reprexclude(exspecs::Vector{ExcludeSpec})
itemreprs = map(exspecs) do (pkgid, name)
string("(", reprpkgid(pkgid), " => ", repr(name), ")")
end
return string("Aqua.ExcludeSpec[", join(itemreprs, ", "), "]")
return string("Aqua.Ambiguities.ExcludeSpec[", join(itemreprs, ", "), "]")
end

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
function find_ambiguities(packages; kwargs...)
package_ids = aspkgids(packages)::Vector{PkgId}
return find_ambiguities(package_ids; kwargs...)
end

function _find_ambiguities(
function find_ambiguities(
packages::Vector{PkgId};
color::Union{Bool,Nothing} = nothing,
exclude::AbstractVector = [],
Expand All @@ -113,7 +101,7 @@ function _find_ambiguities(
code = """
$(Base.load_path_setup_code())
using Aqua
Aqua.test_ambiguities_impl(
Aqua.Ambiguities.test_ambiguities_impl(
$packages_repr,
$options_repr,
$exclude_repr,
Expand Down Expand Up @@ -153,15 +141,6 @@ function reprpkgids(packages::Vector{PkgId})
return packages_repr
end

function reprpkgid(pkg::PkgId)
name = pkg.name
if pkg.uuid === nothing
return "Base.PkgId($(repr(name)))"
end
uuid = pkg.uuid.value
return "Base.PkgId(Base.UUID($(repr(uuid))), $(repr(name)))"
end

struct _NoValue end

function getobj(m::Method)
Expand Down Expand Up @@ -252,3 +231,5 @@ function ambiguity_hint(m1::Method, m2::Method)
end
end
end

end # module
27 changes: 27 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ function askwargs(flag::Bool)
return NamedTuple()
end

aspkgids(pkg::Union{Module,PkgId}) = aspkgids([pkg])
aspkgids(packages) = map(aspkgid, packages)

aspkgid(pkg::PkgId) = pkg
function aspkgid(m::Module)
if !ispackage(m)
error("Non-package (non-toplevel) module is not supported. Got: $m")
end
return PkgId(m)
end

ispackage(m::Module) =
if m in (Base, Core)
true
else
parentmodule(m) == m
end

function reprpkgid(pkg::PkgId)
name = pkg.name
if pkg.uuid === nothing
return "Base.PkgId($(repr(name)))"
end
uuid = pkg.uuid.value
return "Base.PkgId(Base.UUID($(repr(uuid))), $(repr(name)))"
end

struct LazyTestResult
label::String
message::String
Expand Down
5 changes: 4 additions & 1 deletion test/test_ambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ module TestAmbiguities

include("preamble.jl")

using Aqua: Ambiguities

using PkgWithAmbiguities

@testset begin
function check_testcase(exclude, num_ambiguities::Int; broken::Bool = false)
pkgids = Aqua.aspkgids([PkgWithAmbiguities, Core]) # include Core to find constructor ambiguities
num_ambiguities_, strout, strerr = Aqua._find_ambiguities(pkgids; exclude = exclude)
num_ambiguities_, strout, strerr =
Ambiguities.find_ambiguities(pkgids; exclude = exclude)
if broken
@test_broken num_ambiguities_ == num_ambiguities
else
Expand Down
5 changes: 4 additions & 1 deletion test/test_exclude.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module TestExclude

include("preamble.jl")

using Base: PkgId
using Aqua: getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, reprexclude

using Aqua.Ambiguities:
getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, reprexclude

@assert parentmodule(Tuple) === Core
@assert parentmodule(foldl) === Base
Expand Down
2 changes: 1 addition & 1 deletion test/test_getobj.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TestGetObj

using Aqua: getobj
using Aqua.Ambiguities: getobj
using Test

module ModuleA
Expand Down

0 comments on commit 069972e

Please sign in to comment.