Skip to content

Commit

Permalink
Fix bug with pathlib update to static_finders
Browse files Browse the repository at this point in the history
  • Loading branch information
shawncrawley committed Oct 7, 2024
1 parent 4c9697b commit ae1d688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
36 changes: 20 additions & 16 deletions tests/unit_tests/test_tethys_apps/test_static_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,48 @@ def test_init(self):
def test_find(self):
tethys_static_finder = TethysStaticFinder()
path = Path("test_app") / "css" / "main.css"
ret = tethys_static_finder.find(path)
self.assertEqual(str(self.root / "css" / "main.css").lower(), ret.lower())
path_ret = tethys_static_finder.find(path)
self.assertEqual(self.root / "css" / "main.css", path_ret)
str_ret = tethys_static_finder.find(str(path))
self.assertEqual(self.root / "css" / "main.css", str_ret)

def test_find_all(self):
tethys_static_finder = TethysStaticFinder()
path = Path("test_app") / "css" / "main.css"
ret = tethys_static_finder.find(path, all=True)
self.assertIn(
str(self.root / "css" / "main.css").lower(),
list(map(lambda x: x.lower(), ret)),
)
path_ret = tethys_static_finder.find(path, all=True)
self.assertIn(self.root / "css" / "main.css", path_ret)
str_ret = tethys_static_finder.find(str(path), all=True)
self.assertIn(self.root / "css" / "main.css", str_ret)

def test_find_location_with_no_prefix(self):
prefix = None
path = Path("css") / "main.css"

tethys_static_finder = TethysStaticFinder()
ret = tethys_static_finder.find_location(str(self.root), path, prefix)

self.assertEqual(str(self.root / path), ret)
path_ret = tethys_static_finder.find_location(self.root, path, prefix)
self.assertEqual(self.root / path, path_ret)
str_ret = tethys_static_finder.find_location(str(self.root), path, prefix)
self.assertEqual(self.root / path, str_ret)

def test_find_location_with_prefix_not_in_path(self):
prefix = "tethys_app"
path = Path("css") / "main.css"

tethys_static_finder = TethysStaticFinder()
ret = tethys_static_finder.find_location(str(self.root), path, prefix)

self.assertIsNone(ret)
path_ret = tethys_static_finder.find_location(self.root, path, prefix)
self.assertIsNone(path_ret)
str_ret = tethys_static_finder.find_location(str(self.root), path, prefix)
self.assertIsNone(str_ret)

def test_find_location_with_prefix_in_path(self):
prefix = "tethys_app"
path = Path("tethys_app") / "css" / "main.css"

tethys_static_finder = TethysStaticFinder()
ret = tethys_static_finder.find_location(str(self.root), path, prefix)

self.assertEqual(str(self.root / "css" / "main.css"), ret)
path_ret = tethys_static_finder.find_location(self.root, path, prefix)
self.assertEqual(self.root / "css" / "main.css", path_ret)
str_ret = tethys_static_finder.find_location(str(self.root), path, prefix)
self.assertEqual(self.root / "css" / "main.css", str_ret)

def test_list(self):
tethys_static_finder = TethysStaticFinder()
Expand Down
9 changes: 5 additions & 4 deletions tethys_apps/static_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TethysStaticFinder(BaseFinder):
This finder search for static files in a directory called 'public' or 'static'.
"""

def __init__(self, apps=None, *args, **kwargs):
def __init__(self, *args, **kwargs):
# List of locations with static files
self.locations = get_directories_in_tethys(
("static", "public"), with_app_name=True
Expand Down Expand Up @@ -57,13 +57,14 @@ def find_location(self, root, path, prefix=None):
Finds a requested static file in a location, returning the found
absolute path (or ``None`` if no match).
"""
path = Path(path)
if prefix:
prefix = Path(f"{prefix}/")
if not Path(path).is_relative_to(prefix):
if not path.is_relative_to(prefix):
return None
path = path.relative_to(prefix)
path = safe_join(str(root), str(path))
if Path(path).exists():
path = Path(safe_join(str(root), str(path)))
if path.exists():
return path

def list(self, ignore_patterns):
Expand Down

0 comments on commit ae1d688

Please sign in to comment.