Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThejasNU committed Feb 3, 2025
1 parent 5925aa1 commit a910fad
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 11 deletions.
19 changes: 9 additions & 10 deletions libs/agentc_core/agentc_core/catalog/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ScanDirectoryOpts(typing.TypedDict):


def scan_directory(
root_dir: str, wanted_patterns: typing.Iterable[str], opts: ScanDirectoryOpts = None
root_dir: str, target_dir: str, wanted_patterns: typing.Iterable[str], opts: ScanDirectoryOpts = None
) -> typing.Iterable[pathlib.Path]:
"""
Find file paths in a directory tree which match wanted glob patterns, while also handling any ignore
Expand All @@ -23,29 +23,28 @@ def scan_directory(

ignore_file_parsers = []
all_ignore_files_paths = []
if opts:
current_dir = os.getcwd()
user_target_dir = os.path.abspath(os.path.join(current_dir, root_dir))
user_target_dir = os.path.abspath(os.path.join(root_dir, target_dir))

if opts:
# Find all ignore files in the directory tree till user mentioned directory.
for root, _dirs, files in os.walk(current_dir):
for cur_dir, _dirs, files in os.walk(root_dir):
# Ignore path if it does not appear in the path towards user mentioned directory.
if root not in user_target_dir:
if cur_dir not in user_target_dir:
continue

for file in files:
if file in opts["ignore_file_names"]:
all_ignore_files_paths.append(os.path.join(root, file))
all_ignore_files_paths.append(os.path.join(cur_dir, file))

# Stop crawling once user mentioned directory is crawled.
if root == user_target_dir:
if cur_dir == user_target_dir:
break

if opts["ignore_file_parser_factory"]:
for ignore_file_path in all_ignore_files_paths:
ignore_file_parsers.append(opts["ignore_file_parser_factory"](ignore_file_path))

for path in pathlib.Path(root_dir).rglob("*"):
for path in pathlib.Path(user_target_dir).rglob("*"):
if len(ignore_file_parsers) > 0 and any(ignore_file_parser(path) for ignore_file_parser in ignore_file_parsers):
logger.debug(f"Ignoring file {path.absolute()}.")
continue
Expand All @@ -60,5 +59,5 @@ def scan_directory(
import sys

# Ex: python3 agentc_core/catalog/directory.py "*.py" "*.md"
for x in scan_directory("", sys.argv[1:]):
for x in scan_directory("", "", sys.argv[1:]):
print(x)
5 changes: 4 additions & 1 deletion libs/agentc_core/agentc_core/catalog/index.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import fnmatch
import logging
import os
import tqdm
import typing

Expand Down Expand Up @@ -106,6 +107,7 @@ def index_catalog_start(

logger.debug(f"Now crawling source directories. [{','.join(d for d in source_dirs)}]")
printer(f"Crawling {','.join(d for d in source_dirs)}:")

source_files = list()
if kind == "tool":
source_globs = [i.glob_pattern for i in AllIndexers if all(k.is_tool() for k in i.kind)]
Expand All @@ -114,7 +116,8 @@ def index_catalog_start(
else:
raise ValueError(f"Unknown kind: {kind}")
for source_dir in source_dirs:
source_files += scan_directory(source_dir, source_globs, opts=scan_directory_opts)
source_files += scan_directory(os.getcwd(), source_dir, source_globs, opts=scan_directory_opts)

all_errs = []
all_descriptors = []
source_iterable = tqdm.tqdm(source_files) if print_progress else source_files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prompts/prompt2.prompt
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
prompt1.jinja
tool2.yaml
Empty file.
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions libs/agentc_core/tests/catalog/test_scan_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import pathlib

from agentc_core.catalog.directory import scan_directory
from agentc_core.defaults import DEFAULT_SCAN_DIRECTORY_OPTS
from agentc_core.indexer.indexer import AllIndexers


def test_scan_dir_tools():
root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", "scan_files")
source_globs = [i.glob_pattern for i in AllIndexers if all(k.is_tool() for k in i.kind)]
output = []
output += scan_directory(root_dir, "tools", source_globs, opts=DEFAULT_SCAN_DIRECTORY_OPTS)

assert (
pathlib.PosixPath(os.path.join(root_dir, "tools", "tool3.sqlpp")) in output
and pathlib.PosixPath(os.path.join(root_dir, "tools", "tool4.py")) in output
and pathlib.PosixPath(os.path.join(root_dir, "tools", "tool2.sqlpp")) not in output
and pathlib.PosixPath(os.path.join(root_dir, "tool1.py")) not in output
)


def test_scan_dir_prompts():
root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", "scan_files")
source_globs = [i.glob_pattern for i in AllIndexers if all(k.is_prompt() for k in i.kind)]
output = []
output += scan_directory(root_dir, "prompts", source_globs, opts=DEFAULT_SCAN_DIRECTORY_OPTS)

assert (
pathlib.PosixPath(os.path.join(root_dir, "prompts", "prompt1.jinja")) in output
and pathlib.PosixPath(os.path.join(root_dir, "prompts", "prompt2.prompt")) not in output
)

0 comments on commit a910fad

Please sign in to comment.