From 9b6c3203973bc53ff38aa52ccfca13459b218cbe Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Mon, 22 Oct 2018 14:15:48 -0500 Subject: [PATCH] Don't rely on Git to be preconfigured in tests (close #27) --- .appveyor.yml | 3 --- .travis.yml | 8 +++---- src/generate.jl | 42 +++++++++++++++++++++++---------- src/template.jl | 17 ++++--------- test/gitconfig | 3 +++ test/interactive/interactive.jl | 2 +- test/plugins/githubpages.jl | 2 +- test/tests.jl | 11 +++++---- 8 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 test/gitconfig diff --git a/.appveyor.yml b/.appveyor.yml index 5abf512..11ec932 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -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: diff --git a/.travis.yml b/.travis.yml index 5615221..c7fbcda 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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"))' diff --git a/src/generate.jl b/src/generate.jl index 070d0d4..75f516c 100644 --- a/src/generate.jl +++ b/src/generate.jl @@ -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 diff --git a/src/template.jl b/src/template.jl index aeaedff..22a8c74 100644 --- a/src/template.jl +++ b/src/template.jl @@ -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" diff --git a/test/gitconfig b/test/gitconfig new file mode 100644 index 0000000..8675f32 --- /dev/null +++ b/test/gitconfig @@ -0,0 +1,3 @@ +[user] + name = Travis + email = travis@c.i diff --git a/test/interactive/interactive.jl b/test/interactive/interactive.jl index 23c5598..e0a121d 100644 --- a/test/interactive/interactive.jl +++ b/test/interactive/interactive.jl @@ -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 diff --git a/test/plugins/githubpages.jl b/test/plugins/githubpages.jl index 9681b74..ba1104e 100644 --- a/test/plugins/githubpages.jl +++ b/test/plugins/githubpages.jl @@ -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)) diff --git a/test/tests.jl b/test/tests.jl index a0a8405..6fc045d 100644 --- a/test/tests.jl +++ b/test/tests.jl @@ -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