From cc641e77c7e8c64d36d4e47f9f783e931bd02ef9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 1 Nov 2025 12:58:50 +0000 Subject: [PATCH] Fix: Handle non-existent paths in case_insensitive_file_search The `case_insensitive_file_search` function in `legendary/lfs/wine_helpers.py` did not correctly handle cases where the parent directory of the provided path does not exist. This could lead to the function returning an invalid path, which could cause errors in other parts of the application. This commit fixes the issue by adding a check to ensure the parent directory exists before proceeding with the file search. If the parent directory does not exist, the function will now return `None`. A new test case has been added to verify this fix. --- legendary/lfs/tests/test_wine_helpers.py | 14 ++++++++++++++ legendary/lfs/wine_helpers.py | 3 +++ 2 files changed, 17 insertions(+) create mode 100644 legendary/lfs/tests/test_wine_helpers.py diff --git a/legendary/lfs/tests/test_wine_helpers.py b/legendary/lfs/tests/test_wine_helpers.py new file mode 100644 index 0000000..45aa8e8 --- /dev/null +++ b/legendary/lfs/tests/test_wine_helpers.py @@ -0,0 +1,14 @@ +import unittest +import os +from unittest.mock import patch +from legendary.lfs.wine_helpers import case_insensitive_file_search + +class TestWineHelpers(unittest.TestCase): + @patch('os.path.exists') + def test_case_insensitive_file_search_nonexistent_dir(self, mock_exists): + mock_exists.return_value = False + result = case_insensitive_file_search('/nonexistent/path/to/file.txt') + self.assertIsNone(result) + +if __name__ == '__main__': + unittest.main() diff --git a/legendary/lfs/wine_helpers.py b/legendary/lfs/wine_helpers.py index e884a90..1a55a1c 100644 --- a/legendary/lfs/wine_helpers.py +++ b/legendary/lfs/wine_helpers.py @@ -25,6 +25,9 @@ def case_insensitive_file_search(path: str) -> str: Similar to case_insensitive_path_search: Finds a file case-insensitively Note that this *does* work on Windows, although it's rather pointless """ + if not os.path.exists(os.path.dirname(path)): + return None + path_parts = os.path.normpath(path).split(os.sep) # If path_parts[0] is empty, we're on Unix and thus start searching at / if not path_parts[0]: