Skip to content

Commit 629e5c1

Browse files
committed
simplify test runner
1 parent 016b9ad commit 629e5c1

File tree

2 files changed

+55
-99
lines changed

2 files changed

+55
-99
lines changed

src/ParallelTestRunner.jl

Lines changed: 36 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ function extract_flag!(args, flag, default = nothing; typ = typeof(default))
4545
return (false, default)
4646
end
4747

48+
function with_testset(f, testset)
49+
@static if VERSION >= v"1.13.0-DEV.1044"
50+
Test.@with_testset testset f()
51+
else
52+
Test.push_testset(testset)
53+
try
54+
f()
55+
finally
56+
Test.pop_testset()
57+
end
58+
end
59+
return nothing
60+
end
61+
4862
function runtests(ARGS, testfilter = _ -> true)
4963
do_help, _ = extract_flag!(ARGS, "--help")
5064
if do_help
@@ -362,16 +376,15 @@ function runtests(ARGS, testfilter = _ -> true)
362376
println("Testing finished in $elapsed")
363377

364378
# construct a testset to render the test results
365-
completed_tests = Set{String}()
366379
o_ts = Test.DefaultTestSet("Overall")
367-
@static if VERSION < v"1.13.0-DEV.1044"
368-
Test.push_testset(o_ts)
380+
with_testset(o_ts) do
381+
completed_tests = Set{String}()
369382
for (testname, (resp,)) in results
370383
push!(completed_tests, testname)
371384
if isa(resp, Test.DefaultTestSet)
372-
Test.push_testset(resp)
373-
Test.record(o_ts, resp)
374-
Test.pop_testset()
385+
with_testset(resp) do
386+
Test.record(o_ts, resp)
387+
end
375388
elseif isa(resp, Tuple{Int, Int})
376389
fake = Test.DefaultTestSet(testname)
377390
for i in 1:resp[1]
@@ -380,9 +393,9 @@ function runtests(ARGS, testfilter = _ -> true)
380393
for i in 1:resp[2]
381394
Test.record(fake, Test.Broken(:test, nothing))
382395
end
383-
Test.push_testset(fake)
384-
Test.record(o_ts, fake)
385-
Test.pop_testset()
396+
with_testset(fake) do
397+
Test.record(o_ts, fake)
398+
end
386399
elseif isa(resp, RemoteException) && isa(resp.captured.ex, Test.TestSetException)
387400
println("Worker $(resp.pid) failed running test $(testname):")
388401
Base.showerror(stdout, resp.captured)
@@ -397,9 +410,9 @@ function runtests(ARGS, testfilter = _ -> true)
397410
for t in resp.captured.ex.errors_and_fails
398411
Test.record(fake, t)
399412
end
400-
Test.push_testset(fake)
401-
Test.record(o_ts, fake)
402-
Test.pop_testset()
413+
with_testset(fake) do
414+
Test.record(o_ts, fake)
415+
end
403416
else
404417
if !isa(resp, Exception)
405418
resp = ErrorException(string("Unknown result type : ", typeof(resp)))
@@ -409,95 +422,32 @@ function runtests(ARGS, testfilter = _ -> true)
409422
# the test runner itself had some problem, so we may have hit a segfault,
410423
# deserialization errors or something similar. Record this testset as Errored.
411424
fake = Test.DefaultTestSet(testname)
412-
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Any[(resp, [])], LineNumberNode(1)))
413-
Test.push_testset(fake)
414-
Test.record(o_ts, fake)
415-
Test.pop_testset()
416-
end
417-
end
418-
else
419-
Test.@with_testset o_ts begin
420-
for (testname, (resp,)) in results
421-
push!(completed_tests, testname)
422-
if isa(resp, Test.DefaultTestSet)
423-
Test.@with_testset resp begin
424-
Test.record(o_ts, resp)
425-
end
426-
elseif isa(resp, Tuple{Int, Int})
427-
fake = Test.DefaultTestSet(testname)
428-
for i in 1:resp[1]
429-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, nothing))
430-
end
431-
for i in 1:resp[2]
432-
Test.record(fake, Test.Broken(:test, nothing))
433-
end
434-
Test.@with_testset fake begin
435-
Test.record(o_ts, fake)
436-
end
437-
elseif isa(resp, RemoteException) && isa(resp.captured.ex, Test.TestSetException)
438-
println("Worker $(resp.pid) failed running test $(testname):")
439-
Base.showerror(stdout, resp.captured)
440-
println()
441-
fake = Test.DefaultTestSet(testname)
442-
for i in 1:resp.captured.ex.pass
443-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, nothing))
444-
end
445-
for i in 1:resp.captured.ex.broken
446-
Test.record(fake, Test.Broken(:test, nothing))
447-
end
448-
for t in resp.captured.ex.errors_and_fails
449-
Test.record(fake, t)
450-
end
451-
Test.@with_testset fake begin
452-
Test.record(o_ts, fake)
453-
end
454-
else
455-
if !isa(resp, Exception)
456-
resp = ErrorException(string("Unknown result type : ", typeof(resp)))
457-
end
458-
# If this test raised an exception that is not a remote testset exception,
459-
# i.e. not a RemoteException capturing a TestSetException that means
460-
# the test runner itself had some problem, so we may have hit a segfault,
461-
# deserialization errors or something similar. Record this testset as Errored.
462-
fake = Test.DefaultTestSet(testname)
463-
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Base.ExceptionStack([(exception = resp, backtrace = [])]), LineNumberNode(1)))
464-
Test.@with_testset fake begin
465-
Test.record(o_ts, fake)
466-
end
425+
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Base.ExceptionStack([(exception = resp, backtrace = [])]), LineNumberNode(1)))
426+
with_testset(fake) do
427+
Test.record(o_ts, fake)
467428
end
468429
end
469430
end
470-
end
471-
for test in tests
472-
(test in completed_tests) && continue
473-
fake = Test.DefaultTestSet(test)
474-
@static if VERSION < v"1.13.0-DEV.1044"
475-
Test.record(
476-
fake, Test.Error(
477-
:test_interrupted, test, nothing,
478-
[("skipped", [])], LineNumberNode(1)
479-
)
480-
)
481-
Test.push_testset(fake)
482-
Test.record(o_ts, fake)
483-
Test.pop_testset()
484-
else
431+
for test in tests
432+
(test in completed_tests) && continue
433+
fake = Test.DefaultTestSet(test)
485434
Test.record(fake, Test.Error(:test_interrupted, test, nothing, Base.ExceptionStack([(exception = "skipped", backtrace = [])]), LineNumberNode(1)))
486-
Test.@with_testset fake begin
435+
with_testset(fake) do
487436
Test.record(o_ts, fake)
488437
end
489438
end
490439
end
491440
println()
492441
Test.print_test_results(o_ts, 1)
493-
if !o_ts.anynonpass
442+
if (VERSION >= v"1.13.0-DEV.1037" && !Test.anynonpass(o_ts)) ||
443+
(VERSION < v"1.13.0-DEV.1037" && !o_ts.anynonpass)
494444
println(" \033[32;1mSUCCESS\033[0m")
495445
else
496446
println(" \033[31;1mFAILURE\033[0m\n")
497447
Test.print_test_errors(o_ts)
498448
throw(Test.FallbackTestSetException("Test run finished with errors"))
499449
end
500-
450+
return nothing
501451
end # runtests
502452

503-
end # module ParallelTestRunner
453+
end # module ParallelTestRunner

src/setup.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ import ParallelTestRunner
22
import ParallelTestRunner: Distributed, Test
33
using .Distributed, .Test
44

5+
if VERSION >= v"1.13.0-DEV.1044"
6+
using Base.ScopedValues
7+
end
8+
59
## entry point
610

711
function __runtests(f, name)
8-
old_print_setting = Test.TESTSET_PRINT_ENABLE[]
9-
if VERSION < v"1.13.0-DEV.1044"
10-
Test.TESTSET_PRINT_ENABLE[] = false
11-
else
12-
Test.TESTSET_PRINT_ENABLE[] => false
13-
end
14-
15-
return try
12+
function inner()
1613
# generate a temporary module to execute the tests in
1714
mod_name = Symbol("Test", rand(1:100), "Main_", replace(name, '/' => '_'))
1815
mod = @eval(Main, module $mod_name end)
@@ -60,14 +57,23 @@ function __runtests(f, name)
6057
res = vcat(collect(data), rss)
6158

6259
GC.gc(true)
63-
res
64-
finally
65-
if VERSION < v"1.13.0-DEV.1044"
60+
return res
61+
end
62+
63+
res = @static if VERSION >= v"1.13.0-DEV.1044"
64+
@with Test.TESTSET_PRINT_ENABLE => false begin
65+
inner()
66+
end
67+
else
68+
old_print_setting = Test.TESTSET_PRINT_ENABLE[]
69+
Test.TESTSET_PRINT_ENABLE[] = false
70+
try
71+
inner()
72+
finally
6673
Test.TESTSET_PRINT_ENABLE[] = old_print_setting
67-
else
68-
Test.TESTSET_PRINT_ENABLE[] => old_print_setting
6974
end
7075
end
76+
return res
7177
end
7278

7379

0 commit comments

Comments
 (0)