@@ -426,32 +426,31 @@ def test_nested_op_api(self):
426426
427427 somehost = inventory .get_host ("somehost" )
428428
429- ctx_state .set (state )
430- ctx_host .set (somehost )
431-
432429 pyinfra .is_cli = True
433430
434- try :
435- outer_result = server .shell (commands = "echo outer" )
436- assert outer_result ._combined_output is None
431+ with ctx_state .use (state ):
432+ with ctx_host .use (somehost ):
433+ try :
434+ outer_result = server .shell (commands = "echo outer" )
435+ assert outer_result ._combined_output is None
437436
438- def callback ():
439- inner_result = server .shell (commands = "echo inner" )
440- assert inner_result ._combined_output is not None
437+ def callback ():
438+ inner_result = server .shell (commands = "echo inner" )
439+ assert inner_result ._combined_output is not None
441440
442- python .call (function = callback )
441+ python .call (function = callback )
443442
444- assert len (state .get_op_order ()) == 2
443+ assert len (state .get_op_order ()) == 2
445444
446- run_ops (state )
445+ run_ops (state )
447446
448- assert len (state .get_op_order ()) == 3
449- assert state .results [somehost ].success_ops == 3
450- assert outer_result ._combined_output is not None
447+ assert len (state .get_op_order ()) == 3
448+ assert state .results [somehost ].success_ops == 3
449+ assert outer_result ._combined_output is not None
451450
452- disconnect_all (state )
453- finally :
454- pyinfra .is_cli = False
451+ disconnect_all (state )
452+ finally :
453+ pyinfra .is_cli = False
455454
456455
457456class TestOperationFailures (PatchSSHTestCase ):
@@ -519,40 +518,40 @@ def test_cli_op_line_numbers(self):
519518 state .current_deploy_filename = __file__
520519
521520 pyinfra .is_cli = True
522- ctx_state .set (state )
523521
524- # Add op to both hosts
525- for name in ("anotherhost" , "somehost" ):
526- ctx_host .set (inventory .get_host (name ))
527- server .shell ("echo hi" ) # note this is called twice but on *the same line*
522+ with ctx_state .use (state ):
523+ # Add op to both hosts
524+ for name in ("anotherhost" , "somehost" ):
525+ with ctx_host .use (inventory .get_host (name )):
526+ server .shell ("echo hi" ) # note this is called twice but on *the same line*
528527
529- # Add op to just the second host - using the context modules such that
530- # it replicates a deploy file.
531- ctx_host .set (inventory .get_host ("anotherhost" ))
532- first_context_hash = server .user ("anotherhost_user" )._hash
528+ # Add op to just the second host - using the context modules such that
529+ # it replicates a deploy file.
530+ ctx_host .set (inventory .get_host ("anotherhost" ))
531+ first_context_hash = server .user ("anotherhost_user" )._hash
533532
534- # Add op to just the first host - using the context modules such that
535- # it replicates a deploy file.
536- ctx_host .set (inventory .get_host ("somehost" ))
537- second_context_hash = server .user ("somehost_user" )._hash
533+ # Add op to just the first host - using the context modules such that
534+ # it replicates a deploy file.
535+ ctx_host .set (inventory .get_host ("somehost" ))
536+ second_context_hash = server .user ("somehost_user" )._hash
538537
539- ctx_state .reset ()
540- ctx_host .reset ()
538+ ctx_state .reset ()
539+ ctx_host .reset ()
541540
542- pyinfra .is_cli = False
541+ pyinfra .is_cli = False
543542
544- print (state .ops )
545- # Ensure there are two ops
546- op_order = state .get_op_order ()
547- assert len (op_order ) == 3
543+ print (state .ops )
544+ # Ensure there are two ops
545+ op_order = state .get_op_order ()
546+ assert len (op_order ) == 3
548547
549- # And that the two ops above were called in the expected order
550- assert op_order [1 ] == first_context_hash
551- assert op_order [2 ] == second_context_hash
548+ # And that the two ops above were called in the expected order
549+ assert op_order [1 ] == first_context_hash
550+ assert op_order [2 ] == second_context_hash
552551
553- # Ensure somehost has two ops and anotherhost only has the one
554- assert len (state .ops [inventory .get_host ("somehost" )]) == 2
555- assert len (state .ops [inventory .get_host ("anotherhost" )]) == 2
552+ # Ensure somehost has two ops and anotherhost only has the one
553+ assert len (state .ops [inventory .get_host ("somehost" )]) == 2
554+ assert len (state .ops [inventory .get_host ("anotherhost" )]) == 2
556555
557556 # In API mode, pyinfra *overrides* the line numbers such that whenever an
558557 # operation or deploy is added it is simply appended. This makes sense as
0 commit comments