Skip to content

Commit

Permalink
Handle (but warn about) missing directories
Browse files Browse the repository at this point in the history
The previous version of WhiteNoise used `os.walk` which doesn't throw an
error for missing directories, but we now use `os.scandir` which does.
This patch restores the graceful handling of missing directories, but
issues a warning about them.

Closes #166
  • Loading branch information
evansd committed Jul 12, 2018
1 parent 9e02fa5 commit 2a883f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions tests/test_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except ImportError:
from urlparse import urljoin
import shutil
import warnings
from wsgiref.simple_server import demo_app

from .utils import TestServer, Files
Expand Down Expand Up @@ -201,6 +202,11 @@ def test_out_of_range_error(self):
response.headers['Content-Range'],
'bytes */%s' % len(self.files.js_content))

def test_warn_about_missing_directories(self):
with warnings.catch_warnings(record=True) as warning_list:
self.application.add_files(u'/dev/null/nosuchdir\u2713')
self.assertEqual(len(warning_list), 1)

def assert_is_default_response(self, response):
self.assertIn('Hello world!', response.text)

Expand Down
6 changes: 5 additions & 1 deletion whitenoise/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from posixpath import normpath
import re
import warnings
from wsgiref.headers import Headers
from wsgiref.util import FileWrapper

Expand Down Expand Up @@ -91,12 +92,15 @@ def add_files(self, root, prefix=None):
root = root.rstrip(os.path.sep) + os.path.sep
prefix = decode_if_byte_string(prefix)
prefix = ensure_leading_trailing_slash(prefix)
is_directory = os.path.isdir(root)
if not is_directory:
warnings.warn(u'No directory at: {}'.format(root))
if self.autorefresh:
# Later calls to `add_files` overwrite earlier ones, hence we need
# to store the list of directories in reverse order so later ones
# match first when they're checked in "autorefresh" mode
self.directories.insert(0, (root, prefix))
else:
elif is_directory:
self.update_files_dictionary(root, prefix)

def update_files_dictionary(self, root, prefix):
Expand Down

0 comments on commit 2a883f5

Please sign in to comment.