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

refactor: 💡 Improve folder comparison in tests #141

Merged
merged 1 commit into from
May 29, 2024
Merged
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
51 changes: 32 additions & 19 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,44 @@ template_options = Dict(
"UseCirrusCI" => true,
)

@testset "Compare folder generated by this call vs direct copier" begin
tmpdir1 = mktempdir()
tmpdir2 = mktempdir()
function test_diff_dir(dir1, dir2)
ignore(line) = startswith("_commit")(line) || startswith("_src_path")(line)
@testset "$(basename(dir1)) vs $(basename(dir2))" begin
for (root, _, files) in walkdir(dir1)
nice_dir(file) =
replace(root, dir1 => "") |> out -> replace(out, r"^/" => "") |> out -> joinpath(out, file)
if nice_dir("") |> startswith(".git")
continue
end
@testset "File $(nice_dir(file))" for file in files
file1 = joinpath(root, file)
file2 = replace(file1, dir1 => dir2)
lines1 = readlines(file1)
lines2 = readlines(file2)
for (line1, line2) in zip(lines1, lines2)
ignore(line1) && continue
@test line1 == line2
end
end
end
end
end

bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_options]...)
template_path = joinpath(@__DIR__, "..")

@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

COPIERTemplate.generate(tmpdir1; data = template_options, vcs_ref = "HEAD")
bash_args = vcat([["-d"; "$k=$v"] for (k, v) in template_options]...)
ignore(line) = startswith("_commit")(line) || startswith("_src_path")(line)
template_path = joinpath(@__DIR__, "..")
run(`copier copy --vcs-ref HEAD $bash_args $template_path $tmpdir2`)
for (root, dirs, files) in walkdir(tmpdir1)
for file in files
file1 = joinpath(root, file)
file2 = replace(file1, tmpdir1 => tmpdir2)
lines1 = readlines(file1)
lines2 = readlines(file2)
diff = [
"$line1 vs $line2" for
(line1, line2) in zip(lines1, lines2) if !ignore(line1) && line1 != line2
]
@test diff == []
mktempdir(; 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
COPIERTemplate.generate(tmpdir; data = template_options, quiet = true, vcs_ref = "HEAD")
test_diff_dir(tmpdir, dir_copier_cli)
end
end
end
Loading