diff --git a/stdlib/Pkg/test/repl.jl b/stdlib/Pkg/test/repl.jl index 7c7eaba21041d..8b1863ff47499 100644 --- a/stdlib/Pkg/test/repl.jl +++ b/stdlib/Pkg/test/repl.jl @@ -29,7 +29,7 @@ end end temp_pkg_dir() do project_path - cd(project_path) do + with_pkg_env(project_path; change_dir=true) do; withenv("USER" => "Test User") do pkg"generate HelloWorld" LibGit2.close((LibGit2.init("."))) diff --git a/stdlib/Pkg/test/utils.jl b/stdlib/Pkg/test/utils.jl index 9edb795da7ed0..c54a42aa0e123 100644 --- a/stdlib/Pkg/test/utils.jl +++ b/stdlib/Pkg/test/utils.jl @@ -69,3 +69,16 @@ function with_temp_env(f, env_name::AbstractString="Dummy") Pkg.activate() end end + +function with_pkg_env(fn::Function, path::AbstractString="."; change_dir=false) + Pkg.activate(path) + try + if change_dir + cd(fn, path) + else + fn() + end + finally + Pkg.activate() + end +end