"""Unit tests for file system tools and validate_path.""" import os from pathlib import Path import tools # ── Helpers ────────────────────────────────────────────────────────────────── def _set_workdir(tmp_path: Path): """Set tools.WORKDIR to the given tmp_path for testing.""" tools.set_workdir(tmp_path.resolve()) # ── 6.1 validate_path ────────────────────────────────────────────────────── class TestValidatePath: def test_valid_relative_path(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "hello.txt").write_text("hi") result = tools.validate_path("hello.txt") assert result == str(tmp_path.resolve() / "hello.txt") def test_valid_nested_path(self, tmp_path): _set_workdir(tmp_path) sub = tmp_path / "a" / "b" sub.mkdir(parents=True) (sub / "file.txt").write_text("data") result = tools.validate_path("a/b/file.txt") assert result == str(sub.resolve() / "file.txt") def test_dot_dot_traversal_rejected(self, tmp_path): _set_workdir(tmp_path) import pytest with pytest.raises(ValueError, match="Access denied"): tools.validate_path("../outside.txt") def test_absolute_path_outside_workdir(self, tmp_path): _set_workdir(tmp_path) import pytest with pytest.raises(ValueError, match="Access denied"): tools.validate_path("/etc/passwd") def test_symlink_escaping_workdir(self, tmp_path): _set_workdir(tmp_path) # Create a directory outside workdir outside = tmp_path.parent / "outside_dir" outside.mkdir(exist_ok=True) (outside / "secret.txt").write_text("secret") # Create symlink inside workdir pointing outside link = tmp_path / "escape_link" link.symlink_to(outside) import pytest with pytest.raises(ValueError, match="Access denied"): tools.validate_path("escape_link/secret.txt") def test_absolute_path_inside_workdir(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "inside.txt").write_text("ok") abs_path = str(tmp_path / "inside.txt") result = tools.validate_path(abs_path) assert result == str((tmp_path / "inside.txt").resolve()) # ── 6.3 read_file ────────────────────────────────────────────────────────── class TestReadFile: def test_read_existing_file(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "test.txt").write_text("hello world") result = tools.read_file.invoke({"path": "test.txt"}) assert result == "hello world" def test_read_missing_file(self, tmp_path): _set_workdir(tmp_path) result = tools.read_file.invoke({"path": "nonexistent.txt"}) assert "Error: File not found" in result def test_read_empty_file(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "empty.txt").write_text("") result = tools.read_file.invoke({"path": "empty.txt"}) assert result == "" def test_read_file_outside_workdir(self, tmp_path): _set_workdir(tmp_path) result = tools.read_file.invoke({"path": "../outside.txt"}) assert "Access denied" in result # ── 6.4 grep ──────────────────────────────────────────────────────────────── class TestGrep: def test_matches_found(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "data.txt").write_text("foo bar\nbaz\nfoo again") result = tools.grep.invoke({"pattern": "foo"}) assert "data.txt" in result assert "foo bar" in result assert "foo again" in result def test_no_matches(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "data.txt").write_text("hello world") result = tools.grep.invoke({"pattern": "zzz_missing"}) assert "No matches found" in result def test_search_specific_directory(self, tmp_path): _set_workdir(tmp_path) sub = tmp_path / "subdir" sub.mkdir() (sub / "a.txt").write_text("needle here") (tmp_path / "b.txt").write_text("needle there") result = tools.grep.invoke({"pattern": "needle", "path": "subdir"}) assert "a.txt" in result # b.txt is outside the searched subdir assert "b.txt" not in result def test_grep_path_outside_workdir(self, tmp_path): _set_workdir(tmp_path) result = tools.grep.invoke({"pattern": "x", "path": "../../etc"}) assert "Access denied" in result # ── 6.5 write_file ───────────────────────────────────────────────────────── class TestWriteFile: def test_write_new_file(self, tmp_path): _set_workdir(tmp_path) result = tools.write_file.invoke({"path": "new.txt", "content": "hello"}) assert "Successfully wrote" in result assert (tmp_path / "new.txt").read_text() == "hello" def test_overwrite_existing_file(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "exist.txt").write_text("old") result = tools.write_file.invoke({"path": "exist.txt", "content": "new"}) assert "Successfully wrote" in result assert (tmp_path / "exist.txt").read_text() == "new" def test_write_creates_nested_dirs(self, tmp_path): _set_workdir(tmp_path) result = tools.write_file.invoke( {"path": "a/b/c/deep.txt", "content": "deep"} ) assert "Successfully wrote" in result assert (tmp_path / "a" / "b" / "c" / "deep.txt").read_text() == "deep" def test_write_path_outside_workdir(self, tmp_path): _set_workdir(tmp_path) result = tools.write_file.invoke( {"path": "../../evil.txt", "content": "bad"} ) assert "Access denied" in result # ── 6.6 list_dir ─────────────────────────────────────────────────────────── class TestListDir: def test_list_valid_directory(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "file.txt").write_text("x") (tmp_path / "subdir").mkdir() result = tools.list_dir.invoke({"path": "."}) assert "[FILE] file.txt" in result assert "[DIR] subdir" in result def test_list_missing_path(self, tmp_path): _set_workdir(tmp_path) result = tools.list_dir.invoke({"path": "nonexistent"}) assert "Error: Path not found" in result def test_list_file_path(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "afile.txt").write_text("x") result = tools.list_dir.invoke({"path": "afile.txt"}) assert "Error: Not a directory" in result def test_list_path_outside_workdir(self, tmp_path): _set_workdir(tmp_path) result = tools.list_dir.invoke({"path": "../../"}) assert "Access denied" in result # ── 6.8 replace_in_file ──────────────────────────────────────────────────── class TestReplaceInFile: def test_replace_single_occurrence(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "test.txt").write_text("hello world") result = tools.replace_in_file.invoke( {"path": "test.txt", "search": "world", "replace": "universe"} ) assert "Successfully replaced 1 occurrence" in result assert (tmp_path / "test.txt").read_text() == "hello universe" def test_replace_multiple_occurrences(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "test.txt").write_text("foo bar foo baz foo") result = tools.replace_in_file.invoke( {"path": "test.txt", "search": "foo", "replace": "qux"} ) assert "Successfully replaced 3 occurrence" in result assert (tmp_path / "test.txt").read_text() == "qux bar qux baz qux" def test_replace_not_found(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "test.txt").write_text("hello world") result = tools.replace_in_file.invoke( {"path": "test.txt", "search": "missing", "replace": "new"} ) assert "Error: Search text not found" in result def test_replace_missing_file(self, tmp_path): _set_workdir(tmp_path) result = tools.replace_in_file.invoke( {"path": "nonexistent.txt", "search": "a", "replace": "b"} ) assert "Error: File not found" in result def test_replace_path_outside_workdir(self, tmp_path): _set_workdir(tmp_path) result = tools.replace_in_file.invoke( {"path": "../outside.txt", "search": "a", "replace": "b"} ) assert "Access denied" in result def test_replace_on_directory(self, tmp_path): _set_workdir(tmp_path) (tmp_path / "subdir").mkdir() result = tools.replace_in_file.invoke( {"path": "subdir", "search": "a", "replace": "b"} ) assert "Error: Not a file" in result