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"
[extras]
Cassette = "7057c7e9-c182-5462-911a-8362d720325c"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[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)
endswith(pkg, ".jl") && (pkg = pkg[1:end-3])
pkg_dir = joinpath(t.dir, pkg)
ispath(pkg_dir) && throw(ArgumentError("$pkg_dir already exists"))
repo = nothing
try
# 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.
repo = LibGit2.init(pkg_dir)
LibGit2.commit(repo, "Initial commit")
rmt = if t.ssh
url = if t.ssh
"git@$(t.host):$(t.user)/$pkg.jl.git"
else
"https://$(t.host)/$(t.user)/$pkg.jl"
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
# Generate the files.
@ -56,6 +61,8 @@ function (t::Template)(pkg::AbstractString)
catch
rm(pkg_dir; recursive=true, force=true)
rethrow()
finally
repo === nothing || close(repo)
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,6 +9,7 @@ PT.badges(::BasicTest) = PT.Badge("{{X}}", "{{Y}}", "{{Z}}")
PT.view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 0, "Y" => 2)
PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" => 3)
@testset "Plugins" begin
@testset "BasicPlugin" begin
p = BasicTest()
t = tpl(; plugins=[p])
@ -21,9 +22,7 @@ PT.user_view(::BasicTest, ::Template, ::AbstractString) = Dict("X" => 1, "Z" =>
pkg_dir = joinpath(t.dir, pkg)
badge = string(PT.Badge("1", "2", "3"))
@test occursin(badge, read(joinpath(pkg_dir, "README.md"), String))
observed = read(joinpath(pkg_dir, "foo.txt"), String)
# On Travis, everything works with CRLF.
Sys.iswindows() && (observed = replace(observed, "\r\n" => "\n"))
@test observed == s
@test read(joinpath(pkg_dir, "foo.txt"), String) == s
end
end
end

View File

@ -17,6 +17,7 @@ function test_all(pkg::AbstractString; kwargs...)
end
end
@testset "Reference tests" begin
@testset "Default package" begin
test_all("Basic"; authors=USER, manifest=true)
end
@ -27,3 +28,4 @@ end
Coveralls(), Documenter(), GitLabCI(), TravisCI(),
])
end
end

View File

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

View File

@ -1,3 +1,4 @@
@testset "Template" begin
@testset "Template constructor" begin
@testset "user" begin
if isempty(PT.default_user())
@ -50,3 +51,27 @@ end
@test !PT.hasplugin(t, Citation)
@test !PT.hasplugin(t, PT.is_ci)
end
end
@context ErrorOnRepoInit
function Cassette.prehook(::ErrorOnRepoInit, ::typeof(LibGit2.init), pkg_dir)
@test isdir(pkg_dir)
error()
end
@testset "Package generation errors" begin
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