infra: Fix test filesystem paths incompatible with windows (#14388)

- **Description:** This PR fixes test failures on Windows caused by path
handling differences and unescaped special characters in regex. The
failing tests are:
```
FAILED tests/unit_tests/storage/test_filesystem.py::test_yield_keys - AssertionError: assert ['key1', 'subdir\\key2'] == ['key1', 'subdir/key2']
FAILED tests/unit_tests/test_imports.py::test_importable_all - ModuleNotFoundError: No module named 'langchain_community.langchain_community\\adapters'
FAILED tests/unit_tests/tools/file_management/test_utils.py::test_get_validated_relative_path_errs_on_absolute - re.error: incomplete escape \U at position 53
FAILED tests/unit_tests/tools/file_management/test_utils.py::test_get_validated_relative_path_errs_on_parent_dir - re.error: incomplete escape \U at position 69
FAILED tests/unit_tests/tools/file_management/test_utils.py::test_get_validated_relative_path_errs_for_symlink_outside_root - re.error: incomplete escape \U at position 64
```

- **Issue:** fixes
https://github.com/langchain-ai/langchain/issues/11775 (partially)
- **Dependencies:** none
This commit is contained in:
Ran 2023-12-21 23:45:42 +02:00 committed by GitHub
parent 71076cceaf
commit 129a929d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 12 deletions

View File

@ -1,10 +1,11 @@
import glob import glob
import importlib import importlib
from pathlib import Path
def test_importable_all() -> None: def test_importable_all() -> None:
for path in glob.glob("../community/langchain_community/*"): for path in glob.glob("../community/langchain_community/*"):
relative_path = path.split("/")[-1] relative_path = Path(path).parts[-1]
if relative_path.endswith(".typed"): if relative_path.endswith(".typed"):
continue continue
module_name = relative_path.split(".")[0] module_name = relative_path.split(".")[0]

View File

@ -1,6 +1,6 @@
"""Test the File Management utils.""" """Test the File Management utils."""
import re
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
@ -16,8 +16,8 @@ def test_get_validated_relative_path_errs_on_absolute() -> None:
"""Safely resolve a path.""" """Safely resolve a path."""
root = Path(__file__).parent root = Path(__file__).parent
user_path = "/bin/bash" user_path = "/bin/bash"
matches = f"Path {user_path} is outside of the allowed directory {root}" match = re.escape(f"Path {user_path} is outside of the allowed directory {root}")
with pytest.raises(FileValidationError, match=matches): with pytest.raises(FileValidationError, match=match):
get_validated_relative_path(root, user_path) get_validated_relative_path(root, user_path)
@ -25,8 +25,8 @@ def test_get_validated_relative_path_errs_on_parent_dir() -> None:
"""Safely resolve a path.""" """Safely resolve a path."""
root = Path(__file__).parent root = Path(__file__).parent
user_path = "data/sub/../../../sibling" user_path = "data/sub/../../../sibling"
matches = f"Path {user_path} is outside of the allowed directory {root}" match = re.escape(f"Path {user_path} is outside of the allowed directory {root}")
with pytest.raises(FileValidationError, match=matches): with pytest.raises(FileValidationError, match=match):
get_validated_relative_path(root, user_path) get_validated_relative_path(root, user_path)
@ -49,10 +49,10 @@ def test_get_validated_relative_path_errs_for_symlink_outside_root() -> None:
symlink_path = root / user_path symlink_path = root / user_path
symlink_path.symlink_to(outside_path) symlink_path.symlink_to(outside_path)
matches = ( match = re.escape(
f"Path {user_path} is outside of the allowed directory {root.resolve()}" f"Path {user_path} is outside of the allowed directory {root.resolve()}"
) )
with pytest.raises(FileValidationError, match=matches): with pytest.raises(FileValidationError, match=match):
get_validated_relative_path(root, user_path) get_validated_relative_path(root, user_path)
symlink_path.unlink() symlink_path.unlink()

View File

@ -1,10 +1,11 @@
import glob import glob
import importlib import importlib
from pathlib import Path
def test_importable_all() -> None: def test_importable_all() -> None:
for path in glob.glob("../core/langchain_core/*"): for path in glob.glob("../core/langchain_core/*"):
relative_path = path.split("/")[-1] relative_path = Path(path).parts[-1]
if relative_path.endswith(".typed"): if relative_path.endswith(".typed"):
continue continue
module_name = relative_path.split(".")[0] module_name = relative_path.split(".")[0]

View File

@ -1,10 +1,11 @@
import glob import glob
import importlib import importlib
from pathlib import Path
def test_importable_all() -> None: def test_importable_all() -> None:
for path in glob.glob("../experimental/langchain_experimental/*"): for path in glob.glob("../experimental/langchain_experimental/*"):
relative_path = path.split("/")[-1] relative_path = Path(path).parts[-1]
if relative_path.endswith(".typed"): if relative_path.endswith(".typed"):
continue continue
module_name = relative_path.split(".")[0] module_name = relative_path.split(".")[0]

View File

@ -1,3 +1,4 @@
import os
import tempfile import tempfile
from typing import Generator from typing import Generator
@ -74,5 +75,5 @@ def test_yield_keys(file_store: LocalFileStore) -> None:
keys = list(file_store.yield_keys()) keys = list(file_store.yield_keys())
# Assert that the yielded keys match the expected keys # Assert that the yielded keys match the expected keys
expected_keys = ["key1", "subdir/key2"] expected_keys = ["key1", os.path.join("subdir", "key2")]
assert keys == expected_keys assert keys == expected_keys

View File

@ -1,10 +1,11 @@
import glob import glob
import importlib import importlib
from pathlib import Path
def test_importable_all() -> None: def test_importable_all() -> None:
for path in glob.glob("../langchain/langchain/*"): for path in glob.glob("../langchain/langchain/*"):
relative_path = path.split("/")[-1] relative_path = Path(path).parts[-1]
if relative_path.endswith(".typed"): if relative_path.endswith(".typed"):
continue continue
module_name = relative_path.split(".")[0] module_name = relative_path.split(".")[0]