Skip to content

Commit

Permalink
Fix issues: #274, #280 (#282)
Browse files Browse the repository at this point in the history
* Fix issue with reading directories in `iterate_files()` (#280)
* Add directory checking logic in `iterate_files()` (#274)
* Added tests for #282, #274, #280

---------

Co-authored-by: Simon Willison <[email protected]>
  • Loading branch information
ealvar3z and simonw authored Sep 19, 2023
1 parent bb99186 commit 839b4d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,13 @@ def count_files():

def iterate_files():
for directory, pattern in files:
p = pathlib.Path(directory)
if not p.exists() or not p.is_dir():
# fixes issue/274 - raise error if directory does not exist
raise click.UsageError(f"Invalid directory: {directory}")
for path in pathlib.Path(directory).glob(pattern):
if path.is_dir():
continue # fixed issue/280 - skip directories
relative = path.relative_to(directory)
content = None
if binary:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_embed_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def test_embed_multi_files(multi_files, scenario):
("nested/two.txt", b"two"),
("nested/more/three.txt", b"three"),
# This tests the fallback to latin-1 encoding:
("nested/more/ignored.ini", b"Has weird \x96 character"),
("nested/more.txt/ignored.ini", b"Has weird \x96 character"),
):
path = pathlib.Path(files / filename)
path.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -483,6 +483,20 @@ def test_embed_multi_files(multi_files, scenario):
]


@pytest.mark.parametrize(
"args,expected_error",
((["not-a-dir", "*.txt"], "Invalid directory: not-a-dir"),),
)
def test_embed_multi_files_errors(multi_files, args, expected_error):
runner = CliRunner()
result = runner.invoke(
cli,
["embed-multi", "files", "-m", "embed-demo", "--files"] + args,
)
assert result.exit_code == 2
assert expected_error in result.output


@pytest.mark.parametrize(
"extra_args,expected_error",
(
Expand Down

0 comments on commit 839b4d7

Please sign in to comment.