Skip to content

Commit

Permalink
Add testing arg for install()
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoxmt committed Dec 3, 2024
1 parent a6f5e21 commit af3bff9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/tidocs/pandoc_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ def save_version_info(self) -> None:
with open(self.version_file, "w") as f:
f.write(self.VERSION)

def install(self) -> Path:
def install(self, testing: bool = False) -> Path:
"""Download and install Pandoc if necessary.
Downloads and extracts the appropriate Pandoc binary for the current platform if it's not already installed or if the version doesn't match requirements.
Args:
testing (bool): If True, suppress progress output for testing
Returns:
Path: Path to the installed Pandoc binary.
Expand All @@ -109,9 +112,10 @@ def install(self) -> Path:

def _progress_callback(count: int, block_size: int, total_size: int) -> None:
"""Display download progress as percentage."""
percent = int(count * block_size * 100 / total_size)
sys.stdout.write(f"\rDownloading Pandoc: {percent}%")
sys.stdout.flush()
if not testing:
percent = int(count * block_size * 100 / total_size)
sys.stdout.write(f"\rDownloading Pandoc: {percent}%")
sys.stdout.flush()

# Clean existing binary if present
if self.pandoc_binary.exists():
Expand All @@ -133,9 +137,11 @@ def _progress_callback(count: int, block_size: int, total_size: int) -> None:
_, _, archive_name = url.rpartition("/")
archive_path = tmp_dir_path / archive_name

print(f"Downloading from {url} to {archive_path}")
if not testing:
print(f"Downloading from {url} to {archive_path}")
urllib.request.urlretrieve(url, archive_path, _progress_callback)
print() # New line after progress
if not testing:
print() # New line after progress

# Extract archive
extracted = None
Expand Down Expand Up @@ -191,6 +197,7 @@ def run(self, args: list, stdin: Optional[bytes] = None) -> (bytes, bytes):
Example:
>>> pandoc = Pandoc()
>>> _ = pandoc.install(testing=True)
>>> output, err = pandoc.run(["--version"])
>>> output.decode("utf-8").startswith(f"pandoc {pandoc.VERSION}")
True
Expand Down

0 comments on commit af3bff9

Please sign in to comment.