Fix case where path exists but is not a dir

This commit is contained in:
Markus Unterwaditzer 2014-04-06 13:49:08 +02:00
parent 46910dbc4f
commit e9f1273fa8
2 changed files with 15 additions and 6 deletions

View file

@ -27,10 +27,17 @@ class TestFilesystemStorage(StorageTests):
os.makedirs(os.path.join(path, collection))
return {'path': path, 'fileext': '.txt', 'collection': collection}
def test_create_directory(self, tmpdir):
with pytest.raises(ValueError):
def test_create_is_false(self, tmpdir):
with pytest.raises(IOError):
self.storage_class(str(tmpdir), '.txt', collection='lol',
create=False)
def test_is_not_directory(self, tmpdir):
with pytest.raises(IOError):
f = tmpdir.join('hue')
f.write('stub')
self.storage_class(str(tmpdir), '.txt', collection='hue')
def test_create_is_true(self, tmpdir):
self.storage_class(str(tmpdir), '.txt', collection='asd')
assert tmpdir.listdir() == [tmpdir.join('asd')]

View file

@ -44,13 +44,15 @@ class FilesystemStorage(Storage):
if collection is not None:
path = os.path.join(path, collection)
if not os.path.isdir(path):
if os.path.exists(path):
raise IOError('{} is not a directory.')
if create:
os.makedirs(path, 0750)
else:
raise ValueError('Directory {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
raise IOError('Directory {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
self.collection = collection
self.path = expand_path(path)
self.encoding = encoding