From da0e6bffb09c5556bfabb5b711d1e81213cd8f2f Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Mon, 23 Oct 2017 16:37:39 -0700 Subject: Replace rest of unittest-style self-asserts in sftp tests --- tests/test_sftp.py | 72 ++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 46 deletions(-) (limited to 'tests/test_sftp.py') diff --git a/tests/test_sftp.py b/tests/test_sftp.py index e6e4c90a..aa2c237d 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -165,15 +165,14 @@ class TestSFTP(object): with sftp.open(sftp.FOLDER + '/first.txt', 'w') as f: f.write('content!\n') sftp.rename(sftp.FOLDER + '/first.txt', sftp.FOLDER + '/second.txt') - try: + with pytest.raises(IOError, match='No such file'): sftp.open(sftp.FOLDER + '/first.txt', 'r') - self.assertTrue(False, 'no exception on reading nonexistent file') - except IOError: - pass with sftp.open(sftp.FOLDER + '/second.txt', 'r') as f: f.seek(-6, f.SEEK_END) assert u(f.read(4)) == 'tent' finally: + # TODO: this is gross, make some sort of 'remove if possible' / 'rm + # -f' a-like, jeez try: sftp.remove(sftp.FOLDER + '/first.txt') except: @@ -193,17 +192,15 @@ class TestSFTP(object): sftp.rename(sftp.FOLDER + '/a', sftp.FOLDER + '/b') with sftp.open(sftp.FOLDER + '/a', 'w') as f: f.write('two') - try: + with pytest.raises(IOError): # actual message seems generic sftp.rename(sftp.FOLDER + '/a', sftp.FOLDER + '/b') - self.assertTrue(False, 'no exception when rename-ing onto existing file') - except (OSError, IOError): - pass # now check with the posix_rename sftp.posix_rename(sftp.FOLDER + '/a', sftp.FOLDER + '/b') with sftp.open(sftp.FOLDER + '/b', 'r') as f: data = u(f.read()) - assert 'two' == data, "Contents of renamed file not the same as original file" + err = "Contents of renamed file not the same as original file" + assert 'two' == data, err finally: try: @@ -225,12 +222,9 @@ class TestSFTP(object): sftp.open(sftp.FOLDER + '/subfolder/test', 'w').close() sftp.remove(sftp.FOLDER + '/subfolder/test') sftp.rmdir(sftp.FOLDER + '/subfolder') - try: + # shouldn't be able to create that file if dir removed + with pytest.raises(IOError, match="No such file"): sftp.open(sftp.FOLDER + '/subfolder/test') - # shouldn't be able to create that file - self.assertTrue(False, 'no exception at dummy file creation') - except IOError: - pass def test_7_listdir(self, sftp): """ @@ -244,10 +238,10 @@ class TestSFTP(object): x = sftp.listdir(sftp.FOLDER) assert len(x) == 3 - self.assertTrue('duck.txt' in x) - self.assertTrue('fish.txt' in x) - self.assertTrue('tertiary.py' in x) - self.assertTrue('random' not in x) + assert 'duck.txt' in x + assert 'fish.txt' in x + assert 'tertiary.py' in x + assert 'random' not in x finally: sftp.remove(sftp.FOLDER + '/duck.txt') sftp.remove(sftp.FOLDER + '/fish.txt') @@ -264,10 +258,10 @@ class TestSFTP(object): x = [x.filename for x in sftp.listdir_iter(sftp.FOLDER)] assert len(x) == 3 - self.assertTrue('duck.txt' in x) - self.assertTrue('fish.txt' in x) - self.assertTrue('tertiary.py' in x) - self.assertTrue('random' not in x) + assert 'duck.txt' in x + assert 'fish.txt' in x + assert 'tertiary.py' in x + assert 'random' not in x finally: sftp.remove(sftp.FOLDER + '/duck.txt') sftp.remove(sftp.FOLDER + '/fish.txt') @@ -369,7 +363,7 @@ class TestSFTP(object): line_number += 1 pos_list.append(loc) loc = f.tell() - self.assertTrue(f.seekable()) + assert f.seekable() f.seek(pos_list[6], f.SEEK_SET) assert f.readline(), 'Nouzilly == France.\n' f.seek(pos_list[17], f.SEEK_SET) @@ -425,7 +419,7 @@ class TestSFTP(object): assert sftp.stat(sftp.FOLDER + '/link.txt').st_size == 9 # the sftp server may be hiding extra path members from us, so the # length may be longer than we expect: - self.assertTrue(sftp.lstat(sftp.FOLDER + '/link2.txt').st_size >= len(abs_path)) + assert sftp.lstat(sftp.FOLDER + '/link2.txt').st_size >= len(abs_path) assert sftp.stat(sftp.FOLDER + '/link2.txt').st_size == 9 assert sftp.stat(sftp.FOLDER + '/original.txt').st_size == 9 finally: @@ -468,33 +462,21 @@ class TestSFTP(object): error. """ pwd = sftp.normalize('.') - self.assertTrue(len(pwd) > 0) + assert len(pwd) > 0 f = sftp.normalize('./' + sftp.FOLDER) - self.assertTrue(len(f) > 0) + assert len(f) > 0 assert os.path.join(pwd, sftp.FOLDER) == f def test_F_mkdir(self, sftp): """ verify that mkdir/rmdir work. """ - try: - sftp.mkdir(sftp.FOLDER + '/subfolder') - except: - self.assertTrue(False, 'exception creating subfolder') - try: + sftp.mkdir(sftp.FOLDER + '/subfolder') + with pytest.raises(IOError): # generic msg only sftp.mkdir(sftp.FOLDER + '/subfolder') - self.assertTrue(False, 'no exception overwriting subfolder') - except IOError: - pass - try: - sftp.rmdir(sftp.FOLDER + '/subfolder') - except: - self.assertTrue(False, 'exception removing subfolder') - try: + sftp.rmdir(sftp.FOLDER + '/subfolder') + with pytest.raises(IOError, match="No such file"): sftp.rmdir(sftp.FOLDER + '/subfolder') - self.assertTrue(False, 'no exception removing nonexistent subfolder') - except IOError: - pass def test_G_chdir(self, sftp): """ @@ -582,8 +564,7 @@ class TestSFTP(object): sum = f.check('md5', 0, 512) assert '93DE4788FCA28D471516963A1FE3856A' == u(hexlify(sum)).upper() sum = f.check('md5', 0, 0, 510) - self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6', - u(hexlify(sum)).upper()) + assert u(hexlify(sum)).upper() == 'EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6' # noqa finally: sftp.unlink(sftp.FOLDER + '/kitty.txt') @@ -717,11 +698,10 @@ class TestSFTP(object): try: attrs = sftp.putfo(stream, target) # the returned attributes should not be null - self.assertNotEqual(attrs, None) + assert attrs is not None finally: sftp.remove(target) - def test_N_file_with_percent(self, sftp): """ verify that we can create a file with a '%' in the filename. -- cgit v1.2.3