Skip to content

Commit

Permalink
more checkpoint
Browse files Browse the repository at this point in the history
More checkpoint
  • Loading branch information
unkcpz committed Nov 21, 2024
1 parent c626f75 commit 77e0074
Showing 1 changed file with 44 additions and 66 deletions.
110 changes: 44 additions & 66 deletions tests/transports/test_all_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -305,92 +304,71 @@ 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)
# 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)
#
# transport.chdir(directory)
(local_dir / directory).mkdir()
transport.mkdir(str(remote_dir / directory))

transport.mkdir(directory)
transport.chdir(directory)
local_file_name = 'file.txt'
retrieved_file_name = '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')

# here use full path in src and dst
local_file_abs_path = str(local_dir / directory / local_file_name)
retrieved_file_abs_path = str(local_dir / directory / retrieved_file_name)
remote_file_abs_path = str(remote_dir / directory / remote_file_name)

text = 'Viva Verdi\n'
with open(local_file_name, 'w', encoding='utf8') as fhandle:
with open(local_file_abs_path, 'w', encoding='utf8') as fhandle:
fhandle.write(text)

# here use full path in src and dst
transport.put(local_file_name, remote_file_name)
transport.get(remote_file_name, retrieved_file_name)
transport.putfile(local_file_name, remote_file_name)
transport.getfile(remote_file_name, retrieved_file_name)
transport.put(local_file_abs_path, remote_file_abs_path)
transport.get(remote_file_abs_path, retrieved_file_abs_path)
# transport.putfile(local_file_abs_path, remote_file_abs_path)
# 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 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"""
Expand Down

0 comments on commit 77e0074

Please sign in to comment.