mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
4adfd790f0
- Permit the specification of a `root_dir` to the read/write file tools to specify a working directory - Add validation for attempts to read/write outside the directory (e.g., through `../../` or symlinks or `/abs/path`'s that don't lie in the correct path) - Add some tests for all One question is whether we should make a default root directory for these? tradeoffs either way
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
"""Test the ReadFile tool."""
|
|
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from langchain.tools.file_management.read import ReadFileTool
|
|
|
|
|
|
def test_read_file_with_root_dir() -> None:
|
|
"""Test the ReadFile tool."""
|
|
with TemporaryDirectory() as temp_dir:
|
|
with (Path(temp_dir) / "file.txt").open("w") as f:
|
|
f.write("Hello, world!")
|
|
tool = ReadFileTool(root_dir=temp_dir)
|
|
result = tool.run("file.txt")
|
|
assert result == "Hello, world!"
|
|
# Check absolute files can still be passed if they lie within the root dir.
|
|
result = tool.run(str(Path(temp_dir) / "file.txt"))
|
|
assert result == "Hello, world!"
|
|
|
|
|
|
def test_read_file() -> None:
|
|
"""Test the ReadFile tool."""
|
|
with TemporaryDirectory() as temp_dir:
|
|
with (Path(temp_dir) / "file.txt").open("w") as f:
|
|
f.write("Hello, world!")
|
|
tool = ReadFileTool()
|
|
result = tool.run(str(Path(temp_dir) / "file.txt"))
|
|
assert result == "Hello, world!"
|