From 266a22d5c286af04fe9db9fba75a521dae2d23ad Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Sun, 6 Oct 2019 16:12:20 +0700 Subject: [PATCH] Check and warn for unused keywords, update some metadata --- LICENSE | 2 ++ Project.toml | 2 +- src/template.jl | 24 ++++++++++++++---------- test/runtests.jl | 2 +- test/template.jl | 5 +++++ 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/LICENSE b/LICENSE index a1486d4..2d40ab7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,5 @@ +MIT License + Copyright (c) 2017-2019 Chris de Graaf, Invenia Technical Computing Corporation Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Project.toml b/Project.toml index 5f61b62..713da4f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "PkgTemplates" uuid = "14b8a8f1-9102-5b29-a752-f990bacb7fe1" -authors = ["Chris de Graaf "] +authors = ["Chris de Graaf", "Invenia Technical Computing Corporation"] version = "0.7.0" [deps] diff --git a/src/template.jl b/src/template.jl index 9b7dc3e..af768cf 100644 --- a/src/template.jl +++ b/src/template.jl @@ -57,18 +57,20 @@ Template(; kwargs...) = Template(Val(false); kwargs...) # Non-interactive constructor. function Template(::Val{false}; kwargs...) - user = getkw(kwargs, :user) - dir = abspath(expanduser(getkw(kwargs, :dir))) - host = replace(getkw(kwargs, :host), r".*://" => "") - julia = getkw(kwargs, :julia) + kwargs = Dict(kwargs) - authors = getkw(kwargs, :authors) + user = getkw!(kwargs, :user) + dir = abspath(expanduser(getkw!(kwargs, :dir))) + host = replace(getkw!(kwargs, :host), r".*://" => "") + julia = getkw!(kwargs, :julia) + + authors = getkw!(kwargs, :authors) authors isa Vector || (authors = map(strip, split(authors, ","))) # User-supplied plugins come first, so that deduping the list will remove the defaults. plugins = Plugin[] - append!(plugins, getkw(kwargs, :plugins)) - disabled = getkw(kwargs, :disable_defaults) + append!(plugins, getkw!(kwargs, :plugins)) + disabled = getkw!(kwargs, :disable_defaults) append!(plugins, filter(p -> !(typeof(p) in disabled), default_plugins())) plugins = sort(unique(typeof, plugins); by=string) @@ -82,6 +84,8 @@ function Template(::Val{false}; kwargs...) end end + isempty(kwargs) || @warn "Unrecognized keywords were supplied" kwargs + t = Template(authors, dir, host, julia, plugins, user) foreach(p -> validate(p, t), t.plugins) return t @@ -120,11 +124,11 @@ hasplugin(t::Template, ::Type{T}) where T <: Plugin = hasplugin(t, p -> p isa T) # Get a plugin by type. function getplugin(t::Template, ::Type{T}) where T <: Plugin i = findfirst(p -> p isa T, t.plugins) - i === nothing ? nothing : t.plugins[i] + return i === nothing ? nothing : t.plugins[i] end -# Get a keyword, or compute some default value. -getkw(kwargs, k) = get(() -> defaultkw(Template, k), kwargs, k) +# Get a keyword or a default value. +getkw!(kwargs, k) = pop!(kwargs, k, defaultkw(Template, k)) # Default Template keyword values. defaultkw(::Type{T}, s::Symbol) where T = defaultkw(T, Val(s)) diff --git a/test/runtests.jl b/test/runtests.jl index dc369b8..9836b30 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,7 +3,7 @@ using Base.Filesystem: contractuser, path_separator using LibGit2: LibGit2, GitCommit, GitRemote, GitRepo using Pkg: Pkg using Random: Random -using Test: @test, @testset, @test_throws +using Test: @test, @testset, @test_logs, @test_throws using ReferenceTests: @test_reference using SimpleMock: mock diff --git a/test/template.jl b/test/template.jl index 11a2e21..db47e13 100644 --- a/test/template.jl +++ b/test/template.jl @@ -43,6 +43,11 @@ # Disabling a default plugin. test_plugins([], setdiff(defaults, [default_g]), [Git]) end + + @testset "Unsupported keywords warning" begin + @test_logs tpl() + @test_logs (:warn, "Unrecognized keywords were supplied") tpl(; x=1, y=2) + end end @testset "hasplugin" begin