@@ -1198,7 +1198,7 @@ def test_taskhooks_1(tmpdir: Path, capsys):
11981198 cache_dir .mkdir ()
11991199
12001200 foo = Task (
1201- definition = FunAddTwo (a = 1 ), submitter = Submitter (cache_dir = cache_dir ), name = "foo"
1201+ definition = FunAddTwo (a = 1 ), submitter = Submitter (cache_dir = tmpdir ), name = "foo"
12021202 )
12031203 assert foo .hooks
12041204 # ensure all hooks are defined
@@ -1209,8 +1209,7 @@ def test_taskhooks_1(tmpdir: Path, capsys):
12091209 def myhook (task , * args ):
12101210 print ("I was called" )
12111211
1212- foo .hooks .pre_run = myhook
1213- foo .run ()
1212+ FunAddTwo (a = 1 )(cache_dir = cache_dir , pre_run = myhook )
12141213 captured = capsys .readouterr ()
12151214 assert "I was called\n " in captured .out
12161215 del captured
@@ -1219,52 +1218,31 @@ def myhook(task, *args):
12191218 with pytest .raises (AttributeError ):
12201219 foo .hooks .mid_run = myhook
12211220
1221+ # reset all hooks
1222+ foo .hooks .reset ()
1223+ for attr in ("pre_run" , "post_run" , "pre_run_task" , "post_run_task" ):
1224+ hook = getattr (foo .hooks , attr )
1225+ assert hook () is None
1226+
12221227 # clear cache
12231228 shutil .rmtree (cache_dir )
12241229 cache_dir .mkdir ()
12251230
12261231 # set all hooks
1227- foo .hooks .post_run = myhook
1228- foo .hooks .pre_run_task = myhook
1229- foo .hooks .post_run_task = myhook
1230- foo .run ()
1231- captured = capsys .readouterr ()
1232- assert captured .out .count ("I was called\n " ) == 4
1233- del captured
1234-
1235- # hooks are independent across tasks by default
1236- bar = Task (
1237- definition = FunAddTwo (a = 3 ), name = "bar" , submitter = Submitter (cache_dir = tmpdir )
1232+ FunAddTwo (a = 1 )(
1233+ cache_dir = cache_dir ,
1234+ pre_run = myhook ,
1235+ post_run = myhook ,
1236+ pre_run_task = myhook ,
1237+ post_run_task = myhook ,
12381238 )
1239- assert bar .hooks is not foo .hooks
1240- # but can be shared across tasks
1241- bar .hooks = foo .hooks
1242- # and workflows
1243- wf_task = Task (
1244- definition = BasicWorkflow (x = 1 ),
1245- submitter = Submitter (cache_dir = tmpdir , worker = "cf" ),
1246- name = "wf" ,
1247- )
1248- wf_task .hooks = bar .hooks
1249- assert foo .hooks == bar .hooks == wf_task .hooks
1250-
1251- wf_task .run ()
12521239 captured = capsys .readouterr ()
12531240 assert captured .out .count ("I was called\n " ) == 4
12541241 del captured
12551242
1256- # reset all hooks
1257- foo .hooks .reset ()
1258- for attr in ("pre_run" , "post_run" , "pre_run_task" , "post_run_task" ):
1259- hook = getattr (foo .hooks , attr )
1260- assert hook () is None
1261-
12621243
12631244def test_taskhooks_2 (tmpdir , capsys ):
12641245 """checking order of the hooks; using task's attributes"""
1265- foo = Task (
1266- definition = FunAddTwo (a = 1 ), name = "foo" , submitter = Submitter (cache_dir = tmpdir )
1267- )
12681246
12691247 def myhook_prerun (task , * args ):
12701248 print (f"i. prerun hook was called from { task .name } " )
@@ -1278,11 +1256,13 @@ def myhook_postrun_task(task, *args):
12781256 def myhook_postrun (task , * args ):
12791257 print (f"iv. postrun hook was called { task .name } " )
12801258
1281- foo .hooks .pre_run = myhook_prerun
1282- foo .hooks .post_run = myhook_postrun
1283- foo .hooks .pre_run_task = myhook_prerun_task
1284- foo .hooks .post_run_task = myhook_postrun_task
1285- foo .run ()
1259+ FunAddTwo (a = 1 )(
1260+ cache_dir = tmpdir ,
1261+ pre_run = myhook_prerun ,
1262+ post_run = myhook_postrun ,
1263+ pre_run_task = myhook_prerun_task ,
1264+ post_run_task = myhook_postrun_task ,
1265+ )
12861266
12871267 captured = capsys .readouterr ()
12881268 hook_messages = captured .out .strip ().split ("\n " )
@@ -1318,21 +1298,17 @@ def myhook_postrun(task, result, *args):
13181298
13191299def test_taskhooks_4 (tmpdir , capsys ):
13201300 """task raises an error: postrun task should be called, postrun shouldn't be called"""
1321- foo = Task (
1322- definition = FunAddTwo (a = "one" ), name = "foo" , submitter = Submitter (cache_dir = tmpdir )
1323- )
13241301
13251302 def myhook_postrun_task (task , result , * args ):
13261303 print (f"postrun task hook was called, result object is { result } " )
13271304
13281305 def myhook_postrun (task , result , * args ):
13291306 print ("postrun hook should not be called" )
13301307
1331- foo .hooks .post_run = myhook_postrun
1332- foo .hooks .post_run_task = myhook_postrun_task
1333-
13341308 with pytest .raises (Exception ):
1335- foo ()
1309+ FunAddTwo (a = "one" )(
1310+ cache_dir = tmpdir , post_run = myhook_postrun , post_run_task = myhook_postrun_task
1311+ )
13361312
13371313 captured = capsys .readouterr ()
13381314 hook_messages = captured .out .strip ().split ("\n " )
@@ -1351,11 +1327,13 @@ def test_traceback(tmpdir):
13511327 def FunError (x ):
13521328 raise Exception ("Error from the function" )
13531329
1354- with pytest .raises (Exception , match = "Task 'FunError' raised an error " ) as exinfo :
1330+ with pytest .raises (Exception , match = "Error from the function " ) as exinfo :
13551331 FunError (x = 3 )(worker = "cf" , cache_dir = tmpdir )
13561332
13571333 # getting error file from the error message
1358- error_file_match = str (exinfo .value ).split ("here: " )[- 1 ].split ("_error.pklz" )[0 ]
1334+ error_file_match = (
1335+ str (exinfo .value .__notes__ [0 ]).split ("here: " )[- 1 ].split ("_error.pklz" )[0 ]
1336+ )
13591337 error_file = Path (error_file_match ) / "_error.pklz"
13601338 # checking if the file exists
13611339 assert error_file .exists ()
@@ -1386,7 +1364,9 @@ def Workflow(x_list):
13861364 wf (worker = "cf" )
13871365
13881366 # getting error file from the error message
1389- error_file_match = str (exinfo .value ).split ("here: " )[- 1 ].split ("_error.pklz" )[0 ]
1367+ error_file_match = (
1368+ str (exinfo .value ).split ("here: " )[- 1 ].split ("_error.pklz" )[0 ].strip ()
1369+ )
13901370 error_file = Path (error_file_match ) / "_error.pklz"
13911371 # checking if the file exists
13921372 assert error_file .exists ()
0 commit comments