Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Create safeguard when running generate in existing pkg #249

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/COPIERTemplate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ The `data` argument is a dictionary of answers (values) to questions (keys) that

## Keyword arguments

The keyword arguments are passed directly to the internal [`Copier.copy`](@ref).
- `warn_existing_pkg::Boolean = true`: Whether to check if you actually meant `update`. If you run `generate` and the `dst_path` contains a `.copier-answers.yml`, it means that the copy was already made, so you might have means `update` instead. When `true`, a warning is shown and execution is stopped.

The other keyword arguments are passed directly to the internal [`Copier.copy`](@ref).
"""
function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
function generate(src_path, dst_path, data::Dict = Dict(); warn_existing_pkg = true, kwargs...)
if warn_existing_pkg && isfile(joinpath(dst_path, ".copier-answers.yml"))
@warn """There already exists a `.copier-answers.yml` file in the destination path.
You might have meant to use `COPIERTemplate.update` instead, which only fetches the non-applying updates.
If you really meant to use this command, then pass the `warn_existing_pkg = false` flag to this call.
"""

return nothing
end

# If there are answers in the destionation path, skip guessing the answers
if !isfile(joinpath(dst_path, ".copier-answers")) && isdir(dst_path)
existing_data = _read_data_from_existing_path(dst_path)
Expand All @@ -46,6 +57,8 @@ function generate(src_path, dst_path, data::Dict = Dict(); kwargs...)
end
end
Copier.copy(src_path, dst_path, data; kwargs...)

return nothing
end

function generate(dst_path, data::Dict = Dict(); kwargs...)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ end
end
end

@testset "Test that COPIERTemplate.generate warns and exits for existing copy" begin
mktempdir(TMPDIR; prefix = "cli_") do dir_copier_cli
run(`copier copy --quiet $bash_args $template_url $dir_copier_cli`)

@test_logs (:warn,) COPIERTemplate.generate(dir_copier_cli; quiet = true)
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`)
Expand Down
Loading