Skip to content

Commit

Permalink
Implement debugging tools to help developers
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira committed Aug 6, 2024
1 parent b1114d0 commit 2466e25
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 26 deletions.
36 changes: 21 additions & 15 deletions src/debug/Data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
117 changes: 117 additions & 0 deletions src/debug/Debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check warning on line 21 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L14-L21

Added lines #L14 - L21 were not covered by tests
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

Check warning on line 55 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L39-L55

Added lines #L39 - L55 were not covered by tests
else
error("Unexpected $data_choice")

Check warning on line 57 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L57

Added line #L57 was not covered by tests
end
data = merge(data, _data)

Check warning on line 59 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L59

Added line #L59 was not covered by tests
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(

Check warning on line 79 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L79

Added line #L79 was not covered by tests
dst_path = rand_pkg_name(),
_data = Dict();
data_choice::Symbol = :minimum,
kwargs...,
)
data = dbg_data(data_choice, _data)
BestieTemplate.generate(

Check warning on line 86 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L85-L86

Added lines #L85 - L86 were not covered by tests
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(

Check warning on line 117 in src/debug/Debug.jl

View check run for this annotation

Codecov / codecov/patch

src/debug/Debug.jl#L115-L117

Added lines #L115 - L117 were not covered by tests
pkgdir(BestieTemplate),
dst_path,
data;
defaults = true,
overwrite = true,
quiet = true,
vcs_ref = "HEAD",
kwargs...,
)
end

end
28 changes: 17 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -83,7 +83,7 @@ end
BestieTemplate.generate(
template_path,
tmpdir,
Data.strategy_ask;
Data.strategy_ask_default;
quiet = true,
vcs_ref = "HEAD",
)
Expand All @@ -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)
Expand Down Expand Up @@ -141,7 +141,7 @@ end
BestieTemplate.generate(
template_path,
".",
Data.strategy_ask;
Data.strategy_ask_default;
quiet = true,
vcs_ref = "HEAD",
)
Expand All @@ -154,7 +154,7 @@ end
BestieTemplate.generate(
template_path,
"some_folder3",
Data.strategy_ask;
Data.strategy_ask_default;
quiet = true,
vcs_ref = "HEAD",
)
Expand All @@ -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
Expand All @@ -181,7 +186,7 @@ end
)
BestieTemplate.Copier.recopy(
tmpdir,
Data.strategy_ask;
Data.strategy_ask_default;
quiet = true,
overwrite = true,
vcs_ref = "HEAD",
Expand All @@ -202,7 +207,7 @@ end
end
BestieTemplate.Copier.update(
tmpdir,
Data.strategy_ask;
Data.strategy_ask_default;
overwrite = true,
quiet = true,
vcs_ref = "HEAD",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2466e25

Please sign in to comment.