Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sftp] Catch FileNotFoundError rather than OSError in .exists() #1087

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def exists(self, name):
try:
self.sftp.stat(self._remote_path(name))
return True
except OSError:
except FileNotFoundError:
return False

def _isdir_attr(self, item):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_sftp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import socket
import stat
from datetime import datetime
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -56,7 +57,7 @@ def test_mkdir(self, mock_sftp):
self.assertEqual(mock_sftp.mkdir.call_args[0], ('foo',))

@patch('storages.backends.sftpstorage.SFTPStorage.sftp', **{
'stat.side_effect': (IOError(), True)
'stat.side_effect': (FileNotFoundError(), True)
})
def test_mkdir_parent(self, mock_sftp):
self.storage._mkdir('bar/foo')
Expand All @@ -69,7 +70,7 @@ def test_save(self, mock_sftp):
self.assertTrue(mock_sftp.open.return_value.write.called)

@patch('storages.backends.sftpstorage.SFTPStorage.sftp', **{
'stat.side_effect': (IOError(), True)
'stat.side_effect': (FileNotFoundError(), True)
})
def test_save_in_subdir(self, mock_sftp):
self.storage._save('bar/foo', File(io.BytesIO(b'foo'), 'foo'))
Expand All @@ -86,11 +87,18 @@ def test_exists(self, mock_sftp):
self.assertTrue(self.storage.exists('foo'))

@patch('storages.backends.sftpstorage.SFTPStorage.sftp', **{
'stat.side_effect': IOError()
'stat.side_effect': FileNotFoundError()
})
def test_not_exists(self, mock_sftp):
self.assertFalse(self.storage.exists('foo'))

@patch('storages.backends.sftpstorage.SFTPStorage.sftp', **{
'stat.side_effect': socket.timeout()
})
def test_not_exists_timeout(self, mock_sftp):
with self.assertRaises(socket.timeout):
self.storage.exists('foo')

@patch('storages.backends.sftpstorage.SFTPStorage.sftp', **{
'listdir_attr.return_value':
[MagicMock(filename='foo', st_mode=stat.S_IFDIR),
Expand Down