Fix issues: #274, #280 (#282)

* 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 <swillison@gmail.com>
This commit is contained in:
e. alvarez 2023-09-18 23:14:30 -07:00 committed by GitHub
parent bb99186cc1
commit 839b4d7161
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -1268,7 +1268,13 @@ def embed_multi(
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:

View file

@ -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)
@ -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",
(