diff --git a/.github/workflows/ReusableTest.yml b/.github/workflows/ReusableTest.yml index 3c0d6479..00eff2b1 100644 --- a/.github/workflows/ReusableTest.yml +++ b/.github/workflows/ReusableTest.yml @@ -41,6 +41,8 @@ jobs: uses: julia-actions/cache@v2 - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 + env: + TMPDIR: "${{ runner.temp }}" - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v4 with: diff --git a/src/COPIERTemplate.jl b/src/COPIERTemplate.jl index 2aa8e609..95add16d 100644 --- a/src/COPIERTemplate.jl +++ b/src/COPIERTemplate.jl @@ -1,3 +1,11 @@ +""" +# COPIERTemplate.jl + +This package defines a copier template for Julia packages and a basic user interface aroud copier +to use it from Julia. + +The main functions are: [`generate`](@ref) +""" module COPIERTemplate using PythonCall, CondaPkg, UUIDs @@ -7,24 +15,60 @@ function __init__() end """ - generate(dst_path; kwargs...) - generate(src_path, dst_path; kwargs...) + copy(dst_path[, data]; kwargs...) + copy(src_path, dst_path[, data]; kwargs...) -Runs the `copy` command of [copier](https://github.com/copier-org/copier) with the COPIERTemplate template. +Wrapper around Python's [copier.run_copy](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_copy). +Use [`generate`](@ref) for a more user-friendly function. +""" +function Base.copy(src_path, dst_path, data::Dict = Dict(); kwargs...) + copier = PythonCall.pyimport("copier") + copier.run_copy(src_path, dst_path, data; kwargs...) +end -If `src_path` is not informed, the GitHub URL of COPIERTemplate.jl is used. +function Base.copy(dst_path, data::Dict = Dict(); kwargs...) + copy(joinpath(@__DIR__, ".."), dst_path, data; kwargs...) +end -Even though the template is available offline through this template, this uses the github URL to allow updating. +""" + recopy(dst_path[, data]; kwargs...) -The keyword arguments are passed directly to the `run_copy` function of `copier`. +Wrapper around Python's [copier.run_recopy](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_recopy). """ -function generate(src_path, dst_path; kwargs...) +function recopy(dst_path, data::Dict = Dict(); kwargs...) copier = PythonCall.pyimport("copier") - copier.run_copy(src_path, dst_path; kwargs..., vcs_ref = "HEAD") + copier.run_recopy(dst_path, data; kwargs...) +end + +""" + update(dst_path[, data]; kwargs...) + +Wrapper around Python's [copier.run_update](https://copier.readthedocs.io/en/stable/reference/main/#copier.main.run_update). +""" +function update(dst_path, data::Dict = Dict(); kwargs...) + copier = PythonCall.pyimport("copier") + copier.run_update(dst_path, data; kwargs...) +end + +""" + generate(dst_path[, data]; kwargs...) + generate(src_path, dst_path[, data]; true, kwargs...) + +Runs the `copy` command of [copier](https://github.com/copier-org/copier) with the COPIERTemplate template. +If `src_path` is not informed, the GitHub URL of COPIERTemplate.jl is used. + +The `data` argument is a dictionary of answers (values) to questions (keys) that can be used to bypass some of the interactive questions. + +## Keyword arguments + +The keyword arguments are passed directly to [`copy`](@ref). +""" +function generate(src_path, dst_path, data::Dict = Dict(); kwargs...) + copy(src_path, dst_path, data; kwargs...) end -function generate(dst_path, args...; kwargs...) - generate("https://github.com/abelsiqueira/COPIERTemplate.jl", dst_path, args...; kwargs...) +function generate(dst_path, data::Dict = Dict(); kwargs...) + generate("https://github.com/abelsiqueira/COPIERTemplate.jl", dst_path, data; kwargs...) end end diff --git a/test/runtests.jl b/test/runtests.jl index bacf069e..2a0a8454 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,14 +1,30 @@ -# This is only useful for testing offline. It creates a local env to avoid redownloading things. +# Can't use mktempdir on GitHub actions willy nilly (at least on Mac) +TMPDIR = get(ENV, "TMPDIR", mktempdir()) + if get(ENV, "CI", "nothing") == "nothing" + # This is only useful for testing offline. It creates a local env to avoid redownloading things. ENV["JULIA_CONDAPKG_ENV"] = joinpath(@__DIR__, "conda-env") if isdir(ENV["JULIA_CONDAPKG_ENV"]) ENV["JULIA_CONDAPKG_OFFLINE"] = true end + + # Mac is a complicated beast + if Sys.isapple() + TMPDIR = "/private$TMPDIR" + end end using COPIERTemplate using Test +template_minimum_options = Dict( + "PackageName" => "Tmp", + "PackageUUID" => "1234", + "PackageOwner" => "test", + "AuthorName" => "Test", + "AuthorEmail" => "test@me.now", +) + template_options = Dict( "PackageName" => "Tmp", "PackageUUID" => "1234", @@ -51,35 +67,36 @@ function test_diff_dir(dir1, dir2) end end +min_bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_minimum_options]...) bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_options]...) template_path = joinpath(@__DIR__, "..") template_url = "https://github.com/abelsiqueira/COPIERTemplate.jl" +# This is a hack because Windows managed to dirty the repo. +if get(ENV, "CI", "nothing") == "true" && Sys.iswindows() + run(`git reset --hard HEAD`) +end + @testset "Compare COPIERTemplate.generate vs copier CLI on URL/main" begin - mktempdir(; prefix = "cli_") do dir_copier_cli + mktempdir(TMPDIR; prefix = "cli_") do dir_copier_cli run(`copier copy --vcs-ref main --quiet $bash_args $template_url $dir_copier_cli`) - mktempdir(; prefix = "copy_") do tmpdir - COPIERTemplate.generate(tmpdir; data = template_options, quiet = true, vcs_ref = "main") + mktempdir(TMPDIR; prefix = "copy_") do tmpdir + COPIERTemplate.generate(tmpdir, template_options; quiet = true, vcs_ref = "main") test_diff_dir(tmpdir, dir_copier_cli) end end end @testset "Compare COPIERTemplate.generate vs copier CLI on HEAD" begin - # This is a hack because Windows managed to dirty the repo. - if get(ENV, "CI", "nothing") == "true" && Sys.iswindows() - run(`git reset --hard HEAD`) - end - - mktempdir(; prefix = "cli_") do dir_copier_cli + mktempdir(TMPDIR; prefix = "cli_") do dir_copier_cli run(`copier copy --vcs-ref HEAD --quiet $bash_args $template_path $dir_copier_cli`) - mktempdir(; prefix = "copy_") do tmpdir + mktempdir(TMPDIR; prefix = "copy_") do tmpdir COPIERTemplate.generate( template_path, - tmpdir; - data = template_options, + tmpdir, + template_options; quiet = true, vcs_ref = "HEAD", ) @@ -87,3 +104,39 @@ end end end end + +@testset "Testing copy, recopy and rebase" begin + mktempdir(TMPDIR; prefix = "cli_") do dir_copier_cli + run(`copier copy --quiet $bash_args $template_path $dir_copier_cli`) + + @testset "Compare copied project vs copier CLI baseline" begin + mktempdir(TMPDIR; prefix = "copy_") do tmpdir + COPIERTemplate.copy(tmpdir, template_options; quiet = true) + test_diff_dir(tmpdir, dir_copier_cli) + end + end + + @testset "Compare recopied project vs copier CLI baseline" begin + mktempdir(TMPDIR; prefix = "recopy_") do tmpdir + run(`copier copy --defaults --quiet $min_bash_args $template_path $tmpdir`) + COPIERTemplate.recopy(tmpdir, template_options; quiet = true, overwrite = true) + test_diff_dir(tmpdir, dir_copier_cli) + end + end + + @testset "Compare updated project vs copier CLI baseline" begin + mktempdir(TMPDIR; prefix = "update_") do tmpdir + run(`copier copy --defaults --quiet $min_bash_args $template_path $tmpdir`) + cd(tmpdir) do + run(`git init`) + run(`git add .`) + run(`git config user.name "Test"`) + run(`git config user.email "test@test.com"`) + run(`git commit -q -m "First commit"`) + end + COPIERTemplate.update(tmpdir, template_options; overwrite = true, quiet = true) + test_diff_dir(tmpdir, dir_copier_cli) + end + end + end +end