forked from Archives/langchain
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
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
"""Test the WriteFile tool."""
|
|
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
import pytest
|
|
|
|
from langchain.tools.file_management.write import WriteFileTool
|
|
|
|
|
|
def test_write_file_with_root_dir() -> None:
|
|
"""Test the WriteFile tool when a root dir is specified."""
|
|
with TemporaryDirectory() as temp_dir:
|
|
tool = WriteFileTool(root_dir=temp_dir)
|
|
tool.run({"file_path": "file.txt", "text": "Hello, world!"})
|
|
assert (Path(temp_dir) / "file.txt").exists()
|
|
assert (Path(temp_dir) / "file.txt").read_text() == "Hello, world!"
|
|
|
|
|
|
def test_write_file_errs_outside_root_dir() -> None:
|
|
"""Test the WriteFile tool when a root dir is specified."""
|
|
with TemporaryDirectory() as temp_dir:
|
|
tool = WriteFileTool(root_dir=temp_dir)
|
|
with pytest.raises(ValueError):
|
|
tool.run({"file_path": "../file.txt", "text": "Hello, world!"})
|
|
|
|
|
|
def test_write_file() -> None:
|
|
"""Test the WriteFile tool."""
|
|
with TemporaryDirectory() as temp_dir:
|
|
file_path = str(Path(temp_dir) / "file.txt")
|
|
tool = WriteFileTool()
|
|
tool.run({"file_path": file_path, "text": "Hello, world!"})
|
|
assert (Path(temp_dir) / "file.txt").exists()
|
|
assert (Path(temp_dir) / "file.txt").read_text() == "Hello, world!"
|