Skip to content

Commit e940012

Browse files
committed
tests: use, rather than set, context
1 parent 39d1d31 commit e940012

File tree

3 files changed

+171
-159
lines changed

3 files changed

+171
-159
lines changed

tests/test_api/test_api_operations.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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

457456
class 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

tests/test_cli/test_context_objects.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from pyinfra.context import ctx_host, ctx_inventory
66

77

8-
def _create_host():
9-
return Host(None, None, None, None)
8+
def _create_host(name: str = "host"):
9+
return Host(name, Inventory(([name], {})), None, None)
1010

1111

1212
class TestHostContextObject(TestCase):
@@ -15,59 +15,59 @@ def test_context_host_dir_matches_class(self):
1515

1616
def test_context_host_eq(self):
1717
host_obj = _create_host()
18-
ctx_host.set(host_obj)
19-
assert host == host_obj
18+
with ctx_host.use(host_obj):
19+
assert host == host_obj
2020

2121
def test_context_host_repr(self):
22-
host_obj = _create_host()
23-
ctx_host.set(host_obj)
24-
assert repr(host) == "ContextObject(Host):Host(None)"
22+
host_obj = _create_host("somehost")
23+
with ctx_host.use(host_obj):
24+
assert repr(host) == "ContextObject(Host):Host(somehost)"
2525

2626
def test_context_host_str(self):
2727
host_obj = _create_host()
28-
ctx_host.set(host_obj)
29-
assert str(host_obj) == "None"
28+
with ctx_host.use(host_obj):
29+
assert str(host_obj) == "host"
3030

3131
def test_context_host_attr(self):
3232
host_obj = _create_host()
33-
ctx_host.set(host_obj)
3433

3534
with self.assertRaises(AttributeError):
3635
host_obj.hello
3736

38-
setattr(host, "hello", "world")
39-
assert host_obj.hello == host.hello
37+
with ctx_host.use(host_obj):
38+
setattr(host, "hello", "world")
39+
assert host_obj.hello == host.hello
4040

4141
def test_context_host_class_attr(self):
4242
host_obj = _create_host()
43-
ctx_host.set(host_obj)
44-
assert ctx_host.isset() is True
4543

46-
with self.assertRaises(AttributeError):
47-
host_obj.hello
44+
with ctx_host.use(host_obj):
45+
assert ctx_host.isset() is True
46+
47+
with self.assertRaises(AttributeError):
48+
host_obj.hello
4849

49-
setattr(Host, "hello", "class_world")
50-
setattr(host_obj, "hello", "instance_world")
50+
setattr(Host, "hello", "class_world")
51+
setattr(host_obj, "hello", "instance_world")
5152

52-
assert host.hello == host.hello
53+
assert host.hello == host.hello
5354

5455
# Reset and check fallback to class variable
55-
ctx_host.reset()
5656
assert ctx_host.isset() is False
5757
assert host.hello == "class_world"
5858

5959

6060
class TestInventoryContextObject(TestCase):
6161
def test_context_inventory_len(self):
6262
inventory_obj = Inventory(("host", "anotherhost"))
63-
ctx_inventory.set(inventory_obj)
64-
assert ctx_inventory.isset() is True
6563

66-
assert len(inventory) == len(inventory_obj)
64+
with ctx_inventory.use(inventory_obj):
65+
assert ctx_inventory.isset() is True
66+
assert len(inventory) == len(inventory_obj)
6767

6868
def test_context_inventory_iter(self):
6969
inventory_obj = Inventory(("host", "anotherhost"))
70-
ctx_inventory.set(inventory_obj)
71-
assert ctx_inventory.isset() is True
7270

73-
assert list(iter(inventory)) == list(iter(inventory_obj))
71+
with ctx_inventory.use(inventory_obj):
72+
assert ctx_inventory.isset() is True
73+
assert list(iter(inventory)) == list(iter(inventory_obj))

0 commit comments

Comments
 (0)