Skip to content

Commit 97bdfdb

Browse files
authored
Simplify test runner. (#626)
1 parent 87e91dd commit 97bdfdb

File tree

2 files changed

+53
-114
lines changed

2 files changed

+53
-114
lines changed

test/runtests.jl

Lines changed: 38 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,27 @@ elapsed = canonicalize(Dates.CompoundPeriod(t1-t0))
277277
println("Testing finished in $elapsed")
278278

279279
# construct a testset to render the test results
280-
completed_tests = Set{String}()
281280
o_ts = Test.DefaultTestSet("Overall")
282-
@static if VERSION < v"1.13.0-DEV.1044"
283-
Test.push_testset(o_ts)
281+
function with_testset(f, testset)
282+
@static if VERSION >= v"1.13.0-DEV.1044"
283+
Test.@with_testset testset f()
284+
else
285+
Test.push_testset(testset)
286+
try
287+
f()
288+
finally
289+
Test.pop_testset()
290+
end
291+
end
292+
end
293+
with_testset(o_ts) do
294+
completed_tests = Set{String}()
284295
for (testname, (resp,)) in results
285296
push!(completed_tests, testname)
286297
if isa(resp, Test.DefaultTestSet)
287-
Test.push_testset(resp)
288-
Test.record(o_ts, resp)
289-
Test.pop_testset()
298+
with_testset(resp) do
299+
Test.record(o_ts, resp)
300+
end
290301
elseif isa(resp, Tuple{Int,Int})
291302
fake = Test.DefaultTestSet(testname)
292303
for i in 1:resp[1]
@@ -295,9 +306,9 @@ o_ts = Test.DefaultTestSet("Overall")
295306
for i in 1:resp[2]
296307
Test.record(fake, Test.Broken(:test, nothing))
297308
end
298-
Test.push_testset(fake)
299-
Test.record(o_ts, fake)
300-
Test.pop_testset()
309+
with_testset(fake) do
310+
Test.record(o_ts, fake)
311+
end
301312
elseif isa(resp, RemoteException) && isa(resp.captured.ex, Test.TestSetException)
302313
println("Worker $(resp.pid) failed running test $(testname):")
303314
Base.showerror(stdout, resp.captured)
@@ -312,9 +323,9 @@ o_ts = Test.DefaultTestSet("Overall")
312323
for t in resp.captured.ex.errors_and_fails
313324
Test.record(fake, t)
314325
end
315-
Test.push_testset(fake)
316-
Test.record(o_ts, fake)
317-
Test.pop_testset()
326+
with_testset(fake) do
327+
Test.record(o_ts, fake)
328+
end
318329
else
319330
if !isa(resp, Exception)
320331
resp = ErrorException(string("Unknown result type : ", typeof(resp)))
@@ -324,105 +335,28 @@ o_ts = Test.DefaultTestSet("Overall")
324335
# the test runner itself had some problem, so we may have hit a segfault,
325336
# deserialization errors or something similar. Record this testset as Errored.
326337
fake = Test.DefaultTestSet(testname)
327-
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Any[(resp, [])], LineNumberNode(1)))
328-
Test.push_testset(fake)
329-
Test.record(o_ts, fake)
330-
Test.pop_testset()
331-
end
332-
end
333-
for test in all_tests
334-
(test in completed_tests) && continue
335-
fake = Test.DefaultTestSet(test)
336-
Test.record(fake, Test.Error(:test_interrupted, test, nothing,
337-
[("skipped", [])], LineNumberNode(1)))
338-
Test.push_testset(fake)
339-
Test.record(o_ts, fake)
340-
Test.pop_testset()
341-
end
342-
println()
343-
Test.print_test_results(o_ts, 1)
344-
if !o_ts.anynonpass
345-
println(" \033[32;1mSUCCESS\033[0m")
346-
else
347-
println(" \033[31;1mFAILURE\033[0m\n")
348-
Test.print_test_errors(o_ts)
349-
throw(Test.FallbackTestSetException("Test run finished with errors"))
350-
end
351-
else
352-
Test.@with_testset o_ts begin
353-
for (testname, (resp,)) in results
354-
push!(completed_tests, testname)
355-
if isa(resp, Test.DefaultTestSet)
356-
Test.@with_testset resp begin
357-
Test.record(o_ts, resp)
358-
end
359-
elseif isa(resp, Tuple{Int,Int})
360-
fake = Test.DefaultTestSet(testname)
361-
for i in 1:resp[1]
362-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, nothing))
363-
end
364-
for i in 1:resp[2]
365-
Test.record(fake, Test.Broken(:test, nothing))
366-
end
367-
Test.@with_testset fake begin
368-
Test.record(o_ts, fake)
369-
end
370-
elseif isa(resp, RemoteException) && isa(resp.captured.ex, Test.TestSetException)
371-
println("Worker $(resp.pid) failed running test $(testname):")
372-
Base.showerror(stdout, resp.captured)
373-
println()
374-
fake = Test.DefaultTestSet(testname)
375-
for i in 1:resp.captured.ex.pass
376-
Test.record(fake, Test.Pass(:test, nothing, nothing, nothing, nothing))
377-
end
378-
for i in 1:resp.captured.ex.broken
379-
Test.record(fake, Test.Broken(:test, nothing))
380-
end
381-
for t in resp.captured.ex.errors_and_fails
382-
Test.record(fake, t)
383-
end
384-
Test.@with_testset fake begin
385-
Test.record(o_ts, fake)
386-
end
387-
else
388-
if !isa(resp, Exception)
389-
resp = ErrorException(string("Unknown result type : ", typeof(resp)))
390-
end
391-
# If this test raised an exception that is not a remote testset exception,
392-
# i.e. not a RemoteException capturing a TestSetException that means
393-
# the test runner itself had some problem, so we may have hit a segfault,
394-
# deserialization errors or something similar. Record this testset as Errored.
395-
fake = Test.DefaultTestSet(testname)
396-
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Base.ExceptionStack([(exception=resp,backtrace=[])]), LineNumberNode(1)))
397-
Test.@with_testset fake begin
398-
Test.record(o_ts, fake)
399-
end
338+
Test.record(fake, Test.Error(:nontest_error, testname, nothing, Base.ExceptionStack([(exception=resp,backtrace=[])]), LineNumberNode(1)))
339+
with_testset(fake) do
340+
Test.record(o_ts, fake)
400341
end
401342
end
402343
end
403-
for test in all_tests
344+
for test in tests
404345
(test in completed_tests) && continue
405346
fake = Test.DefaultTestSet(test)
406347
Test.record(fake, Test.Error(:test_interrupted, test, nothing, Base.ExceptionStack([(exception="skipped",backtrace=[])]), LineNumberNode(1)))
407-
Test.@with_testset fake begin
348+
with_testset(fake) do
408349
Test.record(o_ts, fake)
409350
end
410351
end
411-
println()
412-
Test.print_test_results(o_ts, 1)
413-
414-
success = @static if VERSION < v"1.13.0-DEV.1044"
415-
!o_ts.anynonpass
416-
else
417-
o_ts.anynonpass == 0x01
418-
end
419-
420-
if success
421-
println(" \033[32;1mSUCCESS\033[0m")
422-
else
423-
println(" \033[31;1mFAILURE\033[0m\n")
424-
Test.print_test_errors(o_ts)
425-
throw(Test.FallbackTestSetException("Test run finished with errors"))
426-
end
427352
end
428-
353+
println()
354+
Test.print_test_results(o_ts, 1)
355+
if (VERSION >= v"1.13.0-DEV.1037" && !Test.anynonpass(o_ts)) ||
356+
(VERSION < v"1.13.0-DEV.1037" && !o_ts.anynonpass)
357+
println(" \033[32;1mSUCCESS\033[0m")
358+
else
359+
println(" \033[31;1mFAILURE\033[0m\n")
360+
Test.print_test_errors(o_ts)
361+
throw(Test.FallbackTestSetException("Test run finished with errors"))
362+
end

test/setup.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ include("testsuite.jl")
44

55
using Random
66

7-
@static if VERSION >= v"1.13.0-DEV.1044"
8-
using Base.ScopedValues
7+
if VERSION >= v"1.13.0-DEV.1044"
8+
using Base.ScopedValues
99
end
1010

1111
## entry point
1212

1313
function runtests(f, name)
14-
@static if VERSION < v"1.13.0-DEV.1044"
15-
old_print_setting = Test.TESTSET_PRINT_ENABLE[]
16-
Test.TESTSET_PRINT_ENABLE[] = false
17-
end
18-
19-
try
14+
function inner()
2015
# generate a temporary module to execute the tests in
2116
mod_name = Symbol("Test", rand(1:100), "Main_", replace(name, '/' => '_'))
2217
mod = @eval(Main, module $mod_name end)
@@ -64,8 +59,18 @@ function runtests(f, name)
6459

6560
GC.gc(true)
6661
res
67-
finally
68-
@static if VERSION < v"1.13.0-DEV.1044"
62+
end
63+
64+
@static if VERSION >= v"1.13.0-DEV.1044"
65+
@with Test.TESTSET_PRINT_ENABLE=>false begin
66+
inner()
67+
end
68+
else
69+
old_print_setting = Test.TESTSET_PRINT_ENABLE[]
70+
Test.TESTSET_PRINT_ENABLE[] = false
71+
try
72+
inner()
73+
finally
6974
Test.TESTSET_PRINT_ENABLE[] = old_print_setting
7075
end
7176
end

0 commit comments

Comments
 (0)