@@ -941,13 +941,34 @@ function with_source_path(f, path)
941941 end
942942end
943943
944- function ensure_setup! (ctx:: TestContext , ts:: TestSetup , logs:: Symbol )
944+ # Call `runtestsetup(ts, ...)` for each `ts::Testsetup` required by the given `TestItem`
945+ # Return setup_name => module_name pairs
946+ function runtestsetups (ti:: TestItem , ctx:: TestContext ; logs:: Symbol , force:: Bool = false )
947+ # Initialse with the list of _requested_ setups, so that if it no setup by that name was
948+ # found when including files we return the setup name as the module name. Attempting to
949+ # import that name, like `using $setup`, will then throw an appropriate error.
950+ setup_to_mod = Dict {Symbol,Symbol} (ti. setups .=> ti. setups)
951+ for (ts_name, ts) in ti. testsetups
952+ @debugv 1 " Ensuring setup for test item $(repr (ti. name)) $(ts_name)$(_on_worker ()) ."
953+ setup_to_mod[ts_name] = runtestsetup (ts, ctx; logs)
954+ end
955+ return setup_to_mod
956+ end
957+
958+ # Run the given `TestSetup`, add the resulting `Module` to the `TestContext` and returns the
959+ # name of the `Module` (i.e. returns a `Symbol`).
960+ # If the `TestSetup` has already been evaluated on this process and so is already in the
961+ # `TestContext`, simply returns the `Module` name.
962+ # Pass `force=true` to force the `TestSetup` to be re-evaluated, even if run before.
963+ function runtestsetup (ts:: TestSetup , ctx:: TestContext ; logs:: Symbol , force:: Bool = false )
945964 mods = ctx. setups_evaled
946965 @lock mods. lock begin
947- mod = get (mods. modules, ts. name, nothing )
948- if mod != = nothing
949- # we've eval-ed this module before, so just return the module name
950- return nameof (mod)
966+ if ! force
967+ mod = get (mods. modules, ts. name, nothing )
968+ if mod != = nothing
969+ # we've eval-ed this module before, so just return the module name
970+ return nameof (mod)
971+ end
951972 end
952973 # We haven't eval-ed this module before, so we need to eval it.
953974 # In case the setup fails to eval, we discard its logs -- we will attempt to eval
@@ -1061,24 +1082,16 @@ function runtestitem(
10611082 prev = get (task_local_storage (), :__TESTITEM_ACTIVE__ , false )
10621083 task_local_storage ()[:__TESTITEM_ACTIVE__ ] = true
10631084 try
1064- for (setup_name, ts) in ti. testsetups
1065- # ensure setup has been evaled before
1066- @debugv 1 " Ensuring setup for test item $(repr (name)) $(setup_name)$(_on_worker ()) ."
1067- ts_mod = ensure_setup! (ctx, ts, logs)
1068- # eval using in our @testitem module
1069- @debugv 1 " Importing setup for test item $(repr (name)) $(setup_name)$(_on_worker ()) ."
1085+ # eval `using $TestSetup` in our @testitem module.
1086+ testsetups = runtestsetups (ti, ctx; logs)
1087+ for (ts_name, ts_module_name) in testsetups
1088+ @debugv 1 " Add setup imports for test item $(repr (name)) $(setup_name)$(_on_worker ()) ."
10701089 # We look up the testsetups from Main (since tests are eval'd in their own
10711090 # temporary anonymous module environment.)
1072- push! (body. args, Expr (:using , Expr (:., :Main , ts_mod)))
1073- # ts_mod is a gensym'd name so that setup modules don't clash
1074- # so we set a const alias inside our @testitem module to make things work
1075- push! (body. args, :(const $ setup_name = $ ts_mod))
1076- end
1077- for setup_name in setdiff (ti. setups, keys (ti. testsetups))
1078- # if the setup was requested but is not in our testsetups, then it was never
1079- # found when including files. We still add `using $setup` in the test item
1080- # so that we throw an appropriate error when running the test item.
1081- push! (body. args, Expr (:using , Expr (:., :Main , setup_name)))
1091+ push! (body. args, Expr (:using , Expr (:., :Main , ts_module_name)))
1092+ # ts_module_name is a gensym'd name so that setup modules don't clash,
1093+ # so set a const alias inside our @testitem module to make things work.
1094+ push! (body. args, :(const $ ts_name = $ ts_module_name))
10821095 end
10831096 @debugv 1 " Setup for test item $(repr (name)) done$(_on_worker ()) ."
10841097
0 commit comments