Skip to content

Commit

Permalink
Use mktempdir for every temporary directory in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaipartmann committed Feb 7, 2025
1 parent ca4ec19 commit 2482927
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 113 deletions.
4 changes: 1 addition & 3 deletions src/auxiliary/precompile_workload.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msg *= "Trigger package precompilation manually and then restart the mpirun!\n"
error(msg)
end
root = joinpath(@__DIR__, "temp_precompilation")
root = mktempdir()
pos1, vol1 = uniform_box(1, 1, 1, 0.5; center=(0.5, 0.5, 0.5))
pos2, vol2 = uniform_box(1, 1, 1, 0.5; center=(-0.5, 0.5, 0.5))
path_bb = joinpath(root, "BB")
Expand Down Expand Up @@ -68,6 +68,4 @@
submit(Job(b3, vv; path=path_cc, freq=1); quiet=true)
submit(Job(ms, vv; path=path_ms, freq=1); quiet=true)
end

rm(root; recursive=true, force=true)
end
48 changes: 14 additions & 34 deletions test/VtkReader/test_read_vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
@testitem "read complete results" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_1")
root = mktempdir()
bname = joinpath(root, "vtk_test_1")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -26,17 +26,15 @@
@test result[:Time] == [time]
@test result[:Damage] == damage
@test result[:Displacement] == displacement

rm(name)
end

#-- read incomplete results
@testitem "read incomplete results" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_2")
root = mktempdir()
bname = joinpath(root, "vtk_test_2")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -58,8 +56,6 @@ end
@test result[:damage] == damage
@test result[:random_point_data] == random_point_data
@test result[:random_field_data] == random_field_data

rm(name)
end

@testitem "wrong file type" begin
Expand All @@ -69,9 +65,9 @@ end
@testitem "corrupt file raw encoding" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_3")
root = mktempdir()
bname = joinpath(root, "vtk_test_3")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -92,16 +88,14 @@ end
end

@test_throws ErrorException read_vtk(name)

rm(name)
end

@testitem "corrupt file offset marker" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_4")
root = mktempdir()
bname = joinpath(root, "vtk_test_4")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -122,17 +116,15 @@ end
end

@test_throws ErrorException read_vtk(name)

rm(name)
end

#-- corrupt file </AppendedData>
@testitem "corrupt file appended data" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_5")
root = mktempdir()
bname = joinpath(root, "vtk_test_5")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -153,18 +145,14 @@ end
end

@test_throws ErrorException read_vtk(name)

rm(name)
end

@testitem "read pvtu file" begin
using Peridynamics.WriteVTK

root = joinpath(@__DIR__, "tempdir_1")
isdir(root) && rm(root; recursive=true, force=true)
root = mktempdir()
bname = joinpath(root, "vtk_test")
name = bname * ".pvtu"
isfile(name) && rm(name)
n_points = 10
n_parts = 3
position = [rand(3, n_points) for _ in 1:n_parts]
Expand All @@ -189,18 +177,14 @@ end
@test result[:displacement] reduce(hcat, displacement)
@test result[:integer_test] reduce(vcat, integer_test)
@test result[:time] [time]

rm(root; recursive=true, force=true)
end

@testitem "read pvtu files with nothing exported" begin
using Peridynamics.WriteVTK

root = joinpath(@__DIR__, "tempdir_2")
isdir(root) && rm(root; recursive=true, force=true)
root = mktempdir()
bname = joinpath(root, "vtk_test")
name = bname * ".pvtu"
isfile(name) && rm(name)
n_points = 10
n_parts = 3
position = [rand(3, n_points) for _ in 1:n_parts]
Expand All @@ -213,16 +197,14 @@ end
result = read_vtk(name)

@test result[:position] reduce(hcat, position)

rm(root; recursive=true, force=true)
end

@testitem "read vtu files with nothing exported" begin
using Peridynamics.WriteVTK

bname = joinpath(@__DIR__, "vtk_test_6")
root = mktempdir()
bname = joinpath(root, "vtk_test_6")
name = bname * ".vtu"
isfile(name) && rm(name)
n_points = 10
position = rand(3, n_points)
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (j,)) for j in 1:n_points]
Expand All @@ -232,6 +214,4 @@ end

@test typeof(result) == Dict{Symbol,VecOrMat{Float64}}
@test result[:position] == position

rm(name)
end
10 changes: 2 additions & 8 deletions test/auxiliary/test_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@
end

@testitem "export_results Body" begin
temp_root = joinpath(@__DIR__, "temp_root_export_results_test_body")
rm(temp_root; recursive=true, force=true)
temp_root = mktempdir()

pos, vol = uniform_box(1, 1, 1, 0.5)
body = Body(BBMaterial(), pos, vol)
Expand Down Expand Up @@ -283,13 +282,10 @@ end
@test r[:displacement] u_rand
@test r[:damage] == zeros(8)
@test r[:time] == [t]

rm(temp_root; recursive=true, force=true)
end

@testitem "export_results MultibodySetup" begin
temp_root = joinpath(@__DIR__, "temp_root_export_results_test_multibody")
rm(temp_root; recursive=true, force=true)
temp_root = mktempdir()

pos_a, vol_a = uniform_box(1, 1, 1, 0.5)
body_a = Body(BBMaterial(), pos_a, vol_a)
Expand Down Expand Up @@ -359,6 +355,4 @@ end
@test r_b[:displacement] u_rand_b
@test r_b[:damage] == zeros(8)
@test r_b[:time] == [t]

rm(temp_root; recursive=true, force=true)
end
12 changes: 2 additions & 10 deletions test/auxiliary/test_process_each_export.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@testitem "process_each_export" begin
root = joinpath(@__DIR__, "temp_root_proc_each_export")
isdir(root) && rm(root; recursive=true, force=true)
root = mktempdir()
root_post_threads = joinpath(root, "post_threads")
mkpath(root_post_threads)
root_post_serial = joinpath(root, "post_serial")
Expand Down Expand Up @@ -83,17 +82,12 @@
file_3_mpi = joinpath(root_post_mpi, "max_displacement_3.txt")
@test isfile(file_3_mpi)
@test contains(read(file_3_mpi, String), "maximum displacement x: 2.4")

rm(root; recursive=true, force=true)
end

@testitem "find_vtk_files" begin
root = joinpath(@__DIR__, "temp_root_find_vtk_files")
isdir(root) && rm(root; recursive=true, force=true)

root = mktempdir()
vtk_files_unsorted = ["timestep_123.pvtu", "abcd_timestep_000005.pvtu",
"timestep_02.pvtu", "_1.pvtu"]
mkpath(root)
for file in vtk_files_unsorted
open(joinpath(root, file), "w+") do io
write(io, "")
Expand All @@ -103,6 +97,4 @@ end
vtk_files = Peridynamics.find_vtk_files(root)
@test basename.(vtk_files) == ["_1.pvtu", "timestep_02.pvtu",
"abcd_timestep_000005.pvtu", "timestep_123.pvtu"]

rm(root; recursive=true, force=true)
end
4 changes: 1 addition & 3 deletions test/integration/material_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ end

@testitem "TestMaterial full simulation" begin
include("test_material.jl")
root = joinpath(@__DIR__, "temp_testmaterial_full_simulation")
root = mktempdir()
path_vtk = joinpath(root, "vtk")

N = 10
Expand All @@ -562,6 +562,4 @@ end
@test isdir(path_vtk)
vtk_files = Peridynamics.find_vtk_files(path_vtk)
@test length(vtk_files) == 3

rm(root; recursive=true, force=true)
end
20 changes: 5 additions & 15 deletions test/integration/mpi_threads_comparison.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testitem "MPI-Threads comparison BBMaterial{NoCorrection}" begin
root = joinpath(@__DIR__, "temp_mpi_threads_comparison_bbnc")
root = mktempdir()
path_threads = joinpath(root, "results_threads")
path_threads_vtk = joinpath(path_threads, "vtk")
path_mpi = joinpath(root, "results_mpi")
Expand Down Expand Up @@ -61,12 +61,10 @@
@test res_threads[key] res_mpi[key]
end
end

rm(root; recursive=true, force=true)
end

@testitem "MPI-Threads comparison BBMaterial{NoCorrection} DynamicRelaxation" begin
root = joinpath(@__DIR__, "temp_mpi_threads_comparison_bbncdr")
root = mktempdir()
path_threads = joinpath(root, "results_threads")
path_threads_vtk = joinpath(path_threads, "vtk")
path_mpi = joinpath(root, "results_mpi")
Expand Down Expand Up @@ -135,12 +133,10 @@ end
@test Δe < 0.03
end
end

rm(root; recursive=true, force=true)
end

@testitem "MPI-Threads comparison BBMaterial{EnergySurfaceCorrection}" begin
root = joinpath(@__DIR__, "temp_mpi_threads_comparison_bbesc")
root = mktempdir()
path_threads = joinpath(root, "results_threads")
path_threads_vtk = joinpath(path_threads, "vtk")
path_mpi = joinpath(root, "results_mpi")
Expand Down Expand Up @@ -202,12 +198,10 @@ end
@test res_threads[key] res_mpi[key]
end
end

rm(root; recursive=true, force=true)
end

@testitem "MPI-Threads comparison OSBMaterial" begin
root = joinpath(@__DIR__, "temp_mpi_threads_comparison_osb")
root = mktempdir()
path_threads = joinpath(root, "results_threads")
path_threads_vtk = joinpath(path_threads, "vtk")
path_mpi = joinpath(root, "results_mpi")
Expand Down Expand Up @@ -269,12 +263,10 @@ end
@test res_threads[key] res_mpi[key]
end
end

rm(root; recursive=true, force=true)
end

@testitem "MPI-Threads comparison CMaterial" begin
root = joinpath(@__DIR__, "temp_mpi_threads_comparison_cc")
root = mktempdir()
path_threads = joinpath(root, "results_threads")
path_threads_vtk = joinpath(path_threads, "vtk")
path_mpi = joinpath(root, "results_mpi")
Expand Down Expand Up @@ -336,6 +328,4 @@ end
@test res_threads[key] res_mpi[key]
end
end

rm(root; recursive=true, force=true)
end
12 changes: 2 additions & 10 deletions test/integration/symmetry_bond_based.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
velocity_bc!(t -> 100, body, :set_a, :z)
velocity_bc!(t -> -100, body, :set_b, :z)
vv = VelocityVerlet(steps=100)
temp_root = joinpath(@__DIR__, "temp_root_symmetry_test_bb_vv")
rm(temp_root; recursive=true, force=true)
temp_root = mktempdir()
job = Job(body, vv; path=temp_root, freq=10)
dh = Peridynamics.submit_threads(job, 1)

Expand Down Expand Up @@ -62,9 +61,6 @@
@test abs(end_displacement[d, i]) > 0
end
end

# delete all simulation files
rm(temp_root; recursive=true, force=true)
end

@testitem "symmetry BBMaterial DynamicRelaxation" begin
Expand All @@ -85,8 +81,7 @@ end
forcedensity_bc!(t -> 1e10, body, :set_a, :z)
forcedensity_bc!(t -> -1e10, body, :set_b, :z)
dr = DynamicRelaxation(steps=100)
temp_root = joinpath(@__DIR__, "temp_root_symmetry_test_bb_dr")
rm(temp_root; recursive=true, force=true)
temp_root = mktempdir()
job = Job(body, dr; path=temp_root, freq=10)
dh = Peridynamics.submit_threads(job, 1)

Expand Down Expand Up @@ -121,7 +116,4 @@ end
@test abs(end_displacement[d, i]) > 0
end
end

# delete all simulation files
rm(temp_root; recursive=true, force=true)
end
Loading

0 comments on commit 2482927

Please sign in to comment.