Implement equality for Template and Plugins

This commit is contained in:
Chris de Graaf 2020-02-15 14:26:25 +07:00
parent a1dc3ef95c
commit 914480157b
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
5 changed files with 36 additions and 5 deletions

View File

@ -1,6 +1,10 @@
const TEMPLATES_DIR = normpath(joinpath(@__DIR__, "..", "templates"))
const DEFAULT_PRIORITY = 1000
function Base.:(==)(a::T, b::T) where T <: Plugin
return all(n -> getfield(a, n) == getfield(b, n), fieldnames(T))
end
"""
Secret(name::AbstractString)

View File

@ -130,6 +130,15 @@ function (t::Template)(pkg::AbstractString)
@info "New package is at $pkg_dir"
end
function Base.:(==)(a::Template, b::Template)
return a.authors == b.authors &&
a.dir == b.dir &&
a.host == b.host &&
a.julia == b.julia &&
a.user == b.user &&
all(map(==, a.plugins, b.plugins))
end
# Does the template have a plugin that satisfies some predicate?
hasplugin(t::Template, f::Function) = any(f, t.plugins)
hasplugin(t::Template, ::Type{T}) where T <: Plugin = hasplugin(t, p -> p isa T)

View File

@ -1,6 +1,9 @@
# Don't move this line from the top, please. {{X}} {{Y}} {{Z}}
struct BasicTest <: PT.BasicPlugin end
struct BasicTest <: PT.BasicPlugin
a::String
b::Bool
end
PT.gitignore(::BasicTest) = ["a", "aa", "aaa"]
PT.source(::BasicTest) = @__FILE__
@ -11,7 +14,7 @@ PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" =>
@testset "Plugins" begin
@testset "BasicPlugin" begin
p = BasicTest()
p = BasicTest("foo", true)
t = tpl(; plugins=[p])
# The X from user_view should override the X from view.
@ -34,4 +37,12 @@ PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" =>
@test_logs tpl(; julia=v"1.2", plugins=[p])
@test_logs tpl(; julia=v"1.3", plugins=[p])
end
@testset "Equality" begin
a = BasicTest("foo", true)
b = BasicTest("foo", true)
@test a == b
c = BasicTest("foo", false)
@test a != c
end
end

View File

@ -44,7 +44,7 @@ const LICENSES_DIR = joinpath(TEMPLATES_DIR, "licenses")
file: "$(joinpath(TEMPLATES_DIR, "github", "workflows", "TagBot.yml"))"
destination: "TagBot.yml"
cron: "0 * * * *"
token: \${{ secrets.GITHUB_TOKEN }}
token: Secret("GITHUB_TOKEN")
ssh: nothing
ssh_password: nothing
changelog: nothing
@ -63,9 +63,8 @@ const LICENSES_DIR = joinpath(TEMPLATES_DIR, "licenses")
end
@testset "show as serialization" begin
# Equality is not implemented for Template, so check the string form.
t1 = tpl()
t2 = eval(Meta.parse(sprint(show, t1)))
@test sprint(show, t1) == sprint(show, t2)
@test t1 == t2
end
end

View File

@ -50,6 +50,14 @@
end
end
@testset "Equality" begin
a = tpl()
b = tpl()
@test a == b
c = tpl(julia=v"0.3")
@test a != c
end
@testset "hasplugin" begin
t = tpl(; plugins=[TravisCI(), Documenter{TravisCI}()])
@test PT.hasplugin(t, typeof(first(PT.default_plugins())))