Skip to content

Commit a45c617

Browse files
authored
Add functionality for loading setup code (#41)
1 parent 478dbf0 commit a45c617

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

templates/test/test/runtests.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const GROUP = uppercase(
1313
end,
1414
)
1515

16-
function istestfile(filename)
17-
return isfile(filename) &&
18-
endswith(filename, ".jl") &&
19-
startswith(basename(filename), "test")
20-
end
16+
"match files of the form `test_*.jl`, but exclude `*setup*.jl`"
17+
istestfile(fn) =
18+
endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup")
19+
"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`"
20+
isexamplefile(fn) =
21+
endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup")
2122

2223
@time begin
2324
# tests in groups based on folder structure
@@ -33,24 +34,27 @@ end
3334

3435
# single files in top folder
3536
for file in filter(istestfile, readdir(@__DIR__))
36-
(file == basename(@__FILE__)) && continue
37+
(file == basename(@__FILE__)) && continue # exclude this file to avoid infinite recursion
3738
@eval @safetestset $file begin
3839
include($file)
3940
end
4041
end
4142

4243
# test examples
4344
examplepath = joinpath(@__DIR__, "..", "examples")
44-
for file in filter(endswith(".jl"), readdir(examplepath; join=true))
45-
filename = basename(file)
46-
@eval begin
47-
@safetestset $filename begin
48-
$(Expr(
49-
:macrocall,
50-
GlobalRef(Suppressor, Symbol("@suppress")),
51-
LineNumberNode(@__LINE__, @__FILE__),
52-
:(include($file)),
53-
))
45+
for (root, _, files) in walkdir(examplepath)
46+
contains(chopprefix(root, @__DIR__), "setup") && continue
47+
for file in filter(isexamplefile, files)
48+
filename = joinpath(root, file)
49+
@eval begin
50+
@safetestset $file begin
51+
$(Expr(
52+
:macrocall,
53+
GlobalRef(Suppressor, Symbol("@suppress")),
54+
LineNumberNode(@__LINE__, @__FILE__),
55+
:(include($filename)),
56+
))
57+
end
5458
end
5559
end
5660
end

test/runtests.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const GROUP = uppercase(
1313
end,
1414
)
1515

16-
function istestfile(filename)
17-
return isfile(filename) &&
18-
endswith(filename, ".jl") &&
19-
startswith(basename(filename), "test")
20-
end
16+
"match files of the form `test_*.jl`, but exclude `*setup*.jl`"
17+
istestfile(fn) =
18+
endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup")
19+
"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`"
20+
isexamplefile(fn) =
21+
endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup")
2122

2223
@time begin
2324
# tests in groups based on folder structure
@@ -33,24 +34,27 @@ end
3334

3435
# single files in top folder
3536
for file in filter(istestfile, readdir(@__DIR__))
36-
(file == basename(@__FILE__)) && continue
37+
(file == basename(@__FILE__)) && continue # exclude this file to avoid infinite recursion
3738
@eval @safetestset $file begin
3839
include($file)
3940
end
4041
end
4142

4243
# test examples
4344
examplepath = joinpath(@__DIR__, "..", "examples")
44-
for file in filter(endswith(".jl"), readdir(examplepath; join=true))
45-
filename = basename(file)
46-
@eval begin
47-
@safetestset $filename begin
48-
$(Expr(
49-
:macrocall,
50-
GlobalRef(Suppressor, Symbol("@suppress")),
51-
LineNumberNode(@__LINE__, @__FILE__),
52-
:(include($file)),
53-
))
45+
for (root, _, files) in walkdir(examplepath)
46+
contains(chopprefix(root, @__DIR__), "setup") && continue
47+
for file in filter(isexamplefile, files)
48+
filename = joinpath(root, file)
49+
@eval begin
50+
@safetestset $file begin
51+
$(Expr(
52+
:macrocall,
53+
GlobalRef(Suppressor, Symbol("@suppress")),
54+
LineNumberNode(@__LINE__, @__FILE__),
55+
:(include($filename)),
56+
))
57+
end
5458
end
5559
end
5660
end

0 commit comments

Comments
 (0)