Skip to content

Commit

Permalink
Fix FileSystemRegexFinder to correctly find files in subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
anttihirvonen committed Nov 15, 2013
1 parent 107b4c6 commit 02eb1da
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
14 changes: 9 additions & 5 deletions jstemplate/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ def find(self, name):
return matches.items()

def _update_matches(self, matches, directory, pattern):
for filename in os.listdir(directory):
if not os.path.isdir(filename):
match = pattern.match(filename)
# Walk recursively under directory and search for matches
for root, dirs, files in os.walk(directory):
# Build relative path to current directory, omit .
relative_root = os.path.relpath(root, directory).replace('.', '', 1)
for filename in files:
# Build relative path to file
relative_filepath = os.path.join(relative_root, filename)
match = pattern.match(relative_filepath)
if match is not None and match.group(1) not in matches:
matches[match.group(1)] = os.path.join(directory, filename)

matches[match.group(1)] = os.path.join(directory, relative_filepath)


def _get_app_template_dirs():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Mustache's template full of {{ foo }} and \.</p>
1 change: 1 addition & 0 deletions jstemplate/tests/templates/subdirectory/testtemplate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Mustache's template full of {{ foo }} and \.</p>
8 changes: 8 additions & 0 deletions jstemplate/tests/test_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def test_find_many_files(self):
)


@override_settings(JSTEMPLATE_DIRS=[os.path.join(here, "templates")])
def test_find_files_in_subdirectories(self):
self.assertEqual(
list(self.finder.find('(subdirectory.*)')),
[('subdirectory/subdirectory2/testtemplate', os.path.join(here, 'templates', 'subdirectory', 'subdirectory2', 'testtemplate.html')),
('subdirectory/testtemplate', os.path.join(here, 'templates', 'subdirectory', 'testtemplate.html'))])


class AppFinderTest(TestCase):
@property
def finder(self):
Expand Down

0 comments on commit 02eb1da

Please sign in to comment.