Skip to content

Commit

Permalink
Use iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoranzo committed Apr 15, 2024
1 parent 1fa011c commit 60b5f66
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/tool_shed/test/base/populators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import (
Iterator,
List,
Optional,
Union,
Expand Down Expand Up @@ -56,28 +57,21 @@
TEST_DATA_REPO_FILES = resource_path(__package__, "../test_data")


def repo_files(test_data_path: str) -> List[Path]:
def repo_files(test_data_path: str) -> Iterator[Path]:
repos = TEST_DATA_REPO_FILES.joinpath(f"repos/{test_data_path}")
paths = []
for trav in repos.iterdir():
with as_file(trav) as path:
paths.append(path)
return sorted(paths)
for child in sorted(_.name for _ in repos.iterdir()):
with as_file(repos.joinpath(child)) as path:
yield path


def repo_tars(test_data_path: str) -> List[Path]:
tar_paths = []
def repo_tars(test_data_path: str) -> Iterator[Path]:
for path in repo_files(test_data_path):
if path.is_dir():
prefix = f"shedtest_{test_data_path}_{path.name}_"
tf = NamedTemporaryFile(delete=False, prefix=prefix)
assert path.is_dir()
prefix = f"shedtest_{test_data_path}_{path.name}_"
with NamedTemporaryFile(prefix=prefix) as tf:
with tarfile.open(tf.name, "w:gz") as tar:
tar.add(str(path.absolute()), arcname=test_data_path or path.name)
tar_path = Path(tf.name)
else:
tar_path = path
tar_paths.append(tar_path)
return tar_paths
yield Path(tf.name)


class HostsTestToolShed(Protocol):
Expand Down

0 comments on commit 60b5f66

Please sign in to comment.