Skip to content

Commit

Permalink
envoy.base.utils(0.5.10): Add directory output to fetch tool
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax committed Jan 14, 2025
1 parent 5c70573 commit 498f309
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion envoy.base.utils/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.9
0.5.10
5 changes: 4 additions & 1 deletion envoy.base.utils/envoy/base/utils/fetch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def downloads(self) -> dict[str, dict]:

@cached_property
def downloads_path(self) -> pathlib.Path:
if self.args.output == "dir":
return pathlib.Path(self.args.output_path)
return pathlib.Path(self.tempdir.name).joinpath("downloads")

@cached_property
Expand Down Expand Up @@ -207,7 +209,8 @@ async def run(self) -> int | None:
print(json.dumps(result))
return 0
exit_now = (
not self.args.output_path
self.args.output == "dir"
or not self.args.output_path
or not self.downloads_path.exists()
or not any(self.downloads_path.iterdir()))
if exit_now:
Expand Down
50 changes: 32 additions & 18 deletions envoy.base.utils/tests/test_fetch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,38 @@ def test_fetchrunner_downloads(patches):
assert "downloads" in runner.__dict__


def test_fetchrunner_downloads_path(patches):
@pytest.mark.parametrize("output_dir", [True, False])
def test_fetchrunner_downloads_path(patches, output_dir):
runner = utils.FetchRunner()
patched = patches(
"pathlib",
("FetchRunner.args",
dict(new_callable=PropertyMock)),
("FetchRunner.tempdir",
dict(new_callable=PropertyMock)),
prefix="envoy.base.utils.fetch_runner")

with patched as (m_plib, m_temp):
with patched as (m_plib, m_args, m_temp):
if output_dir:
m_args.return_value.output = "dir"
assert (
runner.downloads_path
== m_plib.Path.return_value.joinpath.return_value)
== (m_plib.Path.return_value.joinpath.return_value
if not output_dir
else m_plib.Path.return_value))

assert "downloads_path" in runner.__dict__
assert (
m_plib.Path.call_args
== [(m_temp.return_value.name, ), {}])
assert (
m_plib.Path.return_value.joinpath.call_args
== [("downloads", ), {}])

assert "downloads_path" in runner.__dict__
== [((m_temp.return_value.name
if not output_dir
else m_args.return_value.output_path), ), {}])
if not output_dir:
assert (
m_plib.Path.return_value.joinpath.call_args
== [("downloads", ), {}])
else:
assert not m_plib.Path.return_value.joinpath.called


@pytest.mark.parametrize("excludes", ["", "EXCLUDE_PATH"])
Expand Down Expand Up @@ -593,7 +604,7 @@ def test_fetchrunner_hashed(patches):
== [(), {}])


@pytest.mark.parametrize("output", ["json", "NOTJSON"])
@pytest.mark.parametrize("output", ["json", "dir", "OTHER"])
@pytest.mark.parametrize("path", ["", "PATH"])
@pytest.mark.parametrize("exists", [True, False])
@pytest.mark.parametrize("empty", [True, False])
Expand Down Expand Up @@ -660,17 +671,20 @@ async def _concurrent():
m_log.return_value.debug.call_args_list[:5]
== [[(f"{m_elapsed.return_value} Received:\n {x}\n", ), {}]
for x in items])

if output == "json":
if output in ("json", "dir"):
assert result == 0
assert (
m_print.call_args
== [(m_json.dumps.return_value, ), {}])
assert len(m_log.return_value.debug.call_args_list) == 5
assert (
m_json.dumps.call_args
== [({k: v.decode() for k, v in items.items()},), {}])
assert not m_asyncio.to_thread.called
if output == "json":
assert (
m_print.call_args
== [(m_json.dumps.return_value, ), {}])
assert (
m_json.dumps.call_args
== [({k: v.decode() for k, v in items.items()},), {}])
else:
assert not m_print.called
assert not m_json.dumps.called
return
if not path:
assert result == 0
Expand Down

0 comments on commit 498f309

Please sign in to comment.