More tests, more Windows fiddling

Cassette is cool!
This commit is contained in:
Chris de Graaf 2019-09-01 16:10:40 +07:00
parent f61253b010
commit 493c54a948
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
8 changed files with 187 additions and 85 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* eol=lf

View File

@ -16,9 +16,11 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
julia = "1" julia = "1"
[extras] [extras]
Cassette = "7057c7e9-c182-5462-911a-8362d720325c"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf" ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[targets] [targets]
test = ["Random", "ReferenceTests", "Test"] test = ["Cassette", "Suppressor", "Random", "ReferenceTests", "Test"]

View File

@ -6,6 +6,8 @@ Generate a package named `pkg` from a [`Template`](@ref).
function (t::Template)(pkg::AbstractString) function (t::Template)(pkg::AbstractString)
endswith(pkg, ".jl") && (pkg = pkg[1:end-3]) endswith(pkg, ".jl") && (pkg = pkg[1:end-3])
pkg_dir = joinpath(t.dir, pkg) pkg_dir = joinpath(t.dir, pkg)
ispath(pkg_dir) && throw(ArgumentError("$pkg_dir already exists"))
repo = nothing
try try
# Create the directory with some boilerplate inside. # Create the directory with some boilerplate inside.
@ -24,12 +26,15 @@ function (t::Template)(pkg::AbstractString)
# Initialize the repo, make a commit, and set the remote. # Initialize the repo, make a commit, and set the remote.
repo = LibGit2.init(pkg_dir) repo = LibGit2.init(pkg_dir)
LibGit2.commit(repo, "Initial commit") LibGit2.commit(repo, "Initial commit")
rmt = if t.ssh url = if t.ssh
"git@$(t.host):$(t.user)/$pkg.jl.git" "git@$(t.host):$(t.user)/$pkg.jl.git"
else else
"https://$(t.host)/$(t.user)/$pkg.jl" "https://$(t.host)/$(t.user)/$pkg.jl"
end end
close(LibGit2.GitRemote(repo, "origin", rmt)) remote = LibGit2.GitRemote(repo, "origin", url)
# TODO: `git pull` still requires some Git branch config.
LibGit2.add_push!(repo, remote, "refs/heads/master")
close(remote)
end end
# Generate the files. # Generate the files.
@ -56,6 +61,8 @@ function (t::Template)(pkg::AbstractString)
catch catch
rm(pkg_dir; recursive=true, force=true) rm(pkg_dir; recursive=true, force=true)
rethrow() rethrow()
finally
repo === nothing || close(repo)
end end
end end

55
test/git.jl Normal file
View File

@ -0,0 +1,55 @@
@context PTIsInstalled
function Cassette.posthook(::PTIsInstalled, result::Dict, ::typeof(Pkg.installed))
result["PkgTemplates"] = v"1.2.3"
return result
end
@testset "Git repositories" begin
@testset "Does not create Git repo" begin
t = tpl(; git=false)
with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)
@test !isdir(joinpath(pkg_dir, ".git"))
end
end
@testset "Creates Git repo" begin
t = tpl(; git=true)
with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)
@test isdir(joinpath(pkg_dir, ".git"))
end
end
@testset "With HTTPS" begin
t = tpl(; git=true, ssh=false)
with_pkg(t) do pkg
LibGit2.with(GitRepo(joinpath(t.dir, pkg))) do repo
remote = LibGit2.get(GitRemote, repo, "origin")
@test startswith(LibGit2.url(remote), "https://")
end
end
end
@testset "With SSH" begin
t = tpl(; git=true, ssh=true)
with_pkg(t) do pkg
LibGit2.with(GitRepo(joinpath(t.dir, pkg))) do repo
remote = LibGit2.get(GitRemote, repo, "origin")
@test startswith(LibGit2.url(remote), "git@")
end
end
end
@testset "Adds version to commit message" begin
# We're careful to avoid a Pkg.update as it triggers Cassette#130.
t = tpl(; git=true, develop=false, disable_defaults=[Tests])
@overdub PTIsInstalled() with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)
LibGit2.with(GitRepo(pkg_dir)) do repo
commit = GitCommit(repo, "HEAD")
@test occursin("PkgTemplates version: 1.2.3", LibGit2.message(commit))
end
end
end
end

View File

@ -9,21 +9,20 @@ PT.badges(::BasicTest) = PT.Badge("{{X}}", "{{Y}}", "{{Z}}")
PT.view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 0, "Y" => 2) PT.view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 0, "Y" => 2)
PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" => 3) PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" => 3)
@testset "BasicPlugin" begin @testset "Plugins" begin
p = BasicTest() @testset "BasicPlugin" begin
t = tpl(; plugins=[p]) p = BasicTest()
t = tpl(; plugins=[p])
# The X from user_view should override the X from view. # The X from user_view should override the X from view.
s = PT.render_plugin(p, t, "") s = PT.render_plugin(p, t, "")
@test occursin("1 2 3", first(split(s, "\n"))) @test occursin("1 2 3", first(split(s, "\n")))
with_pkg(t) do pkg with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg) pkg_dir = joinpath(t.dir, pkg)
badge = string(PT.Badge("1", "2", "3")) badge = string(PT.Badge("1", "2", "3"))
@test occursin(badge, read(joinpath(pkg_dir, "README.md"), String)) @test occursin(badge, read(joinpath(pkg_dir, "README.md"), String))
observed = read(joinpath(pkg_dir, "foo.txt"), String) @test read(joinpath(pkg_dir, "foo.txt"), String) == s
# On Travis, everything works with CRLF. end
Sys.iswindows() && (observed = replace(observed, "\r\n" => "\n"))
@test observed == s
end end
end end

View File

@ -17,13 +17,15 @@ function test_all(pkg::AbstractString; kwargs...)
end end
end end
@testset "Default package" begin @testset "Reference tests" begin
test_all("Basic"; authors=USER, manifest=true) @testset "Default package" begin
end test_all("Basic"; authors=USER, manifest=true)
end
@testset "All plugins" begin @testset "All plugins" begin
test_all("AllPlugins"; authors=USER, manifest=true, plugins=[ test_all("AllPlugins"; authors=USER, manifest=true, plugins=[
AppVeyor(), CirrusCI(), Citation(), Codecov(), AppVeyor(), CirrusCI(), Citation(), Codecov(),
Coveralls(), Documenter(), GitLabCI(), TravisCI(), Coveralls(), Documenter(), GitLabCI(), TravisCI(),
]) ])
end
end end

View File

@ -1,48 +1,59 @@
using Base.Filesystem: path_separator using Base.Filesystem: path_separator
using LibGit2: LibGit2, GitCommit, GitRemote, GitRepo
using Pkg: Pkg using Pkg: Pkg
using Random: Random using Random: Random
using Test: @test, @testset, @test_throws using Test: @test, @testset, @test_throws
using Cassette: Cassette, @context, @overdub
using ReferenceTests: @test_reference using ReferenceTests: @test_reference
using Suppressor: @suppress
using PkgTemplates using PkgTemplates
const PT = PkgTemplates const PT = PkgTemplates
const PKG = "TestPkg"
const USER = "tester" const USER = "tester"
Random.seed!(1) Random.seed!(1)
# Creata a template that won't error because of a missing username.
tpl(; kwargs...) = Template(; user=USER, kwargs...) tpl(; kwargs...) = Template(; user=USER, kwargs...)
function with_pkg(f::Function, t::Template, pkg::AbstractString=PKG) const pkg_name = Ref("A")
t(pkg)
# Generate an unused package name.
pkgname() = pkg_name[] *= "a"
# Create a randomly named package with a template, and delete it afterwards.
function with_pkg(f::Function, t::Template, pkg::AbstractString=pkgname())
@suppress t(pkg)
try try
f(pkg) f(pkg)
finally finally
haskey(Pkg.installed(), pkg) && Pkg.rm(pkg) haskey(Pkg.installed(), pkg) && @suppress Pkg.rm(pkg)
rm(joinpath(t.dir, pkg); recursive=true, force=true) rm(joinpath(t.dir, pkg); recursive=true, force=true)
end end
end end
@testset "PkgTemplates.jl" begin mktempdir() do dir
mktempdir() do dir Pkg.activate(dir)
Pkg.activate(dir) pushfirst!(DEPOT_PATH, dir)
pushfirst!(DEPOT_PATH, dir) try
try @testset "PkgTemplates.jl" begin
include("template.jl") include("template.jl")
include("plugin.jl") include("plugin.jl")
include("git.jl")
# Quite a bit of output depends on the Julia version, and the test fixtures are # Quite a bit of output depends on the Julia version,
# made with Julia 1.2. Also, Windows uses CRLF which breaks everything. # and the test fixtures are made with Julia 1.2.
if !Sys.iswindows() && VERSION.major == 1 && VERSION.minor == 2 # TODO: Keep this on the latest stable Julia version.
if VERSION.major == 1 && VERSION.minor == 2
include("reference.jl") include("reference.jl")
else else
@info "Skipping reference tests" julia=VERSION @info "Skipping reference tests" julia=VERSION
end end
finally
popfirst!(DEPOT_PATH)
end end
finally
popfirst!(DEPOT_PATH)
end end
end end

View File

@ -1,52 +1,77 @@
@testset "Template constructor" begin @testset "Template" begin
@testset "user" begin @testset "Template constructor" begin
if isempty(PT.default_user()) @testset "user" begin
@test_throws ArgumentError Template() if isempty(PT.default_user())
haskey(ENV, "CI") && run(`git config --global github.user $USER`) @test_throws ArgumentError Template()
end haskey(ENV, "CI") && run(`git config --global github.user $USER`)
@test Template().user == PT.default_user() end
end @test Template().user == PT.default_user()
@testset "authors" begin
@test tpl(; authors=["a"]).authors == ["a"]
@test tpl(; authors="a").authors == ["a"]
@test tpl(; authors="a,b").authors == ["a", "b"]
@test tpl(; authors="a, b").authors == ["a", "b"]
end
@testset "host" begin
@test tpl(; host="https://foo.com").host == "foo.com"
end
@testset "dir" begin
@test tpl(; dir="/foo/bar").dir == joinpath(path_separator, "foo", "bar")
@test tpl(; dir="foo").dir == abspath("foo")
@test tpl(; dir="~/foo").dir == abspath(expanduser("~/foo"))
end
@testset "plugins / disabled_defaults" begin
function test_plugins(plugins, expected, disabled=DataType[])
t = tpl(; plugins=plugins, disable_defaults=disabled)
@test issetequal(values(t.plugins), expected)
end end
defaults = PT.default_plugins() @testset "authors" begin
test_plugins([], defaults) @test tpl(; authors=["a"]).authors == ["a"]
test_plugins([Citation()], union(defaults, [Citation()])) @test tpl(; authors="a").authors == ["a"]
# Overriding a default plugin. @test tpl(; authors="a,b").authors == ["a", "b"]
gi = Gitignore(; dev=false) @test tpl(; authors="a, b").authors == ["a", "b"]
test_plugins([gi], union(setdiff(defaults, [Gitignore()]), [gi])) end
# Disabling a default plugin.
test_plugins([], setdiff(defaults, [Gitignore()]), [Gitignore]) @testset "host" begin
@test tpl(; host="https://foo.com").host == "foo.com"
end
@testset "dir" begin
@test tpl(; dir="/foo/bar").dir == joinpath(path_separator, "foo", "bar")
@test tpl(; dir="foo").dir == abspath("foo")
@test tpl(; dir="~/foo").dir == abspath(expanduser("~/foo"))
end
@testset "plugins / disabled_defaults" begin
function test_plugins(plugins, expected, disabled=DataType[])
t = tpl(; plugins=plugins, disable_defaults=disabled)
@test issetequal(values(t.plugins), expected)
end
defaults = PT.default_plugins()
test_plugins([], defaults)
test_plugins([Citation()], union(defaults, [Citation()]))
# Overriding a default plugin.
gi = Gitignore(; dev=false)
test_plugins([gi], union(setdiff(defaults, [Gitignore()]), [gi]))
# Disabling a default plugin.
test_plugins([], setdiff(defaults, [Gitignore()]), [Gitignore])
end
end
@testset "hasplugin" begin
t = tpl(; plugins=[Documenter{TravisCI}()])
@test PT.hasplugin(t, typeof(first(PT.default_plugins())))
@test PT.hasplugin(t, Documenter)
@test PT.hasplugin(t, _ -> true)
@test !PT.hasplugin(t, _ -> false)
@test !PT.hasplugin(t, Citation)
@test !PT.hasplugin(t, PT.is_ci)
end end
end end
@testset "hasplugin" begin @context ErrorOnRepoInit
t = tpl(; plugins=[Documenter{TravisCI}()]) function Cassette.prehook(::ErrorOnRepoInit, ::typeof(LibGit2.init), pkg_dir)
@test PT.hasplugin(t, typeof(first(PT.default_plugins()))) @test isdir(pkg_dir)
@test PT.hasplugin(t, Documenter) error()
@test PT.hasplugin(t, _ -> true) end
@test !PT.hasplugin(t, _ -> false)
@test !PT.hasplugin(t, Citation) @testset "Package generation errors" begin
@test !PT.hasplugin(t, PT.is_ci) mktempdir() do dir
t = tpl(; dir=dirname(dir))
@test_throws ArgumentError t(basename(dir))
end
mktemp() do f, _io
t = tpl(; dir=dirname(f))
@test_throws ArgumentError t(basename(f))
end
t = tpl()
pkg = pkgname()
@test_throws ErrorException @overdub ErrorOnRepoInit() @suppress t(pkg)
@test !isdir(joinpath(t.dir, pkg))
end end