Skip to content

Commit 61cd153

Browse files
add api + print tags
1 parent c7b6894 commit 61cd153

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

src/ReTestItems.jl

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using TestEnv
99
using Logging: current_logger, with_logger
1010

1111
export runtests, runtestitem
12+
export print_tests
1213
export @testsetup, @testitem
1314
export TestSetup, TestItem, TestItemResult
1415

@@ -231,15 +232,18 @@ will be run.
231232
"""
232233
function runtests end
233234

234-
runtests(; kw...) = runtests(Returns(true), dirname(Base.active_project()); kw...)
235-
runtests(shouldrun; kw...) = runtests(shouldrun, dirname(Base.active_project()); kw...)
236-
runtests(paths::AbstractString...; kw...) = runtests(Returns(true), paths...; kw...)
237-
238-
runtests(pkg::Module; kw...) = runtests(Returns(true), pkg; kw...)
239-
function runtests(shouldrun, pkg::Module; kw...)
240-
dir = pkgdir(pkg)
241-
isnothing(dir) && error("Could not find directory for `$pkg`")
242-
return runtests(shouldrun, dir; kw...)
235+
for func in (:runtests, :print_tests)
236+
@eval begin
237+
$func(; kw...) = $func(Returns(true), dirname(Base.active_project()); kw...)
238+
$func(shouldrun; kw...) = $func(shouldrun, dirname(Base.active_project()); kw...)
239+
$func(paths::AbstractString...; kw...) = $func(Returns(true), paths...; kw...)
240+
$func(pkg::Module; kw...) = $func(Returns(true), pkg; kw...)
241+
function $func(shouldrun, pkg::Module; kw...)
242+
dir = pkgdir(pkg)
243+
isnothing(dir) && error("Could not find directory for `$pkg`")
244+
return $func(shouldrun, dir; kw...)
245+
end
246+
end
243247
end
244248

245249
@kwdef struct _Config
@@ -257,8 +261,10 @@ end
257261
timeout_profile_wait::Int
258262
memory_threshold::Float64
259263
gc_between_testitems::Bool
264+
dryrun::Bool
260265
end
261266

267+
default_validate_paths() = parse(Bool, get(ENV, "RETESTITEMS_VALIDATE_PATHS", "false"))
262268

263269
function runtests(
264270
shouldrun,
@@ -276,11 +282,12 @@ function runtests(
276282
logs::Symbol=Symbol(get(ENV, "RETESTITEMS_LOGS", default_log_display_mode(report, nworkers))),
277283
verbose_results::Bool=(logs !== :issues && isinteractive()),
278284
test_end_expr::Expr=Expr(:block),
279-
validate_paths::Bool=parse(Bool, get(ENV, "RETESTITEMS_VALIDATE_PATHS", "false")),
285+
validate_paths::Bool=default_validate_paths(),
280286
timeout_profile_wait::Real=parse(Int, get(ENV, "RETESTITEMS_TIMEOUT_PROFILE_WAIT", "0")),
281287
gc_between_testitems::Bool=parse(Bool, get(ENV, "RETESTITEMS_GC_BETWEEN_TESTITEMS", string(nworkers > 1))),
282288
failfast::Bool=parse(Bool, get(ENV, "RETESTITEMS_FAILFAST", "false")),
283289
testitem_failfast::Bool=parse(Bool, get(ENV, "RETESTITEMS_TESTITEM_FAILFAST", string(failfast))),
290+
dryrun::Bool=parse(Bool, get(ENV, "RETESTITEMS_DRYRUN", "false")),
284291
)
285292
nworker_threads = _validated_nworker_threads(nworker_threads)
286293
paths′ = _validated_paths(paths, validate_paths)
@@ -301,7 +308,7 @@ function runtests(
301308
(timeout_profile_wait > 0 && Sys.iswindows()) && @warn "CPU profiles on timeout is not supported on Windows, ignoring `timeout_profile_wait`"
302309
mkpath(RETESTITEMS_TEMP_FOLDER[]) # ensure our folder wasn't removed
303310
save_current_stdio()
304-
cfg = _Config(; nworkers, nworker_threads, worker_init_expr, test_end_expr, testitem_timeout, testitem_failfast, failfast, retries, logs, report, verbose_results, timeout_profile_wait, memory_threshold, gc_between_testitems)
311+
cfg = _Config(; nworkers, nworker_threads, worker_init_expr, test_end_expr, testitem_timeout, testitem_failfast, failfast, retries, logs, report, verbose_results, timeout_profile_wait, memory_threshold, gc_between_testitems, dryrun)
305312
debuglvl = Int(debug)
306313
if debuglvl > 0
307314
withdebug(debuglvl) do
@@ -312,6 +319,21 @@ function runtests(
312319
end
313320
end
314321

322+
"""
323+
ReTestItems.print_tests()
324+
ReTestItems.print_tests(mod::Module)
325+
ReTestItems.print_tests(paths::AbstractString...)
326+
327+
Print the tests that would be run by `runtests`.
328+
Also available via `runtests(args...; dryrun=true, kw...)`.
329+
"""
330+
function print_tests(
331+
shouldrun, paths::AbstractString...;
332+
name=nothing, tags=nothing, validate_paths=default_validate_paths(),
333+
)
334+
runtests(shouldrun, paths...; dryrun=true, name, tags, validate_paths)
335+
end
336+
315337
# keep track of temporary test environments we create in case we can reuse them
316338
# on repeated runs of `runtests` in the same Project
317339
# in https://relationalai.atlassian.net/browse/RAI-11599, it was noted that when
@@ -339,8 +361,9 @@ function _runtests(ti_filter, paths, cfg::_Config)
339361
# Wrapping with the logger that was set before eval'ing any user code to
340362
# avoid world age issues when logging https://github.com/JuliaLang/julia/issues/33865
341363
with_logger(current_logger()) do
342-
if is_running_test_runtests_jl(proj_file)
364+
if is_running_test_runtests_jl(proj_file) || cfg.dryrun
343365
# Assume this is `Pkg.test`, so test env already active.
366+
# Or if `dryrun`, we don't need the env as we won't run the tests.
344367
@debugv 2 "Running in current environment `$(Base.active_project())`"
345368
return _runtests_in_current_env(ti_filter, paths, proj_file, cfg)
346369
else
@@ -377,10 +400,8 @@ function _runtests_in_current_env(
377400
ntestitems = length(testitems.testitems)
378401
@debugv 1 "Done including tests in $paths"
379402
@info "Finished scanning for test items in $(round(time() - inc_time, digits=2)) seconds."
380-
# TODO: make this a keyword to `runtests` that gets passed to `_runtests_in_current_env`
381-
dryrun = parse(Bool, get(ENV, "RETESTITEMS_DRYRUN", "false"))::Bool
382-
if dryrun
383-
print_testitems(testitems)
403+
if cfg.dryrun
404+
_print_testitems(testitems)
384405
return nothing
385406
end
386407
@info "Scheduling $ntestitems tests on pid $(Libc.getpid())" *

src/testcontext.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,46 @@ cancel!(t::TestItems) = @atomicswap t.cancelled = true
103103
# `cancel` and check the return value to know if they had already been cancelled.
104104
is_cancelled(t::TestItems) = @atomic t.cancelled
105105

106-
function print_testitems(tis::TestItems)
106+
function _print_testitems(tis::TestItems)
107107
ntestitems = length(tis.testitems)
108108
if ntestitems == 0
109109
printstyled("No test items found\n"; bold=true)
110110
return nothing
111111
end
112112
plural = ntestitems == 1 ? "" : "s"
113113
printstyled("$ntestitems test item$plural found:\n"; bold=true)
114-
print_testitems(tis.graph)
114+
_print_testitems(tis.graph)
115+
@info "Found $ntestitems test item$plural."
115116
end
116-
function print_testitems(dir::DirNode, indent::Int=0)
117+
function _print_testitems(dir::DirNode, indent::Int=0)
117118
println(" "^indent, dir.path)
118119
for child in dir.children
119-
print_testitems(child, indent + 1)
120+
_print_testitems(child, indent + 1)
120121
end
121122
end
122-
function print_testitems(file::FileNode, indent::Int=0)
123+
function _print_testitems(file::FileNode, indent::Int=0)
123124
println(" "^indent, file.path)
124125
for ti in file.testitems
125-
printstyled(" "^(indent+1), ti.name, "\n"; bold=true)
126+
printstyled(" "^(indent+1), ti.name; bold=true)
127+
if !isempty(ti.tags)
128+
printstyled(sprint(_print_tags, ti.tags); color=:light_black)
129+
end
130+
print('\n')
126131
end
127132
end
133+
function _print_tags(io::IO, tags::AbstractVector{Symbol})
134+
print(io, " [")
135+
isfirst = true
136+
for tag in tags
137+
if !isfirst
138+
print(io, ", ")
139+
end
140+
show(io, tag)
141+
isfirst = false
142+
end
143+
print(io, "]")
144+
end
145+
128146

129147
###
130148
### record results

0 commit comments

Comments
 (0)