Skip to content

Commit 738aa3a

Browse files
committed
q-dev: replace overriding with patching
1 parent 665e9cd commit 738aa3a

File tree

1 file changed

+54
-45
lines changed

1 file changed

+54
-45
lines changed

qubesusbproxy/tests.py

+54-45
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,13 @@ def test_010_on_qdb_change_multiple_assignments_including_full(self):
722722
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
723723
)
724724

725-
self.ext.attach_and_notify = AsyncMock()
726725
loop = asyncio.get_event_loop()
727-
loop.run_until_complete(self.ext.on_domain_start(front, None))
728-
self.assertEqual(
729-
self.ext.attach_and_notify.call_args[0][1].options, {"pid": "did"}
730-
)
726+
with (mock.patch.object(self.ext, "attach_and_notify")
727+
as attach_and_notify):
728+
loop.run_until_complete(self.ext.on_domain_start(front, None))
729+
self.assertEqual(
730+
attach_and_notify.call_args[0][1].options, {"pid": "did"}
731+
)
731732

732733
def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
733734
back, front = self.added_assign_setup()
@@ -752,12 +753,13 @@ def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
752753
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
753754
)
754755

755-
self.ext.attach_and_notify = AsyncMock()
756756
loop = asyncio.get_event_loop()
757-
loop.run_until_complete(self.ext.on_domain_start(front, None))
758-
self.assertEqual(
759-
self.ext.attach_and_notify.call_args[0][1].options, {"pid": "any"}
760-
)
757+
with (mock.patch.object(self.ext, "attach_and_notify")
758+
as attach_and_notify):
759+
loop.run_until_complete(self.ext.on_domain_start(front, None))
760+
self.assertEqual(
761+
attach_and_notify.call_args[0][1].options, {"pid": "any"}
762+
)
761763

762764
def test_012_on_qdb_change_multiple_assignments_dev(self):
763765
back, front = self.added_assign_setup()
@@ -785,12 +787,13 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
785787
qubesusbproxy.core3ext.USBDevice(Port(back, "1-2", "usb"))
786788
)
787789

788-
self.ext.attach_and_notify = AsyncMock()
789790
loop = asyncio.get_event_loop()
790-
loop.run_until_complete(self.ext.on_domain_start(front, None))
791-
self.assertEqual(
792-
self.ext.attach_and_notify.call_args[0][1].options, {"any": "did"}
793-
)
791+
with (mock.patch.object(self.ext, "attach_and_notify")
792+
as attach_and_notify):
793+
loop.run_until_complete(self.ext.on_domain_start(front, None))
794+
self.assertEqual(
795+
attach_and_notify.call_args[0][1].options, {"any": "did"}
796+
)
794797

795798
def test_013_on_qdb_change_two_fronts(self):
796799
back, front = self.added_assign_setup()
@@ -826,13 +829,14 @@ def test_014_failed_confirmation(self, socket):
826829
socket.return_value = "allow:nonsense"
827830

828831
loop = asyncio.get_event_loop()
829-
self.ext.attach_and_notify = AsyncMock()
830-
loop.run_until_complete(
831-
qubes.ext.utils.resolve_conflicts_and_attach(
832-
self.ext, {"1-1": {front: assmnt, back: assmnt}}
832+
with (mock.patch.object(self.ext, "attach_and_notify")
833+
as attach_and_notify):
834+
loop.run_until_complete(
835+
qubes.ext.utils.resolve_conflicts_and_attach(
836+
self.ext, {"1-1": {front: assmnt, back: assmnt}}
837+
)
833838
)
834-
)
835-
self.ext.attach_and_notify.assert_not_called()
839+
attach_and_notify.assert_not_called()
836840

837841
# call_socket_service returns coroutine
838842
@unittest.mock.patch(
@@ -850,13 +854,14 @@ def test_015_successful_confirmation(self, socket):
850854
socket.return_value = "allow:front-vm"
851855

852856
loop = asyncio.get_event_loop()
853-
self.ext.attach_and_notify = AsyncMock()
854-
loop.run_until_complete(
855-
qubes.ext.utils.resolve_conflicts_and_attach(
856-
self.ext, {"1-1": {front: assmnt, back: assmnt}}
857+
with (mock.patch.object(self.ext, "attach_and_notify")
858+
as attach_and_notify):
859+
loop.run_until_complete(
860+
qubes.ext.utils.resolve_conflicts_and_attach(
861+
self.ext, {"1-1": {front: assmnt, back: assmnt}}
862+
)
857863
)
858-
)
859-
self.ext.attach_and_notify.assert_called_once_with(front, assmnt)
864+
attach_and_notify.assert_called_once_with(front, assmnt)
860865

861866
def test_016_on_qdb_change_ask(self):
862867
back, front = self.added_assign_setup()
@@ -902,12 +907,13 @@ def test_020_on_startup_multiple_assignments_including_full(self):
902907
qubesusbproxy.core3ext.USBDevice(Port(back, "1-1", "usb"))
903908
)
904909

905-
self.ext.attach_and_notify = AsyncMock()
906910
loop = asyncio.get_event_loop()
907-
loop.run_until_complete(self.ext.on_domain_start(front, None))
908-
self.assertEqual(
909-
self.ext.attach_and_notify.call_args[0][1].options, {"pid": "did"}
910-
)
911+
with (mock.patch.object(self.ext, "attach_and_notify")
912+
as attach_and_notify):
913+
loop.run_until_complete(self.ext.on_domain_start(front, None))
914+
self.assertEqual(
915+
attach_and_notify.call_args[0][1].options, {"pid": "did"}
916+
)
911917

912918
def test_021_on_startup_multiple_assignments_port_vs_dev(self):
913919
back, front = self.added_assign_setup()
@@ -933,11 +939,12 @@ def test_021_on_startup_multiple_assignments_port_vs_dev(self):
933939
)
934940

935941
loop = asyncio.get_event_loop()
936-
self.ext.attach_and_notify = AsyncMock()
937-
loop.run_until_complete(self.ext.on_domain_start(front, None))
938-
self.assertEqual(
939-
self.ext.attach_and_notify.call_args[0][1].options, {"pid": "any"}
940-
)
942+
with (mock.patch.object(self.ext, "attach_and_notify")
943+
as attach_and_notify):
944+
loop.run_until_complete(self.ext.on_domain_start(front, None))
945+
self.assertEqual(
946+
attach_and_notify.call_args[0][1].options, {"pid": "any"}
947+
)
941948

942949
def test_022_on_startup_multiple_assignments_dev(self):
943950
back, front = self.added_assign_setup()
@@ -965,12 +972,13 @@ def test_022_on_startup_multiple_assignments_dev(self):
965972
qubesusbproxy.core3ext.USBDevice(Port(back, "1-2", "usb"))
966973
)
967974

968-
self.ext.attach_and_notify = AsyncMock()
969975
loop = asyncio.get_event_loop()
970-
loop.run_until_complete(self.ext.on_domain_start(front, None))
971-
self.assertEqual(
972-
self.ext.attach_and_notify.call_args[0][1].options, {"any": "did"}
973-
)
976+
with (mock.patch.object(self.ext, "attach_and_notify")
977+
as attach_and_notify):
978+
loop.run_until_complete(self.ext.on_domain_start(front, None))
979+
self.assertEqual(
980+
attach_and_notify.call_args[0][1].options, {"any": "did"}
981+
)
974982

975983
def test_023_on_startup_already_attached(self):
976984
back, front = self.added_assign_setup(attachment="sys-usb")
@@ -983,10 +991,11 @@ def test_023_on_startup_already_attached(self):
983991
front.devices["usb"]._assigned.append(assmnt)
984992
back.devices["usb"]._exposed.append(exp_dev)
985993

986-
self.ext.attach_and_notify = AsyncMock()
987994
loop = asyncio.get_event_loop()
988-
loop.run_until_complete(self.ext.on_domain_start(front, None))
989-
self.ext.attach_and_notify.assert_not_called()
995+
with (mock.patch.object(self.ext, "attach_and_notify")
996+
as attach_and_notify):
997+
loop.run_until_complete(self.ext.on_domain_start(front, None))
998+
attach_and_notify.assert_not_called()
990999

9911000

9921001
def list_tests():

0 commit comments

Comments
 (0)