Home / Class/ TestFilesystemGrepSearch Class — langchain Architecture

TestFilesystemGrepSearch Class — langchain Architecture

Architecture documentation for the TestFilesystemGrepSearch class in test_file_search.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74["TestFilesystemGrepSearch"]
  5e183a7b_f937_e2fe_e90a_7c6aea33188b["StructuredTool"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|extends| 5e183a7b_f937_e2fe_e90a_7c6aea33188b
  fd751914_766d_3dc6_73f9_3e0a51985938["test_file_search.py"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|defined in| fd751914_766d_3dc6_73f9_3e0a51985938
  63a90ea5_0fab_2b80_7e27_98d637e13851["test_grep_invalid_include_pattern()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 63a90ea5_0fab_2b80_7e27_98d637e13851
  06919874_a62c_1075_68d3_ccf60969515a["test_ripgrep_command_uses_literal_pattern()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 06919874_a62c_1075_68d3_ccf60969515a
  2e2518f5_1068_68a1_ae83_7fca3e90f48a["test_grep_basic_search_python_fallback()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 2e2518f5_1068_68a1_ae83_7fca3e90f48a
  07ba06d7_0b02_3d77_e177_dd60a7e87f76["test_grep_with_include_filter()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 07ba06d7_0b02_3d77_e177_dd60a7e87f76
  a0ad3c1b_8900_9f92_49e1_a37153a803ff["test_grep_output_mode_content()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| a0ad3c1b_8900_9f92_49e1_a37153a803ff
  77c884a0_34b0_99a4_8558_6afb4a6be9c3["test_grep_output_mode_count()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 77c884a0_34b0_99a4_8558_6afb4a6be9c3
  25cdcacf_0e04_3fb2_7c80_e4bcdc4e3dc4["test_grep_invalid_regex_pattern()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 25cdcacf_0e04_3fb2_7c80_e4bcdc4e3dc4
  0d292765_278e_dc9e_ba87_cbfa3fcd84ba["test_grep_no_matches()"]
  3358c7da_0c80_0cf4_72fe_8cc779a1ec74 -->|method| 0d292765_278e_dc9e_ba87_cbfa3fcd84ba

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_file_search.py lines 17–137

class TestFilesystemGrepSearch:
    """Tests for filesystem-backed grep search."""

    def test_grep_invalid_include_pattern(self, tmp_path: Path) -> None:
        """Return error when include glob cannot be parsed."""
        (tmp_path / "example.py").write_text("print('hello')\n", encoding="utf-8")

        middleware = FilesystemFileSearchMiddleware(root_path=str(tmp_path), use_ripgrep=False)

        assert isinstance(middleware.grep_search, StructuredTool)
        assert middleware.grep_search.func is not None
        result = middleware.grep_search.func(pattern="print", include="*.{py")

        assert result == "Invalid include pattern"

    def test_ripgrep_command_uses_literal_pattern(
        self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        """Ensure ripgrep receives pattern after ``--`` to avoid option parsing."""
        (tmp_path / "example.py").write_text("print('hello')\n", encoding="utf-8")

        middleware = FilesystemFileSearchMiddleware(root_path=str(tmp_path), use_ripgrep=True)

        captured: dict[str, list[str]] = {}

        class DummyResult:
            stdout = ""

        def fake_run(*args: Any, **kwargs: Any) -> DummyResult:
            cmd = args[0]
            captured["cmd"] = cmd
            return DummyResult()

        monkeypatch.setattr("langchain.agents.middleware.file_search.subprocess.run", fake_run)

        middleware._ripgrep_search("--pattern", "/", None)

        assert "cmd" in captured
        cmd = captured["cmd"]
        assert cmd[:2] == ["rg", "--json"]
        assert "--" in cmd
        separator_index = cmd.index("--")
        assert cmd[separator_index + 1] == "--pattern"

    def test_grep_basic_search_python_fallback(self, tmp_path: Path) -> None:
        """Test basic grep search using Python fallback."""
        (tmp_path / "file1.py").write_text("def hello():\n    pass\n", encoding="utf-8")
        (tmp_path / "file2.py").write_text("def world():\n    pass\n", encoding="utf-8")
        (tmp_path / "file3.txt").write_text("hello world\n", encoding="utf-8")

        middleware = FilesystemFileSearchMiddleware(root_path=str(tmp_path), use_ripgrep=False)

        assert isinstance(middleware.grep_search, StructuredTool)
        assert middleware.grep_search.func is not None
        result = middleware.grep_search.func(pattern="hello")

        assert "/file1.py" in result
        assert "/file3.txt" in result
        assert "/file2.py" not in result

    def test_grep_with_include_filter(self, tmp_path: Path) -> None:
        """Test grep search with include pattern filter."""
        (tmp_path / "file1.py").write_text("def hello():\n    pass\n", encoding="utf-8")
        (tmp_path / "file2.txt").write_text("hello world\n", encoding="utf-8")

        middleware = FilesystemFileSearchMiddleware(root_path=str(tmp_path), use_ripgrep=False)

        assert isinstance(middleware.grep_search, StructuredTool)
        assert middleware.grep_search.func is not None
        result = middleware.grep_search.func(pattern="hello", include="*.py")

        assert "/file1.py" in result
        assert "/file2.txt" not in result

    def test_grep_output_mode_content(self, tmp_path: Path) -> None:
        """Test grep search with content output mode."""
        (tmp_path / "test.py").write_text("line1\nhello\nline3\n", encoding="utf-8")

        middleware = FilesystemFileSearchMiddleware(root_path=str(tmp_path), use_ripgrep=False)

        assert isinstance(middleware.grep_search, StructuredTool)

Extends

Frequently Asked Questions

What is the TestFilesystemGrepSearch class?
TestFilesystemGrepSearch is a class in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_file_search.py.
Where is TestFilesystemGrepSearch defined?
TestFilesystemGrepSearch is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_file_search.py at line 17.
What does TestFilesystemGrepSearch extend?
TestFilesystemGrepSearch extends StructuredTool.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free