Skip to content

Commit 597f7df

Browse files
Add option to throw rather than warn if given a useless path (#131)
* Throw rather than warn if given a useless path * Make path validation opt-in * Move path validation to own function * Add tests * Bump version
1 parent 94f74bf commit 597f7df

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ReTestItems"
22
uuid = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
3-
version = "1.21.0"
3+
version = "1.22.0"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/ReTestItems.jl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ function _validated_nworker_threads(str)
9999
return replace(str, "auto" => string(Sys.CPU_THREADS))
100100
end
101101

102+
function _validated_paths(paths, should_throw::Bool)
103+
return filter(paths) do p
104+
if !ispath(p)
105+
msg = "No such path $(repr(p))"
106+
should_throw ? throw(ArgumentError(msg)) : @warn msg
107+
return false
108+
elseif !(is_test_file(p) || is_testsetup_file(p)) && isfile(p)
109+
msg = "$(repr(p)) is not a test file"
110+
should_throw ? throw(ArgumentError(msg)) : @warn msg
111+
return false
112+
else
113+
return true
114+
end
115+
end
116+
end
117+
102118
"""
103119
ReTestItems.runtests()
104120
ReTestItems.runtests(mod::Module)
@@ -168,8 +184,12 @@ will be run.
168184
For interative sessions, `:eager` is the default when running with 0 or 1 worker processes, `:batched` otherwise.
169185
For non-interactive sessions, `:issues` is used by default.
170186
- `verbose_results::Bool`: If `true`, the final test report will list each `@testitem`, otherwise
171-
the results are aggregated. Default is `false` for non-interactive sessions
172-
or when `logs=:issues`, `true` otherwise.
187+
the results are aggregated. Default is `false` for non-interactive sessions
188+
or when `logs=:issues`, `true` otherwise.
189+
- `validate_paths::Bool=false`: If `true`, `runtests` will throw an error if any of the
190+
`paths` passed to it cannot contain test files, either because the path doesn't exist or
191+
the path points to a file which is not a test file. Default is `false`.
192+
Can also be set using the `RETESTITEMS_VALIDATE_PATHS` environment variable.
173193
"""
174194
function runtests end
175195

@@ -214,19 +234,11 @@ function runtests(
214234
logs::Symbol=Symbol(get(ENV, "RETESTITEMS_LOGS", default_log_display_mode(report, nworkers))),
215235
verbose_results::Bool=(logs !== :issues && isinteractive()),
216236
test_end_expr::Expr=Expr(:block),
237+
validate_paths::Bool=parse(Bool, get(ENV, "RETESTITEMS_VALIDATE_PATHS", "false")),
217238
)
218239
nworker_threads = _validated_nworker_threads(nworker_threads)
219-
paths′ = filter(paths) do p
220-
if !ispath(p)
221-
@warn "No such path $(repr(p))"
222-
return false
223-
elseif !(is_test_file(p) || is_testsetup_file(p)) && isfile(p)
224-
@warn "$(repr(p)) is not a test file"
225-
return false
226-
else
227-
return true
228-
end
229-
end
240+
paths′ = _validated_paths(paths, validate_paths)
241+
230242
logs in LOG_DISPLAY_MODES || throw(ArgumentError("`logs` must be one of $LOG_DISPLAY_MODES, got $(repr(logs))"))
231243
report && logs == :eager && throw(ArgumentError("`report=true` is not compatible with `logs=:eager`"))
232244
(0 memory_threshold 1) || throw(ArgumentError("`memory_threshold` must be between 0 and 1, got $(repr(memory_threshold))"))

test/integrationtests.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,39 @@ end
7979
@test n_passed(results) == 1
8080
end
8181

82-
@testset "Warn when not test file" begin
82+
@testset "Warn or error when not test file" begin
8383
pkg = joinpath(TEST_PKG_DIR, "TestsInSrc.jl")
8484

8585
# warn if the path does not exist
8686
dne = joinpath(pkg, "does_not_exist")
87-
@test_logs (:warn, "No such path \"$dne\"") match_mode=:any begin
87+
dne_msg = "No such path \"$dne\""
88+
@test_logs (:warn, dne_msg) match_mode=:any begin
8889
runtests(dne)
8990
end
91+
# throw if `validate_paths`
92+
@test_throws ArgumentError(dne_msg) runtests(dne; validate_paths=true)
93+
# test setting `validate_paths` via environment variable
94+
withenv("RETESTITEMS_VALIDATE_PATHS" => 1) do
95+
@test_throws ArgumentError(dne_msg) runtests(dne)
96+
end
9097

9198
# warn if the file is not a test file
9299
file = joinpath(pkg, "src", "foo.jl")
93100
@assert isfile(file)
94-
@test_logs (:warn, "\"$file\" is not a test file") match_mode=:any begin
101+
file_msg = "\"$file\" is not a test file"
102+
@test_logs (:warn, file_msg) match_mode=:any begin
95103
runtests(file)
96104
end
105+
# throw if `validate_paths`
106+
@test_throws ArgumentError(file_msg) runtests(file; validate_paths=true)
97107

98108
# Warn for each invalid path
99-
@test_logs (:warn, "No such path \"$dne\"") (:warn, "\"$file\" is not a test file") match_mode=:any begin
109+
@test_logs (:warn, dne_msg) (:warn, file_msg) match_mode=:any begin
100110
runtests(dne, file)
101111
end
112+
# Throw on first invalid path if `validate_paths`
113+
@test_throws ArgumentError(dne_msg) runtests(dne, file; validate_paths=true)
114+
@test_throws ArgumentError(file_msg) runtests(file, dne; validate_paths=true)
102115

103116
# Warn for each invalid path and still run valid ones
104117
test_file = joinpath(pkg, "src", "foo_test.jl")
@@ -109,6 +122,8 @@ end
109122
end
110123
end
111124
@test n_tests(results) == 2 # foo_test.jl has 2 tests
125+
# Throw on first invalid path, even if some are valid, if `validate_paths`
126+
@test_throws ArgumentError(dne_msg) runtests(test_file, dne, file; validate_paths=true)
112127
end
113128

114129
@testset "filter `runtests(func, x)`" begin

test/internals.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,36 @@ end
249249
end
250250
end
251251

252+
@testset "_validated_paths" begin
253+
_validated_paths = ReTestItems._validated_paths
254+
testfiles_dir = joinpath(pkgdir(ReTestItems), "test", "testfiles")
255+
256+
test_file = joinpath(testfiles_dir, "_happy_tests.jl")
257+
@assert isfile(test_file)
258+
for _throw in (false, true)
259+
@test _validated_paths((test_file,), _throw) == (test_file,)
260+
@test_logs _validated_paths((test_file,), _throw) # test nothing is logged
261+
end
262+
263+
@assert !ispath("foo")
264+
@test _validated_paths(("foo",), false) == ()
265+
@test_logs (:warn, "No such path \"foo\"") _validated_paths(("foo",), false)
266+
@test_throws ArgumentError("No such path \"foo\"") _validated_paths(("foo",), true)
267+
268+
@assert isfile(test_file)
269+
@assert !ispath("foo")
270+
paths = (test_file, "foo",)
271+
@test _validated_paths(paths, false) == (test_file,)
272+
@test_logs (:warn, "No such path \"foo\"") _validated_paths(paths, false)
273+
@test_throws ArgumentError("No such path \"foo\"") _validated_paths(paths, true)
274+
275+
nontest_file = joinpath(testfiles_dir, "_empty_file.jl")
276+
@assert isfile(nontest_file)
277+
@assert !ReTestItems.is_test_file(nontest_file)
278+
@assert !ReTestItems.is_testsetup_file(nontest_file)
279+
@test _validated_paths((nontest_file,), false) == ()
280+
@test_logs (:warn, "\"$nontest_file\" is not a test file") _validated_paths((nontest_file,), false)
281+
@test_throws ArgumentError("\"$nontest_file\" is not a test file") _validated_paths((nontest_file,), true)
282+
end
283+
252284
end # internals.jl testset

0 commit comments

Comments
 (0)