Skip to content

Commit d14f719

Browse files
author
Kincaid Savoie
committed
ssh_client now throws an exception when given a directory and recurse is not set
1 parent a890f41 commit d14f719

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pssh/ssh_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def copy_file(self, local_file, remote_file, recurse=False):
309309
"""
310310
if os.path.isdir(local_file) and recurse:
311311
return self._copy_dir(local_file, remote_file)
312+
elif os.path.isdir(local_file) and not recurse:
313+
raise ValueError("Recurse must be true if local_file is a "
314+
"directory.")
312315
sftp = self._make_sftp()
313316
destination = [_dir for _dir in remote_file.split(os.path.sep)
314317
if _dir][:-1][0]

tests/test_ssh_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ def test_ssh_client_directory(self):
165165
shutil.rmtree(local_test_path)
166166
shutil.rmtree(remote_test_path)
167167

168+
def test_ssh_client_directory_no_recurse(self):
169+
"""Tests copying directories with SSH client. Copy all the files from
170+
local directory to server, then make sure they are all present."""
171+
test_file_data = 'test'
172+
local_test_path = 'directory_test'
173+
remote_test_path = 'directory_test_copied'
174+
os.mkdir(local_test_path)
175+
remote_file_paths = []
176+
for i in range(0, 10):
177+
local_file_path = os.path.join(local_test_path, 'foo' + str(i))
178+
remote_file_path = os.path.join(remote_test_path, 'foo' + str(i))
179+
remote_file_paths.append(remote_file_path)
180+
test_file = open(local_file_path, 'w')
181+
test_file.write(test_file_data)
182+
test_file.close()
183+
client = SSHClient(self.host, port=self.listen_port,
184+
pkey=self.user_key)
185+
self.assertRaises(ValueError, client.copy_file, local_test_path, remote_test_path)
186+
shutil.rmtree(local_test_path)
187+
168188
def test_ssh_agent_authentication(self):
169189
"""Test authentication via SSH agent.
170190
Do not provide public key to use when creating SSHClient,

0 commit comments

Comments
 (0)