Skip to content

Commit 069972e

Browse files
committed
Wrap ambiguities.jl into module
1 parent 6b61322 commit 069972e

File tree

5 files changed

+60
-46
lines changed

5 files changed

+60
-46
lines changed

src/ambiguities.jl

+24-43
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,27 @@ false-positive.
2525
- Other keyword arguments such as `imported` and `ambiguous_bottom`
2626
are passed to `Test.detect_ambiguities` as-is.
2727
"""
28-
test_ambiguities(packages; kwargs...) = _test_ambiguities(aspkgids(packages); kwargs...)
28+
function test_ambiguities(packages; broken::Bool = false, kwargs...)
29+
num_ambiguities, strout, strerr = Ambiguities.find_ambiguities(packages; kwargs...)
2930

30-
const ExcludeSpec = Pair{Base.PkgId,String}
31-
32-
aspkgids(pkg::Union{Module,PkgId}) = aspkgids([pkg])
33-
aspkgids(packages) = mapfoldl(aspkgid, push!, packages, init = PkgId[])
31+
println(stderr, strerr)
32+
println(stdout, strout)
3433

35-
aspkgid(pkg::PkgId) = pkg
36-
function aspkgid(m::Module)
37-
if !ispackage(m)
38-
error("Non-package (non-toplevel) module is not supported. Got: $m")
34+
if broken
35+
@test_broken num_ambiguities == 0
36+
else
37+
@test num_ambiguities == 0
3938
end
40-
return PkgId(m)
41-
end
42-
function aspkgid(name::Symbol)
43-
# Maybe `Base.depwarn()`
44-
return Base.identify_package(String(name))::PkgId
4539
end
4640

47-
ispackage(m::Module) =
48-
if m in (Base, Core)
49-
true
50-
else
51-
parentmodule(m) == m
52-
end
41+
module Ambiguities
42+
43+
using Base: PkgId
44+
using Test: detect_ambiguities
45+
46+
using ..Aqua: aspkgids, checked_repr, reprpkgid
47+
48+
const ExcludeSpec = Pair{Base.PkgId,String}
5349

5450
strnameof(x) = string(x)
5551
strnameof(x::Type) = string(nameof(x))
@@ -81,23 +77,15 @@ function reprexclude(exspecs::Vector{ExcludeSpec})
8177
itemreprs = map(exspecs) do (pkgid, name)
8278
string("(", reprpkgid(pkgid), " => ", repr(name), ")")
8379
end
84-
return string("Aqua.ExcludeSpec[", join(itemreprs, ", "), "]")
80+
return string("Aqua.Ambiguities.ExcludeSpec[", join(itemreprs, ", "), "]")
8581
end
8682

87-
function _test_ambiguities(packages::Vector{PkgId}; broken::Bool = false, kwargs...)
88-
num_ambiguities, strout, strerr = _find_ambiguities(packages; kwargs...)
89-
90-
println(stderr, strerr)
91-
println(stdout, strout)
92-
93-
if broken
94-
@test_broken num_ambiguities == 0
95-
else
96-
@test num_ambiguities == 0
97-
end
83+
function find_ambiguities(packages; kwargs...)
84+
package_ids = aspkgids(packages)::Vector{PkgId}
85+
return find_ambiguities(package_ids; kwargs...)
9886
end
9987

100-
function _find_ambiguities(
88+
function find_ambiguities(
10189
packages::Vector{PkgId};
10290
color::Union{Bool,Nothing} = nothing,
10391
exclude::AbstractVector = [],
@@ -113,7 +101,7 @@ function _find_ambiguities(
113101
code = """
114102
$(Base.load_path_setup_code())
115103
using Aqua
116-
Aqua.test_ambiguities_impl(
104+
Aqua.Ambiguities.test_ambiguities_impl(
117105
$packages_repr,
118106
$options_repr,
119107
$exclude_repr,
@@ -153,15 +141,6 @@ function reprpkgids(packages::Vector{PkgId})
153141
return packages_repr
154142
end
155143

156-
function reprpkgid(pkg::PkgId)
157-
name = pkg.name
158-
if pkg.uuid === nothing
159-
return "Base.PkgId($(repr(name)))"
160-
end
161-
uuid = pkg.uuid.value
162-
return "Base.PkgId(Base.UUID($(repr(uuid))), $(repr(name)))"
163-
end
164-
165144
struct _NoValue end
166145

167146
function getobj(m::Method)
@@ -252,3 +231,5 @@ function ambiguity_hint(m1::Method, m2::Method)
252231
end
253232
end
254233
end
234+
235+
end # module

src/utils.jl

+27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ function askwargs(flag::Bool)
1212
return NamedTuple()
1313
end
1414

15+
aspkgids(pkg::Union{Module,PkgId}) = aspkgids([pkg])
16+
aspkgids(packages) = map(aspkgid, packages)
17+
18+
aspkgid(pkg::PkgId) = pkg
19+
function aspkgid(m::Module)
20+
if !ispackage(m)
21+
error("Non-package (non-toplevel) module is not supported. Got: $m")
22+
end
23+
return PkgId(m)
24+
end
25+
26+
ispackage(m::Module) =
27+
if m in (Base, Core)
28+
true
29+
else
30+
parentmodule(m) == m
31+
end
32+
33+
function reprpkgid(pkg::PkgId)
34+
name = pkg.name
35+
if pkg.uuid === nothing
36+
return "Base.PkgId($(repr(name)))"
37+
end
38+
uuid = pkg.uuid.value
39+
return "Base.PkgId(Base.UUID($(repr(uuid))), $(repr(name)))"
40+
end
41+
1542
struct LazyTestResult
1643
label::String
1744
message::String

test/test_ambiguities.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ module TestAmbiguities
22

33
include("preamble.jl")
44

5+
using Aqua: Ambiguities
6+
57
using PkgWithAmbiguities
68

79
@testset begin
810
function check_testcase(exclude, num_ambiguities::Int; broken::Bool = false)
911
pkgids = Aqua.aspkgids([PkgWithAmbiguities, Core]) # include Core to find constructor ambiguities
10-
num_ambiguities_, strout, strerr = Aqua._find_ambiguities(pkgids; exclude = exclude)
12+
num_ambiguities_, strout, strerr =
13+
Ambiguities.find_ambiguities(pkgids; exclude = exclude)
1114
if broken
1215
@test_broken num_ambiguities_ == num_ambiguities
1316
else

test/test_exclude.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module TestExclude
22

33
include("preamble.jl")
4+
45
using Base: PkgId
5-
using Aqua: getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, reprexclude
6+
7+
using Aqua.Ambiguities:
8+
getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, reprexclude
69

710
@assert parentmodule(Tuple) === Core
811
@assert parentmodule(foldl) === Base

test/test_getobj.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module TestGetObj
22

3-
using Aqua: getobj
3+
using Aqua.Ambiguities: getobj
44
using Test
55

66
module ModuleA

0 commit comments

Comments
 (0)