From 64d54e942c8839acdb615bb9cbe402d7c9bd0709 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Sep 2013 17:25:58 +0100 Subject: Convert SFTPClient to a context manager --- tests/test_sftp.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 1ae9781d..e3f7e59b 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -195,6 +195,18 @@ class SFTPTest (unittest.TestCase): pass sftp = paramiko.SFTP.from_transport(tc) + def test_2_sftp_can_be_used_as_context_manager(self): + """ + verify that the sftp session is closed when exiting the context manager + """ + global sftp + with sftp: + pass + try: + self._assert_opening_file_raises_error(sftp) + finally: + sftp = paramiko.SFTP.from_transport(tc) + def test_3_write(self): """ verify that a file can be created and written, and the size is correct. @@ -796,6 +808,15 @@ class SFTPTest (unittest.TestCase): sftp.remove('%s/nonutf8data' % FOLDER) + def _assert_opening_file_raises_error(self, sftp): + try: + sftp.open(FOLDER + '/test2', 'w') + self.fail('expected exception') + # TODO: extract failing command (the open) to share with a passing assertion + except EOFError as error: + pass + + if __name__ == '__main__': SFTPTest.init_loopback() # logging is required by test_N_file_with_percent -- cgit v1.2.3 From a6c8b1118c5dd7e49d75cc87298709a545f9d74b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Sep 2013 17:41:33 +0100 Subject: Fix error on Python 2.5 --- tests/test_sftp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index e3f7e59b..743a7831 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -813,7 +813,7 @@ class SFTPTest (unittest.TestCase): sftp.open(FOLDER + '/test2', 'w') self.fail('expected exception') # TODO: extract failing command (the open) to share with a passing assertion - except EOFError as error: + except EOFError: pass -- cgit v1.2.3 From cd07ddbfbb34a2482e90f3b95de97a1584ee8a9f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Sep 2013 17:42:31 +0100 Subject: Remove TODO --- tests/test_sftp.py | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 743a7831..58013cfd 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -812,7 +812,6 @@ class SFTPTest (unittest.TestCase): try: sftp.open(FOLDER + '/test2', 'w') self.fail('expected exception') - # TODO: extract failing command (the open) to share with a passing assertion except EOFError: pass -- cgit v1.2.3 From fb3c81bf423b673526768fe4b506e3d74d295ac5 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Thu, 18 Sep 2014 13:32:24 -0700 Subject: Clean up & tweak failing sftp closure test --- tests/test_sftp.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 58013cfd..a35914e1 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -203,7 +203,10 @@ class SFTPTest (unittest.TestCase): with sftp: pass try: - self._assert_opening_file_raises_error(sftp) + sftp.open(FOLDER + '/test2', 'w') + self.fail('expected exception') + except EOFError, socket.error: + pass finally: sftp = paramiko.SFTP.from_transport(tc) @@ -808,14 +811,6 @@ class SFTPTest (unittest.TestCase): sftp.remove('%s/nonutf8data' % FOLDER) - def _assert_opening_file_raises_error(self, sftp): - try: - sftp.open(FOLDER + '/test2', 'w') - self.fail('expected exception') - except EOFError: - pass - - if __name__ == '__main__': SFTPTest.init_loopback() # logging is required by test_N_file_with_percent -- cgit v1.2.3 From e27964254fbf5a55d5cc0c4db9268cc001827af1 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Thu, 18 Sep 2014 16:53:48 -0700 Subject: A herp and a derp (and an import shuffle) --- tests/test_sftp.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index a35914e1..72c7ba03 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -23,12 +23,13 @@ a real actual sftp server is contacted, and a new folder is created there to do test file operations in (so no existing files will be harmed). """ -from binascii import hexlify import os +import socket import sys -import warnings import threading import unittest +import warnings +from binascii import hexlify from tempfile import mkstemp import paramiko @@ -205,7 +206,7 @@ class SFTPTest (unittest.TestCase): try: sftp.open(FOLDER + '/test2', 'w') self.fail('expected exception') - except EOFError, socket.error: + except (EOFError, socket.error): pass finally: sftp = paramiko.SFTP.from_transport(tc) -- cgit v1.2.3