Prioritize supplied git config over global one, fix warning test

This commit is contained in:
Chris de Graaf 2017-08-16 17:01:52 -05:00
parent 4a9cf7629f
commit 4a349e47b0
2 changed files with 34 additions and 21 deletions

View File

@ -4,16 +4,19 @@
Records common information used to generate a package. Records common information used to generate a package.
# Keyword Arguments # Keyword Arguments
* `user::AbstractString=LibGit2.getconfig("github.username", "")`: GitHub username. * `user::AbstractString="")`: GitHub username. If left unset, it will try to take the
If left as default and there is no value configured, an error will be thrown. value of a supplied git config's "github.username" key, then the global git config's
Alternatively, you can add a value to `git_config["github.username"]` to set your value. If neither is set, an `ArgumentError` is thrown.
username. This is case-sensitive for some plugins, so take care to enter it correctly. **This is case-sensitive for some plugins, so take care to enter it correctly.**
* `host::AbstractString="github.com"`: Code hosting service where your package will reside. * `host::AbstractString="github.com"`: URL to the code hosting service where your package
* `license::Union{AbstractString, Void}=nothing`: Name of the package licsense. If will reside.
* `license::Union{AbstractString, Void}=nothing`: Name of the package license. If
no license is specified, no license is created. [`show_license`](@ref) can be used to no license is specified, no license is created. [`show_license`](@ref) can be used to
list all available licenses, or to print out a particular license's text. list all available licenses, or to print out a particular license's text.
* `authors::Union{AbstractString, Array}=LibGit2.getconfig("user.name", "")`: Names that * `authors::Union{AbstractString, Array}=""`: Names that appear on the license. Supply a
appear on the license. Supply a string for one author, and an array for multiple. string for one author, and an array for multiple. Similarly to `user`, it will try to
take the value of a supplied git config's "user.name" key, then the global git config's
value, if it is left unset
* `years::Union{Int, AbstractString}=string(Dates.year(Dates.today()))`: Copyright years * `years::Union{Int, AbstractString}=string(Dates.year(Dates.today()))`: Copyright years
on the license. Can be supplied by a number, or a string such as "2016 - 2017". on the license. Can be supplied by a number, or a string such as "2016 - 2017".
* `dir::AbstractString=Pkg.dir()`: Directory in which the package will go. * `dir::AbstractString=Pkg.dir()`: Directory in which the package will go.
@ -34,22 +37,26 @@ Records common information used to generate a package.
plugins::Dict{DataType, Plugin} plugins::Dict{DataType, Plugin}
function Template(; function Template(;
user::AbstractString=LibGit2.getconfig("github.username", ""), user::AbstractString="",
host::AbstractString="https://github.com", host::AbstractString="https://github.com",
license::Union{AbstractString, Void}=nothing, license::Union{AbstractString, Void}=nothing,
authors::Union{AbstractString, Array}=LibGit2.getconfig("user.name", ""), authors::Union{AbstractString, Array}="",
years::Union{Int, AbstractString}=string(Dates.year(Dates.today())), years::Union{Int, AbstractString}=string(Dates.year(Dates.today())),
dir::AbstractString=Pkg.dir(), dir::AbstractString=Pkg.dir(),
julia_version::VersionNumber=VERSION, julia_version::VersionNumber=VERSION,
git_config::Dict=Dict(), git_config::Dict=Dict(),
plugins::Vector{P}=Vector{Plugin}(), plugins::Vector{P}=Vector{Plugin}(),
) where P <: Plugin ) where P <: Plugin
# If no username was set or found, look for one in the supplied git config. # If no username was set, look for one in a supplied git config,
if isempty(user) && (!haskey(git_config, "github.username") || # and then in the global git config.
isempty(git_config["github.username"])) if isempty(user)
user = get(
git_config, "github.username",
LibGit2.getconfig("github.username", ""),
)
end
if isempty(user)
throw(ArgumentError("No GitHub username found, set one with user=username")) throw(ArgumentError("No GitHub username found, set one with user=username"))
elseif isempty(user)
user = git_config["github.username"]
end end
host = URI(startswith(host, "https://") ? host : "https://$host").host host = URI(startswith(host, "https://") ? host : "https://$host").host
@ -58,9 +65,9 @@ Records common information used to generate a package.
throw(ArgumentError("License '$license' is not available")) throw(ArgumentError("License '$license' is not available"))
end end
# If an explicitly supplied git config contains a name and the author name was not # If no author was set, look for one in the supplied git config,
# explicitly supplied, then take the git config's name as the author name. # and then in the global git config.
if haskey(git_config, "user.name") && authors == LibGit2.getconfig("user.name", "") if isempty(authors)
authors = get(git_config, "user.name", LibGit2.getconfig("user.name", "")) authors = get(git_config, "user.name", LibGit2.getconfig("user.name", ""))
elseif isa(authors, Array) elseif isa(authors, Array)
authors = join(authors, ", ") authors = join(authors, ", ")

View File

@ -53,7 +53,8 @@ write(test_file, template_text)
@test t.authors == git_config["user.name"] @test t.authors == git_config["user.name"]
t = Template(; git_config=git_config) t = Template(; git_config=git_config)
@test t.user == "TesterMcTestFace" @test t.user == git_config["github.username"]
@test t.authors == git_config["user.name"]
t = Template(; t = Template(;
user="invenia", user="invenia",
@ -62,11 +63,16 @@ write(test_file, template_text)
@test Set(keys(t.plugins)) == Set([GitHubPages, TravisCI, AppVeyor, CodeCov]) @test Set(keys(t.plugins)) == Set([GitHubPages, TravisCI, AppVeyor, CodeCov])
@test Set(values(t.plugins)) == Set([GitHubPages(), TravisCI(), AppVeyor(), CodeCov()]) @test Set(values(t.plugins)) == Set([GitHubPages(), TravisCI(), AppVeyor(), CodeCov()])
@test_warn r".*" Template(; @test_warn r".+" Template(;
user="invenia", user="invenia",
plugins=[TravisCI(), TravisCI()], plugins=[TravisCI(), TravisCI()],
) )
@test_throws ArgumentError Template() if isempty(LibGit2.getconfig("github.username", ""))
@test_throws ArgumentError Template()
else
t = Template()
@test t.user == LibGit2.getconfig("github.username", "")
end
@test_throws ArgumentError Template(; user="invenia", license="FakeLicense") @test_throws ArgumentError Template(; user="invenia", license="FakeLicense")
end end