From aeb255fe9f7fc4efdaa58988dfd1d9ce40c120ad Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Thu, 21 Nov 2024 16:04:35 +0100 Subject: [PATCH] more checkpoint More checkpoint --- tests/transports/test_all_plugins.py | 92 +++++++++------------------- 1 file changed, 30 insertions(+), 62 deletions(-) diff --git a/tests/transports/test_all_plugins.py b/tests/transports/test_all_plugins.py index 3f73fbe16..ecb7b830c 100644 --- a/tests/transports/test_all_plugins.py +++ b/tests/transports/test_all_plugins.py @@ -77,6 +77,18 @@ def test_deprecate_chdir_and_getcwd(custom_transport, remote_tmp_path): assert location == transport.getcwd() +def test_chdir_to_empty_string(custom_transport, remote_tmp_path): + """I check that if I pass an empty string to chdir, the cwd does + not change (this is a paramiko default behavior), but getcwd() + is still correctly defined. + """ + with custom_transport as transport: + new_dir = str(remote_tmp_path) + transport.chdir(new_dir) + transport.chdir('') + assert new_dir == transport.getcwd() + + def test_makedirs(custom_transport, remote_tmp_path): """Verify the functioning of makedirs command""" with custom_transport as transport: @@ -223,21 +235,14 @@ def test_dir_copy(custom_transport, remote_tmp_path): transport.copy('', dst_dir) -def test_dir_permissions_creation_modification(custom_transport): +def test_dir_permissions_creation_modification(custom_transport, remote_tmp_path): """Verify if chmod raises OSError when trying to change bits on a non-existing folder """ with custom_transport as transport: - location = transport.normalize(os.path.join('/', 'tmp')) - directory = 'temp_dir_test' - transport.chdir(location) + directory = str(remote_tmp_path / 'test') - while transport.isdir(directory): - # I append a random letter/number until it is unique - directory += random.choice(string.ascii_uppercase + string.digits) - - # create directory with non default permissions - transport.mkdir(directory) + transport.makedirs(directory) # change permissions transport.chmod(directory, 0o777) @@ -274,18 +279,12 @@ def test_dir_permissions_creation_modification(custom_transport): transport.rmdir(directory) -def test_dir_reading_permissions(custom_transport): +def test_dir_reading_permissions(custom_transport, remote_tmp_path): """Try to enter a directory with no read permissions. Verify that the cwd has not changed after failed try. """ with custom_transport as transport: - location = transport.normalize(os.path.join('/', 'tmp')) - directory = 'temp_dir_test' - transport.chdir(location) - - while transport.isdir(directory): - # I append a random letter/number until it is unique - directory += random.choice(string.ascii_uppercase + string.digits) + directory = str(remote_tmp_path / 'test') # create directory with non default permissions transport.mkdir(directory) @@ -305,67 +304,43 @@ def test_dir_reading_permissions(custom_transport): assert old_cwd == new_cwd - # TODO : the test leaves a directory even if it is successful - # The bug is in paramiko. After lowering the permissions, - # I cannot restore them to higher values - # transport.rmdir(directory) - def test_isfile_isdir_to_empty_string(custom_transport): """I check that isdir or isfile return False when executed on an empty string """ with custom_transport as transport: - location = transport.normalize(os.path.join('/', 'tmp')) - transport.chdir(location) assert not transport.isdir('') assert not transport.isfile('') -def test_isfile_isdir_to_non_existing_string(custom_transport): +def test_isfile_isdir_to_non_existing_string(custom_transport, remote_tmp_path): """I check that isdir or isfile return False when executed on an empty string """ with custom_transport as transport: - location = transport.normalize(os.path.join('/', 'tmp')) - transport.chdir(location) - fake_folder = 'pippo' + fake_folder = str(remote_tmp_path / 'pippo') assert not transport.isfile(fake_folder) assert not transport.isdir(fake_folder) with pytest.raises(OSError): transport.chdir(fake_folder) -def test_chdir_to_empty_string(custom_transport): - """I check that if I pass an empty string to chdir, the cwd does - not change (this is a paramiko default behavior), but getcwd() - is still correctly defined. - """ - with custom_transport as transport: - new_dir = transport.normalize(os.path.join('/', 'tmp')) - transport.chdir(new_dir) - transport.chdir('') - assert new_dir == transport.getcwd() - - -def test_put_and_get_file(custom_transport): +def test_put_and_get_file(custom_transport, tmp_path_factory): """Test putting and getting files.""" - local_dir = os.path.join('/', 'tmp') - remote_dir = local_dir + local_dir = tmp_path_factory.mktemp('local') + remote_dir = tmp_path_factory.mktemp('remote') + directory = 'tmp_try' with custom_transport as transport: - transport.chdir(remote_dir) - while transport.isdir(directory): - # I append a random letter/number until it is unique - directory += random.choice(string.ascii_uppercase + string.digits) + (local_dir / directory).mkdir() + transport.mkdir(str(remote_dir / directory)) - transport.mkdir(directory) - transport.chdir(directory) + local_file_name = str(local_dir / directory / 'file.txt') + retrieved_file_name = str(local_dir / directory / 'file_retrieved.txt') - local_file_name = os.path.join(local_dir, directory, 'file.txt') - remote_file_name = 'file_remote.txt' - retrieved_file_name = os.path.join(local_dir, directory, 'file_retrieved.txt') + remote_file_name = str(remote_dir / directory / 'file_remote.txt') text = 'Viva Verdi\n' with open(local_file_name, 'w', encoding='utf8') as fhandle: @@ -377,20 +352,13 @@ def test_put_and_get_file(custom_transport): transport.putfile(local_file_name, remote_file_name) transport.getfile(remote_file_name, retrieved_file_name) - list_of_files = transport.listdir('.') + list_of_files = transport.listdir(str(remote_dir / directory)) # it is False because local_file_name has the full path, # while list_of_files has not assert local_file_name not in list_of_files - assert remote_file_name in list_of_files + assert remote_file_name in [str(remote_dir / directory / file) for file in list_of_files] assert retrieved_file_name not in list_of_files - os.remove(local_file_name) - transport.remove(remote_file_name) - os.remove(retrieved_file_name) - - transport.chdir('..') - transport.rmdir(directory) - def test_put_get_abs_path_file(custom_transport): """Test of exception for non existing files and abs path"""