@@ -301,69 +301,79 @@ defmodule ExUnit.Runner do
301
301
end
302
302
303
303
defp run_module_tests ( config , test_module , async? , tests ) do
304
- { module_pid , module_ref } = run_setup_all ( test_module , async? , self ( ) )
304
+ Process . put ( @ current_key , test_module )
305
+ % ExUnit.TestModule { name: module , tags: tags , parameters: params } = test_module
306
+ context = tags |> Map . merge ( params ) |> Map . merge ( % { module: module , async: async? } )
307
+
308
+ config
309
+ |> run_setup_all ( test_module , context , fn context ->
310
+ if max_failures_reached? ( config ) ,
311
+ do: [ ] ,
312
+ else: run_tests ( config , tests , test_module . parameters , context )
313
+ end )
314
+ |> case do
315
+ { { :ok , finished_tests } , test_module } ->
316
+ { test_module , [ ] , finished_tests }
305
317
306
- { test_module , invalid_tests , finished_tests } =
307
- receive do
308
- { ^ module_pid , :setup_all , { :ok , context } } ->
309
- finished_tests =
310
- if max_failures_reached? ( config ) ,
311
- do: [ ] ,
312
- else: run_tests ( config , tests , test_module . parameters , context )
318
+ { :error , test_module } ->
319
+ { test_module , Enum . map ( tests , & % { & 1 | state: { :invalid , test_module } } ) , [ ] }
320
+ end
321
+ end
313
322
314
- :ok = exit_setup_all ( module_pid , module_ref )
315
- { test_module , [ ] , finished_tests }
323
+ defp run_setup_all (
324
+ _config ,
325
+ % ExUnit.TestModule { setup_all?: false } = test_module ,
326
+ context ,
327
+ callback
328
+ ) do
329
+ { { :ok , callback . ( context ) } , test_module }
330
+ end
316
331
317
- { ^ module_pid , :setup_all , { :error , test_module } } ->
318
- invalid_tests = mark_tests_invalid ( tests , test_module )
319
- :ok = exit_setup_all ( module_pid , module_ref )
320
- { test_module , invalid_tests , [ ] }
332
+ defp run_setup_all ( config , % ExUnit.TestModule { name: module } = test_module , context , callback ) do
333
+ parent_pid = self ( )
321
334
322
- { :DOWN , ^ module_ref , :process , ^ module_pid , error } ->
323
- test_module = % { test_module | state: failed ( { :EXIT , module_pid } , error , [ ] ) }
324
- invalid_tests = mark_tests_invalid ( tests , test_module )
325
- { test_module , invalid_tests , [ ] }
326
- end
335
+ { module_pid , module_ref } =
336
+ spawn_monitor ( fn ->
337
+ ExUnit.OnExitHandler . register ( self ( ) )
338
+
339
+ result =
340
+ try do
341
+ { :ok , module . __ex_unit__ ( :setup_all , context ) }
342
+ catch
343
+ kind , error ->
344
+ failed = failed ( kind , error , prune_stacktrace ( __STACKTRACE__ ) )
345
+ { :error , % { test_module | state: failed } }
346
+ end
327
347
328
- timeout = get_timeout ( config , % { } )
329
- { exec_on_exit ( test_module , module_pid , timeout ) , invalid_tests , finished_tests }
330
- end
348
+ send ( parent_pid , { self ( ) , :setup_all , result } )
331
349
332
- defp mark_tests_invalid ( tests , test_module ) do
333
- Enum . map ( tests , & % { & 1 | state: { :invalid , test_module } } )
334
- end
350
+ # We keep the process alive so all of its resources
351
+ # stay alive until we run all tests in this case.
352
+ ref = Process . monitor ( parent_pid )
335
353
336
- defp run_setup_all (
337
- % ExUnit.TestModule { name: module , tags: tags , parameters: params } = test_module ,
338
- async? ,
339
- parent_pid
340
- ) do
341
- Process . put ( @ current_key , test_module )
342
-
343
- spawn_monitor ( fn ->
344
- ExUnit.OnExitHandler . register ( self ( ) )
345
- tags = tags |> Map . merge ( params ) |> Map . merge ( % { module: module , async: async? } )
346
-
347
- result =
348
- try do
349
- { :ok , module . __ex_unit__ ( :setup_all , Map . merge ( tags , params ) ) }
350
- catch
351
- kind , error ->
352
- failed = failed ( kind , error , prune_stacktrace ( __STACKTRACE__ ) )
353
- { :error , % { test_module | state: failed } }
354
+ receive do
355
+ { ^ parent_pid , :exit } -> :ok
356
+ { :DOWN , ^ ref , _ , _ , _ } -> :ok
354
357
end
358
+ end )
355
359
356
- send ( parent_pid , { self ( ) , :setup_all , result } )
360
+ { ok_or_error , test_module } =
361
+ receive do
362
+ { ^ module_pid , :setup_all , { :ok , context } } ->
363
+ finished_tests = callback . ( context )
364
+ :ok = exit_setup_all ( module_pid , module_ref )
365
+ { { :ok , finished_tests } , test_module }
357
366
358
- # We keep the process alive so all of its resources
359
- # stay alive until we run all tests in this case.
360
- ref = Process . monitor ( parent_pid )
367
+ { ^ module_pid , :setup_all , { :error , test_module } } ->
368
+ :ok = exit_setup_all ( module_pid , module_ref )
369
+ { :error , test_module }
361
370
362
- receive do
363
- { ^ parent_pid , :exit } -> :ok
364
- { :DOWN , ^ ref , _ , _ , _ } -> :ok
371
+ { :DOWN , ^ module_ref , :process , ^ module_pid , error } ->
372
+ { :error , % { test_module | state: failed ( { :EXIT , module_pid } , error , [ ] ) } }
365
373
end
366
- end )
374
+
375
+ timeout = get_timeout ( config , % { } )
376
+ { ok_or_error , exec_on_exit ( test_module , module_pid , timeout ) }
367
377
end
368
378
369
379
defp exit_setup_all ( pid , ref ) do
0 commit comments