From 602250fdf9515a8127cd567d79afa8134d4cf923 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Jan 2013 23:39:48 +0000 Subject: Turn SFTPFile into a context manager --- tests/test_sftp.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 2eadabcd..39c8aa83 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -188,6 +188,17 @@ class SFTPTest (unittest.TestCase): finally: sftp.remove(FOLDER + '/duck.txt') + def test_3_sftp_file_can_be_used_as_context_manager(self): + """ + verify that an opened file can be used as a context manager + """ + try: + with sftp.open(FOLDER + '/duck.txt', 'w') as f: + f.write(ARTICLE) + self.assertEqual(sftp.stat(FOLDER + '/duck.txt').st_size, 1483) + finally: + sftp.remove(FOLDER + '/duck.txt') + def test_4_append(self): """ verify that a file can be opened for append, and tell() still works. -- cgit v1.2.3 From 0b6aebb8a95d44102968a3e0caf94cd234369e5d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 5 Jan 2013 00:05:58 +0000 Subject: Verify Python version >= 2.6 before running context manager test --- tests/test_sftp.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 39c8aa83..7ce3e68c 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -192,6 +192,10 @@ class SFTPTest (unittest.TestCase): """ verify that an opened file can be used as a context manager """ + major, minor, micro, releaselevel, serial = sys.version_info + if (major, minor) <= (2, 5): + return + try: with sftp.open(FOLDER + '/duck.txt', 'w') as f: f.write(ARTICLE) -- cgit v1.2.3 From 08109136b4217b3fc620436819f4c92434189955 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 5 Jan 2013 00:15:26 +0000 Subject: Replace useless version check with import from __future__ --- tests/test_sftp.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index 7ce3e68c..f95da69c 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -23,6 +23,8 @@ 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 __future__ import with_statement + from binascii import hexlify import logging import os @@ -192,10 +194,6 @@ class SFTPTest (unittest.TestCase): """ verify that an opened file can be used as a context manager """ - major, minor, micro, releaselevel, serial = sys.version_info - if (major, minor) <= (2, 5): - return - try: with sftp.open(FOLDER + '/duck.txt', 'w') as f: f.write(ARTICLE) -- cgit v1.2.3