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

Add bugfix with tests for find_resource #189

Merged
merged 5 commits into from
Mar 4, 2024
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
8 changes: 7 additions & 1 deletion ovos_workshop/resource_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,16 @@ def find_resource(res_name: str, root_dir: str, res_dirname: str,
Path: The full path to the resource file or None if not found
"""
if lang:
for directory in locate_lang_directories(lang, root_dir, res_dirname):
for directory in locate_lang_directories(lang, root_dir, '.'):
# Iterate over nodes in the language directory
for x in directory.iterdir():
if x.is_file() and res_name == x.name:
return x
elif x.is_dir() and x.name == res_dirname:
for y in x.iterdir():
# Iterate over resource subdirectories within a lang dir
if y.is_file() and res_name == y.name:
return y

for directory in locate_base_directories(root_dir, res_dirname):
for d, _, file_names in walk(directory):
Expand Down
Empty file.
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions test/unittests/test_resource_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from os import environ
from os.path import isdir, join, dirname
from pathlib import Path


class TestResourceFiles(unittest.TestCase):
Expand All @@ -20,6 +21,30 @@ def test_resolve_resource_file(self):

def test_find_resource(self):
from ovos_workshop.resource_files import find_resource
test_dir = join(dirname(__file__), "test_res")

# Test valid nested request
valid_dialog = find_resource("test.dialog", test_dir, "dialog", "en-us")
self.assertEqual(valid_dialog, Path(test_dir, "en-us", "dialog",
"test.dialog"))

# Test valid top-level lang resource
valid_vocab = find_resource("test.voc", test_dir, "vocab", "en-us")
self.assertEqual(valid_vocab, Path(test_dir, "en-us", "test.voc"))

# Test lang-agnostic resource
valid_ui = find_resource("test.qml", test_dir, "ui")
self.assertEqual(valid_ui, Path(test_dir, "ui", "test.qml"))

# Test valid in other locale
valid_dialog = find_resource("test.dialog", test_dir, "dialog", "en-gb")
self.assertEqual(valid_dialog, Path(test_dir, "en-us", "dialog",
"test.dialog"))

# Test invalid resource
invalid_resource = find_resource("test.dialog", test_dir, "vocab",
"de-de")
self.assertIsNone(invalid_resource)


class TestResourceType(unittest.TestCase):
Expand Down
Loading