From 0fcbaa83bf0514377cc65f37a66ad9b2272ee4d9 Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Thu, 26 Sep 2019 13:22:20 +0700 Subject: [PATCH] Add compat with Julia 1.4 --- src/plugins/git.jl | 19 +++++++++++++------ test/git.jl | 2 +- test/runtests.jl | 6 ++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/plugins/git.jl b/src/plugins/git.jl index 94a01bd..06e93f6 100644 --- a/src/plugins/git.jl +++ b/src/plugins/git.jl @@ -80,12 +80,8 @@ function posthook(p::Git, ::Template, pkg_dir::AbstractString) LibGit2.with(GitRepo(pkg_dir)) do repo LibGit2.add!(repo, ".") msg = "Files generated by PkgTemplates" - # TODO: Newer versions of Julia will not have Pkg.installed. - installed = Pkg.installed() - if haskey(installed, "PkgTemplates") - ver = string(installed["PkgTemplates"]) - msg *= "\n\nPkgTemplates version: $ver" - end + v = version_of("PkgTemplates") + v === nothing || (msg *= "\n\nPkgTemplates version: $v") commit(p, repo, pkg_dir, msg) end end @@ -99,3 +95,14 @@ function commit(p::Git, repo::GitRepo, pkg_dir::AbstractString, msg::AbstractStr end needs_username(::Git) = true + +if isdefined(Pkg, :dependencies) + function version_of(pkg::AbstractString) + for p in values(Pkg.dependencies()) + p.name == pkg && return p.version + end + return nothing + end +else + version_of(pkg::AbstractString) = get(Pkg.installed(), pkg, nothing) +end diff --git a/test/git.jl b/test/git.jl index d87870d..25ac319 100644 --- a/test/git.jl +++ b/test/git.jl @@ -38,7 +38,7 @@ @testset "Adds version to commit message" begin # We're careful to avoid a Pkg.update as it triggers Cassette#130. t = tpl(; disable_defaults=[Tests], plugins=[Git()]) - mock(Pkg.installed => () -> Dict("PkgTemplates" => v"1.2.3")) do _pi + mock(PT.version_of => _p -> v"1.2.3") do _i with_pkg(t) do pkg pkg_dir = joinpath(t.dir, pkg) LibGit2.with(GitRepo(pkg_dir)) do repo diff --git a/test/runtests.jl b/test/runtests.jl index 6c93fb1..dc369b8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,7 @@ using Random: Random using Test: @test, @testset, @test_throws using ReferenceTests: @test_reference -using SimpleMock: Mock, mock +using SimpleMock: mock using Suppressor: @suppress using PkgTemplates @@ -30,7 +30,9 @@ function with_pkg(f::Function, t::Template, pkg::AbstractString=pkgname()) try f(pkg) finally - haskey(Pkg.installed(), pkg) && @suppress Pkg.rm(pkg) + # On 1.4, this sometimes won't work, but the error is that the package isn't installed. + # We're going to delete the package directory anyways, so just ignore any errors. + PT.version_of(pkg) === nothing || try @suppress Pkg.rm(pkg) catch; end rm(joinpath(t.dir, pkg); recursive=true, force=true) end end