Commit 0f4c52b0 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Apply formatter changes

parent 37eba599
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -26,14 +26,18 @@ def with_anyio(timeout=10):
	"""
	Create a wrapping decorator to run asynchronous test functions
	"""

	def decorator(testfunc):
		async def test_async_wrapper(args):
			async with anyio.fail_after(timeout):
				return await testfunc(*args)

		@wraps(testfunc)
		def test_wrapper(*args):
			return anyio.run(test_async_wrapper, args)

		return test_wrapper

	return decorator


+14 −11
Original line number Diff line number Diff line
@@ -27,8 +27,10 @@ from wpa_supplicant import errors
from wpa_supplicant.client import base


@mock.patch('wpa_supplicant.util.connect_unix_datagram', new_callable=anyio_mock.AsyncMock)
@mock.patch('wpa_supplicant.client.base.BaseClient.send_command', new_callable=anyio_mock.AsyncMock)
@mock.patch("wpa_supplicant.util.connect_unix_datagram", new_callable=anyio_mock.AsyncMock)
@mock.patch(
	"wpa_supplicant.client.base.BaseClient.send_command", new_callable=anyio_mock.AsyncMock
)
class ConnectTests(unittest.TestCase):
	"""
	Tests for the connect() method
@@ -75,6 +77,7 @@ class SendMessageTests(unittest.TestCase):
	def setUp(self):
		async def _make_client():
			return base.BaseClient()

		self.client = client = anyio.run(_make_client)
		client.sock = anyio_mock.AsyncMock()
		client.sock.send.side_effect = len
@@ -118,10 +121,9 @@ class SendMessageTests(unittest.TestCase):
			client.sock.recv.return_value = b"FOO\nBAR\nBAZ\n"
			self.assertListEqual(
				await client.send_command(
					"SOME_COMMAND",
					convert=lambda x: x.splitlines(),
					"SOME_COMMAND", convert=lambda x: x.splitlines(),
				),
				["FOO", "BAR", "BAZ"]
				["FOO", "BAR", "BAZ"],
			)

	@anyio_mock.with_anyio()
@@ -133,11 +135,9 @@ class SendMessageTests(unittest.TestCase):
			client.sock.recv.return_value = b"FOO\nBAR\nBAZ\n"
			self.assertListEqual(
				await client.send_command(
					"SOME_COMMAND",
					convert=lambda x: x.splitlines(),
					expect="PONG",
					"SOME_COMMAND", convert=lambda x: x.splitlines(), expect="PONG",
				),
				["FOO", "BAR", "BAZ"]
				["FOO", "BAR", "BAZ"],
			)

	@anyio_mock.with_anyio()
@@ -180,7 +180,7 @@ class SendMessageTests(unittest.TestCase):
				b"<2>SOME-MESSAGE",
				b"<1>SOME-OTHER-MESSAGE with|args",
				b"OK",
				b"<2>SOME-MESSAGE"
				b"<2>SOME-MESSAGE",
			]
			assert await client.send_command("SOME_COMMAND") is None

@@ -208,6 +208,7 @@ class EventTests(unittest.TestCase):
	def setUp(self):
		async def _make_client():
			return base.BaseClient()

		self.client = client = anyio.run(_make_client)
		client.sock = anyio_mock.AsyncMock()
		client.sock.send.side_effect = len
@@ -262,7 +263,9 @@ class EventTests(unittest.TestCase):
				b"OK",  # Respond to DETACH
				b"<3>OTHER-MESSAGE",
			]
			prio, evt, args = await client.event("EXAMPLE-EVENT1", "EXAMPLE-EVENT2", "EXAMPLE-EVENT3")
			prio, evt, args = await client.event(
				"EXAMPLE-EVENT1", "EXAMPLE-EVENT2", "EXAMPLE-EVENT3"
			)
			assert prio == 4
			assert evt == "EXAMPLE-EVENT3"
			assert args is None
+2 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ class MethodsTests(unittest.TestCase):
	def setUp(self):
		async def _make_client():
			return interfaces.InterfaceClient()

		self.client = client = anyio.run(_make_client)
		client.sock = anyio_mock.AsyncMock()
		client.sock.send.side_effect = len
@@ -55,4 +56,4 @@ class MethodsTests(unittest.TestCase):

			async for bss in client.scan():
				self.assertIsInstance(bss, dict)
				self.assertIn('good', bss)
				self.assertIn("good", bss)
+23 −15
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ class InterfaceMethodsTests(unittest.TestCase):
	def setUp(self):
		async def _make_client():
			return master.MasterClient()

		self.client = client = anyio.run(_make_client)
		client.sock = anyio_mock.AsyncMock()
		client.sock.send.side_effect = len
@@ -47,8 +48,10 @@ class InterfaceMethodsTests(unittest.TestCase):
		client1 = master.MasterClient()
		client2 = master.MasterClient()

		with patch("wpa_supplicant.client.base.BaseClient.connect",
				new_callable=anyio_mock.AsyncMock):
		with patch(
			"wpa_supplicant.client.base.BaseClient.connect",
			new_callable=anyio_mock.AsyncMock,
		):
			await client1.connect("/tmp/foo/bar")
			await client2.connect(pathlib.Path("/tmp/foo/bar"))

@@ -64,15 +67,16 @@ class InterfaceMethodsTests(unittest.TestCase):
		Check list_interfaces() processes lines of names in a list
		"""
		async with self.client as client:
			# fmt: off
			client.sock.recv.return_value = (
				b"enp0s0\n"
				b"enp1s0\n"
				b"wlp2s0\n"
			)
			# fmt: on

			self.assertListEqual(
				await client.list_interfaces(),
				["enp0s0", "enp1s0", "wlp2s0"]
				await client.list_interfaces(), ["enp0s0", "enp1s0", "wlp2s0"]
			)

			client.sock.send.assert_called_once_with(b"INTERFACES")
@@ -83,29 +87,31 @@ class InterfaceMethodsTests(unittest.TestCase):
		Check add_interface() sends the correct arguments
		"""
		async with self.client as client:
			client.ctrl_dir = pathlib.Path('/tmp')
			client.ctrl_dir = pathlib.Path("/tmp")
			client.sock.recv.return_value = b"OK"

			assert await client.add_interface('enp1s0', driver='wired') is None
			assert await client.add_interface("enp1s0", driver="wired") is None

			client.sock.send.assert_called_once()
			args = client.sock.send.call_args[0]
			assert args[0].startswith(b"INTERFACE_ADD enp1s0\t\twired\tDIR=/tmp GROUP=")

	@patch('wpa_supplicant.client.interfaces.InterfaceClient.connect',
			new_callable=anyio_mock.AsyncMock)
	@patch(
		"wpa_supplicant.client.interfaces.InterfaceClient.connect",
		new_callable=anyio_mock.AsyncMock,
	)
	@anyio_mock.with_anyio()
	async def test_connect_interface(self, connect_mock):
		"""
		Check connect_interface() returns a connected InterfaceClient
		"""
		async with self.client as client:
			client.ctrl_dir = pathlib.Path('/tmp')
			client.ctrl_dir = pathlib.Path("/tmp")
			client.sock.recv.side_effect = [
				b"enp1s0\n",  # Response to INTERFACES
			]

			ifclient = await client.connect_interface('enp1s0')
			ifclient = await client.connect_interface("enp1s0")

			self.assertIsInstance(ifclient, interfaces.InterfaceClient)
			connect_mock.assert_called_once_with("/tmp/enp1s0")
@@ -113,21 +119,23 @@ class InterfaceMethodsTests(unittest.TestCase):
			# Check only INTERFACES was sent
			client.sock.send.assert_called_once_with(b"INTERFACES")

	@patch('wpa_supplicant.client.interfaces.InterfaceClient.connect',
			new_callable=anyio_mock.AsyncMock)
	@patch(
		"wpa_supplicant.client.interfaces.InterfaceClient.connect",
		new_callable=anyio_mock.AsyncMock,
	)
	@anyio_mock.with_anyio()
	async def test_connect_interface_with_add(self, connect_mock):
		"""
		Check connect_interface() adds the interface when not already managed
		"""
		async with self.client as client:
			client.ctrl_dir = pathlib.Path('/tmp')
			client.ctrl_dir = pathlib.Path("/tmp")
			client.sock.recv.side_effect = [
				b"",  # Response to INTERFACES
				b"OK",  # Response to INTERFACE_ADD
			]

			ifclient = await client.connect_interface('enp1s0')
			ifclient = await client.connect_interface("enp1s0")

			self.assertIsInstance(ifclient, interfaces.InterfaceClient)
			connect_mock.assert_called_once_with("/tmp/enp1s0")
+3 −3
Original line number Diff line number Diff line
@@ -33,13 +33,13 @@ class AnyIOTests(unittest.TestCase):
	"""

	@mocketize
	@patch('mocket.mocket.MocketSocket.getsockopt', return_value=0)
	@patch('mocket.mocket.MocketSocket.connect')
	@patch("mocket.mocket.MocketSocket.getsockopt", return_value=0)
	@patch("mocket.mocket.MocketSocket.connect")
	def test_connect_unix_datagram(self, mock_connect, mock_getsockopt):
		"""
		Check that a socket of the right type is wrapped, bound and connected after a call
		"""
		test_path = '/some/path'
		test_path = "/some/path"
		Mocket.register(MocketEntry(test_path, []))

		# Perform the action
Loading