From 2466e25f47f131684f5695758659e661ee5cd4d9 Mon Sep 17 00:00:00 2001 From: Abel Soares Siqueira Date: Sun, 4 Aug 2024 14:55:19 +0200 Subject: [PATCH] Implement debugging tools to help developers --- src/debug/Data.jl | 36 ++++++++------ src/debug/Debug.jl | 117 +++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 28 ++++++----- 3 files changed, 155 insertions(+), 26 deletions(-) diff --git a/src/debug/Data.jl b/src/debug/Data.jl index 469559a9..f46068ba 100644 --- a/src/debug/Data.jl +++ b/src/debug/Data.jl @@ -32,21 +32,27 @@ const strategy_minimum = merge( const strategy_recommended = merge(strategy_minimum, Dict("AnswerStrategy" => "recommended")) -const strategy_ask = merge( - strategy_recommended, - Dict( - "AnswerStrategy" => "ask", - "AddPrecommit" => true, - "AddMacToCI" => true, - "AddWinToCI" => true, - "RunJuliaNightlyOnCI" => true, - "UseCirrusCI" => false, - "AddCopierCI" => false, - "AddContributionDocs" => true, - "AddAllcontributors" => true, - "AddCodeOfConduct" => true, - "AddGitHubTemplates" => true, - ), +const strategy_ask = merge(strategy_recommended, Dict("AnswerStrategy" => "ask")) + +const optional_questions_with_default = Dict( + "AddPrecommit" => true, + "AddMacToCI" => true, + "AddWinToCI" => true, + "RunJuliaNightlyOnCI" => true, + "UseCirrusCI" => false, + "AddCopierCI" => false, + "AddContributionDocs" => true, + "AddAllcontributors" => true, + "AddCodeOfConduct" => true, + "AddGitHubTemplates" => true, ) +const strategy_ask_default = merge(strategy_ask, optional_questions_with_default) + +const strategy_ask_and_say_no = + merge(strategy_recommended, Dict(k => false for k in keys(optional_questions_with_default))) + +const strategy_ask_and_say_yes = + merge(strategy_recommended, Dict(k => true for k in keys(optional_questions_with_default))) + end diff --git a/src/debug/Debug.jl b/src/debug/Debug.jl index fb008058..16fd87a8 100644 --- a/src/debug/Debug.jl +++ b/src/debug/Debug.jl @@ -9,4 +9,121 @@ module Debug include("Data.jl") +import ..BestieTemplate: BestieTemplate, generate, apply + +function rand_pkg_name() + prob_done = 0.0 + pkg_name = "PkgDebugBestie" + n = 1 + while isdir("$(pkg_name)$n") + n += 1 + end + return "$(pkg_name)$n" +end + +""" + dbg_data(data_choice, more_data) + +Returns the fake debug data merged with `more_data`. +The options for `data_choice` are: + +- `:nothing`, `:none`: No random data. +- `:required`, `:req`: Only the required data is generated. You get asked which choice of optional data. +- `:minimum`, `:min`: The required data plus the choice "minimum" for optional data. +- `:recommended`, `:rec`: The required data plus the choice "recommended" for optional data. +- `:ask`: The required data plus the choice "ask". The optional questions will be asked. +- `:ask_default`: Same as `:ask` plus the default answers for the optional questions. +- `:ask_and_say_no`: Same as `:ask` plus answers no/false to the optional questions. +- `:ask_and_say_yes`: Same as `:ask` plus answers yes/true to the optional questions. +""" +function dbg_data(data_choice, _data = Dict()) + data = if data_choice in [:nothing, :none] + Dict() + elseif data_choice in [:required, :req] + Data.minimum_defaults + elseif data_choice in [:minimum, :min] + Data.strategy_minimum + elseif data_choice in [:recommended, :rec] + Data.strategy_recommended + elseif data_choice in [:ask] + Data.strategy_ask + elseif data_choice in [:ask_default] + Data.strategy_ask_default + elseif data_choice in [:ask_and_say_no] + Data.strategy_ask_and_say_no + elseif data_choice in [:ask_and_say_yes] + Data.strategy_ask_and_say_yes + else + error("Unexpected $data_choice") + end + data = merge(data, _data) +end + +""" + dbg_generate([dst_path, data]; data_choice=:minimum) + +Convenience function to help debug `generate`. +It runs `generate` with the `dst_path` destination (random by default) and the given `data` +(nothing by default). + +It also uses a `data_choice` to determine fake starting data. This is passed to +[`dbg_data`](@ref). + +It uses the `pkgdir` location of Bestie and adds the flags + +- `defaults = true`: Sent to copier to use the default answers. +- `quiet = true`: Low verbosity. +- `vcs_ref = HEAD`: Use the development version of the template, including dirty repo + changes. +""" +function dbg_generate( + dst_path = rand_pkg_name(), + _data = Dict(); + data_choice::Symbol = :minimum, + kwargs..., +) + data = dbg_data(data_choice, _data) + BestieTemplate.generate( + pkgdir(BestieTemplate), + dst_path, + data; + defaults = true, + quiet = true, + vcs_ref = "HEAD", + kwargs..., + ) +end + +""" + dbg_apply([dst_path, data]; data_choice=:minimum) + +Convenience function to help debug `apply`. +It runs `apply` with the `dst_path` destination and the given `data` +(nothing by default). + +It also uses a `data_choice` to determine fake starting data. This is passed to +[`dbg_data`](@ref). + +It uses the `pkgdir` location of Bestie and adds the flags + +- `defaults = true`: Sent to copier to use the default answers. +- `quiet = true`: Low verbosity. +- `overwrite = true`: Overwrite all files. +- `vcs_ref = HEAD`: Use the development version of the template, including dirty repo + changes. +""" +function dbg_apply(dst_path, _data = Dict(); data_choice::Symbol = :minimum, kwargs...) + data = dbg_data(data_choice, _data) + BestieTemplate.apply( + pkgdir(BestieTemplate), + dst_path, + data; + defaults = true, + overwrite = true, + quiet = true, + vcs_ref = "HEAD", + kwargs..., + ) +end + end diff --git a/test/runtests.jl b/test/runtests.jl index f88cf053..f1054665 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -54,7 +54,7 @@ end data_to_cli_args(dict) = vcat([["-d"; "$k=$v"] for (k, v) in dict]...) cli_args_min_defaults = data_to_cli_args(Data.minimum_defaults) -cli_args_strat_ask = data_to_cli_args(Data.strategy_ask) +cli_args_strat_ask = data_to_cli_args(Data.strategy_ask_default) template_path = joinpath(@__DIR__, "..") template_url = "https://github.com/abelsiqueira/BestieTemplate.jl" @@ -69,7 +69,7 @@ end run(`copier copy --vcs-ref main --quiet $cli_args_strat_ask $template_url $dir_copier_cli`) mktempdir(TMPDIR; prefix = "copy_") do tmpdir - BestieTemplate.generate(tmpdir, Data.strategy_ask; quiet = true, vcs_ref = "main") + BestieTemplate.generate(tmpdir, Data.strategy_ask_default; quiet = true, vcs_ref = "main") test_diff_dir(tmpdir, dir_copier_cli) end end @@ -83,7 +83,7 @@ end BestieTemplate.generate( template_path, tmpdir, - Data.strategy_ask; + Data.strategy_ask_default; quiet = true, vcs_ref = "HEAD", ) @@ -104,7 +104,7 @@ end BestieTemplate.generate(tmpdir, Data.minimum_defaults; defaults = true, quiet = true) cd(tmpdir) do _git_setup() - BestieTemplate.update(Data.strategy_ask; defaults = true, quiet = true) + BestieTemplate.update(Data.strategy_ask_default; defaults = true, quiet = true) end test_diff_dir(tmpdir, dir_copier_cli) @@ -141,7 +141,7 @@ end BestieTemplate.generate( template_path, ".", - Data.strategy_ask; + Data.strategy_ask_default; quiet = true, vcs_ref = "HEAD", ) @@ -154,7 +154,7 @@ end BestieTemplate.generate( template_path, "some_folder3", - Data.strategy_ask; + Data.strategy_ask_default; quiet = true, vcs_ref = "HEAD", ) @@ -169,7 +169,12 @@ end @testset "Compare copied project vs copier CLI baseline" begin mktempdir(TMPDIR; prefix = "copy_") do tmpdir - BestieTemplate.Copier.copy(tmpdir, Data.strategy_ask; quiet = true, vcs_ref = "HEAD") + BestieTemplate.Copier.copy( + tmpdir, + Data.strategy_ask_default; + quiet = true, + vcs_ref = "HEAD", + ) test_diff_dir(tmpdir, dir_copier_cli) end end @@ -181,7 +186,7 @@ end ) BestieTemplate.Copier.recopy( tmpdir, - Data.strategy_ask; + Data.strategy_ask_default; quiet = true, overwrite = true, vcs_ref = "HEAD", @@ -202,7 +207,7 @@ end end BestieTemplate.Copier.update( tmpdir, - Data.strategy_ask; + Data.strategy_ask_default; overwrite = true, quiet = true, vcs_ref = "HEAD", @@ -239,7 +244,8 @@ end @testset "Test automatic guessing the package name from the path" begin mktempdir(TMPDIR; prefix = "path_is_dir_") do dir_path_is_dir cd(dir_path_is_dir) do - data = Dict(key => value for (key, value) in Data.strategy_ask if key != "PackageName") + data = + Dict(key => value for (key, value) in Data.strategy_ask_default if key != "PackageName") mkdir("some_folder") BestieTemplate.generate( template_path, @@ -267,7 +273,7 @@ end mktempdir(TMPDIR; prefix = "valid_pkg_name_") do dir cd(dir) do for name in ["Bad.jl", "0Bad", "bad"] - data = copy(Data.strategy_ask) + data = copy(Data.strategy_ask_default) data["PackageName"] = name @test_throws PythonCall.Core.PyException BestieTemplate.generate( template_path,