Skip to content

Commit

Permalink
Add a helper function for the last change, to make coverage easier
Browse files Browse the repository at this point in the history
  • Loading branch information
ronf committed Dec 27, 2023
1 parent 5816813 commit 8b55fd9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
7 changes: 2 additions & 5 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from asyncssh.packet import Byte, String, UInt32

from .sk_stub import sk_available, patch_sk
from .util import AsyncTestCase, asynctest, get_test_key, run
from .util import AsyncTestCase, asynctest, get_test_key, run, try_remove


def agent_test(func):
Expand Down Expand Up @@ -85,10 +85,7 @@ async def stop(self):
self._server.close()
await self._server.wait_closed()

try:
os.remove(self._path)
except OSError:
pass
try_remove(self._path)


class _TestAgent(AsyncTestCase):
Expand Down
49 changes: 11 additions & 38 deletions tests/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from asyncssh.socks import SOCKS4_OK_RESPONSE, SOCKS5_OK_RESPONSE_HDR

from .server import Server, ServerTestCase
from .util import asynctest, echo, make_certificate
from .util import asynctest, echo, make_certificate, try_remove


def _echo_non_async(stdin, stdout, stderr=None):
Expand Down Expand Up @@ -651,10 +651,7 @@ async def test_forward_local_path_to_port(self):
async with conn.forward_local_path_to_port('local', '', 7):
await self._check_local_unix_connection('local')

try:
os.remove('local')
except OSError:
pass
try_remove('local')

@unittest.skipIf(sys.platform == 'win32',
'skip UNIX domain socket tests on Windows')
Expand All @@ -668,10 +665,7 @@ async def test_forward_local_path_to_port_failure(self):
with self.assertRaises(OSError):
await conn.forward_local_path_to_port('local', '', 7)

try:
os.remove('local')
except OSError:
pass
try_remove('local')

@asynctest
async def test_forward_local_port_pause(self):
Expand Down Expand Up @@ -805,10 +799,7 @@ async def test_forward_remote_port_to_path(self):
server.close()
await server.wait_closed()

try:
os.remove('local')
except OSError:
pass
try_remove('local')

@asynctest
async def test_forward_remote_specific_port(self):
Expand Down Expand Up @@ -1030,10 +1021,7 @@ async def test_unix_server(self):
await listener.wait_closed()
listener.close()

try:
os.remove('echo')
except OSError:
pass
try_remove('echo')

@asynctest
async def test_unix_server_open(self):
Expand Down Expand Up @@ -1066,10 +1054,7 @@ async def test_unix_server_non_async(self):
async with conn.start_unix_server(_unix_listener_non_async, path):
await self._check_local_unix_connection('echo')

try:
os.remove('echo')
except OSError:
pass
try_remove('echo')

@asynctest
async def test_unix_server_failure(self):
Expand All @@ -1087,10 +1072,7 @@ async def test_forward_local_path(self):
async with conn.forward_local_path('local', '/echo'):
await self._check_local_unix_connection('local')

try:
os.remove('local')
except OSError:
pass
try_remove('local')

@asynctest
async def test_forward_local_port_to_path_accept_handler(self):
Expand Down Expand Up @@ -1168,11 +1150,8 @@ async def test_forward_remote_path(self):
server.close()
await server.wait_closed()

try:
os.remove('echo')
os.remove('local')
except OSError:
pass
try_remove('echo')
try_remove('local')

@asynctest
async def test_forward_remote_path_to_port(self):
Expand All @@ -1192,10 +1171,7 @@ async def test_forward_remote_path_to_port(self):
server.close()
await server.wait_closed()

try:
os.remove('echo')
except OSError:
pass
try_remove('echo')

@asynctest
async def test_forward_remote_path_failure(self):
Expand All @@ -1209,10 +1185,7 @@ async def test_forward_remote_path_failure(self):
with self.assertRaises(asyncssh.ChannelListenError):
await conn.forward_remote_path(path, 'local')

try:
os.remove('echo')
except OSError:
pass
try_remove('echo')

@asynctest
async def test_forward_remote_path_not_permitted(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ def run(cmd):
raise


def try_remove(filename):
"""Try to remove a file, ignoring errors"""

try:
os.remove(filename)
except OSError: # pragma: no cover
pass


class ConnectionStub:
"""Stub class used to replace an SSHConnection object"""

Expand Down

0 comments on commit 8b55fd9

Please sign in to comment.