Don't rely on Git to be preconfigured in tests (close #27)

This commit is contained in:
Chris de Graaf 2018-10-22 14:15:48 -05:00
parent 78a4fb0b9b
commit 9b6c320397
8 changed files with 49 additions and 39 deletions

View File

@ -20,9 +20,6 @@ build_script:
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"
test_script:
# Git configuration is required to make commits in generated packages.
- git config --global user.name "AppVeyor"
- git config --global user.email "appveyor@example.com"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
on_success:

View File

@ -12,12 +12,10 @@ matrix:
fast_finish: true
notifications:
email: false
before_script:
# Git configuration is required to make commits in generated packages.
- git config --global user.name "Travis"
- git config --global user.email "travis@example.com"
after_success:
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; CodeCov.submit(process_folder())'
# For zero-argument template example.
# For package generation examples.
- git config --global user.name "Travis"
- git config --global user.email "travis@c.i"
- git config --global github.user "travis"
- julia -e 'using Pkg; ps=Pkg.PackageSpec(name="Documenter", version="0.19"); Pkg.add(ps); Pkg.pin(ps); include(joinpath("docs", "make.jl"))'

View File

@ -4,7 +4,11 @@
Generate a package named `pkg` from `t`.
"""
function generate(pkg::AbstractString, t::Template)
function generate(
pkg::AbstractString,
t::Template;
gitconfig::Union{GitConfig, Nothing}=nothing,
)
pkg = splitjl(pkg)
pkg_dir = joinpath(t.dir, pkg)
ispath(pkg_dir) && throw(ArgumentError("$pkg_dir already exists"))
@ -16,6 +20,16 @@ function generate(pkg::AbstractString, t::Template)
# Initialize the repo.
repo = LibGit2.init(pkg_dir)
@info "Initialized git repo at $pkg_dir"
if gitconfig !== nothing
# Configure the repo.
repoconfig = GitConfig(repo)
for c in LibGit2.GitConfigIter(gitconfig)
LibGit2.set!(repoconfig, unsafe_string(c.name), unsafe_string(c.value))
end
end
# Commit and set the remote.
LibGit2.commit(repo, "Initial commit")
rmt = if t.ssh
"git@$(t.host):$(t.user)/$pkg.jl.git"
@ -29,7 +43,7 @@ function generate(pkg::AbstractString, t::Template)
# Create the gh-pages branch if necessary.
if haskey(t.plugins, GitHubPages)
LibGit2.branch!(repo, "gh-pages")
LibGit2.commit(repo, "Empty initial commit")
LibGit2.commit(repo, "Initial commit")
@info "Created empty gh-pages branch"
LibGit2.branch!(repo, "master")
end
@ -47,9 +61,8 @@ function generate(pkg::AbstractString, t::Template)
LibGit2.add!(repo, files...)
LibGit2.commit(repo, "Files generated by PkgTemplates")
@info "Staged and committed $(length(files)) files/directories: $(join(files, ", "))"
@info "Committed $(length(files)) files/directories: $(join(files, ", "))"
@info "Finished"
if length(collect(LibGit2.GitBranchIter(repo))) > 1
@info "Remember to push all created branches to your remote: git push --all"
end
@ -59,7 +72,13 @@ function generate(pkg::AbstractString, t::Template)
end
end
generate(t::Template, pkg::AbstractString) = generate(pkg, t)
function generate(
t::Template,
pkg::AbstractString;
gitconfig::Union{GitConfig, Nothing}=nothing,
)
generate(pkg, t; gitconfig=gitconfig)
end
"""
generate_interactive(pkg::AbstractString; fast::Bool=false) -> Template
@ -68,9 +87,13 @@ Interactively create a template, and then generate a package with it. Arguments
keywords are used in the same way as in [`generate`](@ref) and
[`interactive_template`](@ref).
"""
function generate_interactive(pkg::AbstractString; fast::Bool=false)
function generate_interactive(
pkg::AbstractString;
fast::Bool=false,
gitconfig::Union{GitConfig, Nothing}=nothing,
)
t = interactive_template(; fast=fast)
generate(pkg, t)
generate(pkg, t; gitconfig=gitconfig)
return t
end
@ -297,9 +320,4 @@ function substitute(
return substitute(template, merge(d, view))
end
"""
splitjl(pkg::AbstractString) -> AbstractString
Remove ".jl" from the end of a package name if it is present.
"""
splitjl(pkg::AbstractString) = endswith(pkg, ".jl") ? pkg[1:end-3] : pkg

View File

@ -47,15 +47,11 @@ create a template, you can use [`interactive_template`](@ref) instead.
)
# Check for required Git options for package generation
# (you can't commit to a repository without them).
if isempty(LibGit2.getconfig("user.name", ""))
@warn "Git config option 'user.name' missing, package generation will fail"
end
if isempty(LibGit2.getconfig("user.email", ""))
@warn "Git config option 'user.email' missing, package generation will fail"
end
isempty(LibGit2.getconfig("user.name", "")) && missingopt("user.name")
isempty(LibGit2.getconfig("user.email", "")) && missingopt("user.email")
# If no username was set, look for one in the global git config.
# Note: This is one of a few GitHub specifics.
# Note: This is one of a few GitHub specifics (maybe we could use the host value).
if isempty(user)
user = LibGit2.getconfig("github.user", "")
end
@ -216,9 +212,6 @@ function interactive_template(; fast::Bool=false)
return Template(; kwargs...)
end
"""
leaves(t:Type) -> Vector{DataType}
Get all concrete subtypes of `t`.
"""
leaves(t::Type)::Vector{DataType} = isconcretetype(t) ? [t] : vcat(leaves.(subtypes(t))...)
missingopt(name) = @warn "Git config option '$name' missing, package generation will fail unless you supply a GitConfig"

3
test/gitconfig Normal file
View File

@ -0,0 +1,3 @@
[user]
name = Travis
email = travis@c.i

View File

@ -52,7 +52,7 @@ end
@testset "Interactive package generation" begin
write(stdin.buffer, "$me\n\n\r\n\n\n\n\n\nd")
generate_interactive(test_pkg)
generate_interactive(test_pkg; gitconfig=gitconfig)
@test isdir(joinpath(default_dir, test_pkg))
rm(joinpath(default_dir, test_pkg); force=true, recursive=true)
end

View File

@ -56,7 +56,7 @@ pkg_dir = joinpath(t.dir, test_pkg)
@testset "Package generation with GitHubPages plugin" begin
temp_dir = mktempdir()
t = Template(; user=me, dir=temp_dir, plugins=[GitHubPages()])
generate(test_pkg, t)
generate(test_pkg, t; gitconfig=gitconfig)
# Check that the gh-pages branch exists.
repo = LibGit2.GitRepo(joinpath(t.dir, test_pkg))

View File

@ -20,6 +20,7 @@ const test_pkg = "TestPkg"
const fake_path = "/dev/null/this/file/does/not/exist"
const test_file = tempname()
const default_dir = Pkg.devdir()
const gitconfig = GitConfig(joinpath(@__DIR__, "gitconfig"))
const template_text = """
PKGNAME: {{PKGNAME}}
VERSION: {{VERSION}}}
@ -79,7 +80,7 @@ write(test_file, template_text)
)
# Duplicate plugins should warn.
@test_logs (:warn, r".+") t = Template(;
@test_logs (:warn, r"duplicates") match_mode=:any t = Template(;
user=me,
plugins=[TravisCI(), TravisCI()],
)
@ -239,7 +240,7 @@ end
@testset "Package generation" begin
t = Template(; user=me)
generate(test_pkg, t)
generate(test_pkg, t; gitconfig=gitconfig)
pkg_dir = joinpath(default_dir, test_pkg)
# Check that the expected files all exist.
@ -266,7 +267,7 @@ end
# Check that the remote is an SSH URL when we want it to be.
t = Template(; user=me, ssh=true)
generate(t, test_pkg) # Test the reversed-arguments method here.
generate(t, test_pkg; gitconfig=gitconfig) # Test the reversed-arguments method here.
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
@test LibGit2.url(remote) == "git@github.com:$me/$test_pkg.jl.git"
@ -274,7 +275,7 @@ end
# Check that the remote is set correctly for non-default hosts.
t = Template(; user=me, host="gitlab.com")
generate(test_pkg, t)
generate(test_pkg, t; gitconfig=gitconfig)
repo = LibGit2.GitRepo(pkg_dir)
remote = LibGit2.get(LibGit2.GitRemote, repo, "origin")
@test LibGit2.url(remote) == "https://gitlab.com/$me/$test_pkg.jl"
@ -283,7 +284,7 @@ end
# Check that the package ends up in the right directory.
temp_dir = mktempdir()
t = Template(; user=me, dir=temp_dir)
generate(test_pkg, t)
generate(test_pkg, t; gitconfig=gitconfig)
@test isdir(joinpath(temp_dir, test_pkg))
rm(temp_dir; recursive=true)
end