Skip to content

Commit

Permalink
Merge pull request #297 from invenia/mjr/mocking
Browse files Browse the repository at this point in the history
Use Mocking instead of SimpleMock
  • Loading branch information
mjram0s authored Jun 17, 2021
2 parents 4302ecb + 7f6882d commit bcea5d2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
name = "PkgTemplates"
uuid = "14b8a8f1-9102-5b29-a752-f990bacb7fe1"
authors = ["Chris de Graaf", "Invenia Technical Computing Corporation"]
version = "0.7.16"
version = "0.7.17"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
Mocking = "78c3b35d-d492-501b-9361-3d52fe80e533"
Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
Mocking = "0.7"
Mustache = "1"
Parameters = "0.12"
julia = "1"

[extras]
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SimpleMock = "a896ed2c-15a5-4479-b61d-a0e88e2a1d25"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DeepDiffs", "Random", "Suppressor", "SimpleMock", "Test"]
test = ["DeepDiffs", "Random", "Suppressor", "Test"]
2 changes: 2 additions & 0 deletions src/PkgTemplates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using UUIDs: uuid4
using Mustache: render
using Parameters: @with_kw_noshow

using Mocking

export
Template,
AppVeyor,
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ function validate(p::Git, t::Template)

foreach((:name, :email)) do k
user_k = "user.$k"
if getproperty(p, k) === nothing && isempty(LibGit2.getconfig(user_k, ""))
if getproperty(p, k) === nothing && isempty(@mock LibGit2.getconfig(user_k, ""))
throw(ArgumentError("Git: Global Git config is missing required value '$user_k'"))
end
end
end

# Set up the Git repository.
function prehook(p::Git, t::Template, pkg_dir::AbstractString)
LibGit2.with(LibGit2.init(pkg_dir)) do repo
LibGit2.with(@mock LibGit2.init(pkg_dir)) do repo
LibGit2.with(GitConfig(repo)) do config
foreach((:name, :email)) do k
v = getproperty(p, k)
Expand Down Expand Up @@ -108,7 +108,7 @@ function posthook(p::Git, ::Template, pkg_dir::AbstractString)
LibGit2.with(GitRepo(pkg_dir)) do repo
LibGit2.add!(repo, ".")
msg = "Files generated by PkgTemplates"
v = version_of("PkgTemplates")
v = @mock version_of("PkgTemplates")
v === nothing || (msg *= "\n\nPkgTemplates version: $v")
# TODO: Put the template config in the message too?
commit(p, repo, pkg_dir, msg)
Expand Down
4 changes: 2 additions & 2 deletions src/template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Template(::Val{true}; kwargs...) = interactive(Template; kwargs...)
function Template(::Val{false}; kwargs...)
kwargs = Dict(kwargs)

user = getkw!(kwargs, :user)
user = @mock getkw!(kwargs, :user)
dir = abspath(expanduser(getkw!(kwargs, :dir)))
host = replace(getkw!(kwargs, :host), r".*://" => "")
julia = getkw!(kwargs, :julia)
Expand Down Expand Up @@ -216,7 +216,7 @@ end
prompt(::Type{Template}, ::Type, ::Val{:pkg}) = Base.prompt("Package name")

function prompt(::Type{Template}, ::Type, ::Val{:user})
return if isempty(default_user())
return if isempty(@mock default_user())
input = Base.prompt("Enter value for 'user' (String, required)")
input === nothing && throw(InterruptException())
return input
Expand Down
4 changes: 3 additions & 1 deletion test/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
@testset "Adds version to commit message" begin
# We're careful to avoid a Pkg.update as it triggers Cassette#130.
t = tpl(; plugins=[Git(), !Tests])
mock(PT.version_of => _p -> v"1.2.3") do _i

patch = @patch PkgTemplates.version_of(t) = v"1.2.3"
apply(patch) do
with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)
LibGit2.with(GitRepo(pkg_dir)) do repo
Expand Down
4 changes: 3 additions & 1 deletion test/interactive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ end
DONE, # Customize nothing
"username", LF, # Enter user after it's prompted
)
mock(PT.default_user => () -> "") do _du

patch = @patch PkgTemplates.default_user() = ""
apply(patch) do
@test Template(; interactive=true) == Template(; user="username")
end
end
Expand Down
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ using Random: Random, randstring
using Test: @test, @testset, @test_broken, @test_logs, @test_throws

using DeepDiffs: deepdiff
using SimpleMock: mock
using Mocking
using Suppressor: @suppress

using PkgTemplates
const PT = PkgTemplates

Mocking.activate()

const PT = PkgTemplates
const USER = "tester"

Random.seed!(1)
Expand Down
16 changes: 12 additions & 4 deletions test/template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
@testset "user" begin
msg = sprint(showerror, PT.MissingUserException{TravisCI}())
@test startswith(msg, "TravisCI: ")
mock(PT.default_user => () -> "") do _du

patch = @patch PkgTemplates.getkw!(kwargs, k) = ""
apply(patch) do
@test_throws PT.MissingUserException Template()
@test isempty(Template(; plugins=[!Git]).user)
end
mock(PT.default_user => () -> "username") do _du

patch = @patch PkgTemplates.getkw!(kwargs, k) = "username"
apply(patch) do
@test Template().user == "username"
end
end
Expand Down Expand Up @@ -76,7 +80,9 @@
foreach((GitHubActions, TravisCI, GitLabCI)) do T
@test_throws ArgumentError tpl(; plugins=[Documenter{T}()])
end
mock(LibGit2.getconfig => (_k, _d) -> "") do _gc

patch = @patch LibGit2.getconfig(r, n) = ""
apply(patch) do
@test_throws ArgumentError tpl(; plugins=[Git()])
end
end
Expand All @@ -95,7 +101,9 @@ end

t = tpl()
pkg = pkgname()
mock(LibGit2.init => dir -> (@test isdir(dir); error())) do _init

patch = @patch LibGit2.init(pkg_dir) = error()
apply(patch) do
@test_throws ErrorException @suppress t(pkg)
end
@test !isdir(joinpath(t.dir, pkg))
Expand Down

2 comments on commit bcea5d2

@mjram0s
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/39070

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.17 -m "<description of version>" bcea5d227e563812adefd5642cf4f949cf805f6a
git push origin v0.7.17

Please sign in to comment.