Skip to content

Commit

Permalink
Add flag to disable output (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani authored May 21, 2024
1 parent 320a7da commit 5f61294
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/packaged/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import subprocess
import sys
import tempfile
from typing import cast, TYPE_CHECKING
from unittest import mock

import yen.github
from yaspin import yaspin

if TYPE_CHECKING:
from yaspin.core import Yaspin

MAKESELF_PATH = os.path.join(os.path.dirname(__file__), "makeself.sh")
DEFAULT_PYTHON_VERSION = "3.12"
PACKAGED_PYTHON_FOLDER_NAME = ".packaged_python"
Expand Down Expand Up @@ -38,6 +43,7 @@ def create_package(
build_command: str,
startup_command: str,
python_version: str,
quiet: bool = False,
) -> None:
"""Create the makeself executable, with the startup script in it."""
if source_directory is None:
Expand Down Expand Up @@ -70,7 +76,11 @@ def create_package(

# Run the build command in the source directory, while making sure
# that `python` and related binaries point to the installed python
spinner = yaspin(text="Running the build command...")
if quiet:
spinner = cast("Yaspin", mock.Mock())
else:
spinner = yaspin(text="Running the build command...")

spinner.start()
try:
subprocess.run(
Expand Down Expand Up @@ -177,7 +187,9 @@ def create_package(

finally:
spinner.stop()
print(f"Package {output_path!r} built successfully!")
if not quiet:
print(f"Package {output_path!r} built successfully!")

# Cleanup the packaged python and startup script from source directory
if os.path.exists(startup_script_path):
os.remove(startup_script_path)
Expand Down
9 changes: 7 additions & 2 deletions src/packaged/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from __future__ import annotations

import argparse
import dataclasses
import os.path
import platform
import sys
import typing

from packaged import (
DEFAULT_PYTHON_VERSION,
Expand Down Expand Up @@ -69,6 +67,12 @@ def cli(argv: list[str] | None = None) -> int:
help="Version of Python to package your project with.",
default=DEFAULT_PYTHON_VERSION,
)
parser.add_argument(
"--quiet",
help="Disable all output",
action="store_true",
default="CI" in os.environ,
)
args = parser.parse_args(argv)
config = Config(**vars(args))

Expand All @@ -79,6 +83,7 @@ def cli(argv: list[str] | None = None) -> int:
config.build_command,
config.startup_command,
config.python_version,
config.quiet,
)
except SourceDirectoryNotFound as exc:
error(f"Folder {exc.directory_path!r} does not exist.")
Expand Down
2 changes: 2 additions & 0 deletions src/packaged/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config:
build_command: str
startup_command: str
python_version: str
quiet: bool


CONFIG_NAME = "./packaged.toml"
Expand Down Expand Up @@ -54,6 +55,7 @@ def parse_config(source_directory: str) -> Config:
config_data["build_command"],
config_data["startup_command"],
config_data.get("python_version", "3.12"),
config_data.get("quiet", "CI" in os.environ),
)
except KeyError as exc:
key = exc.args[0]
Expand Down
57 changes: 55 additions & 2 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import annotations

import os
from unittest import mock

from pytest import MonkeyPatch

import packaged
import packaged.cli


def test_cli() -> None:
def test_cli(monkeypatch: MonkeyPatch) -> None:
"""Ensures that CLI passes args to `create_package()` properly."""
with mock.patch.object(packaged.cli, "create_package") as mocked:
packaged.cli.cli(["./foo", "pip install foo", "python -m foo"])
Expand All @@ -18,6 +21,7 @@ def test_cli() -> None:
"pip install foo",
"python -m foo",
packaged.DEFAULT_PYTHON_VERSION,
False,
)

with mock.patch.object(packaged.cli, "create_package") as mocked:
Expand All @@ -26,7 +30,9 @@ def test_cli() -> None:
)

# specified python version
mocked.assert_called_with(None, "./baz", "pip install baz", "python -m baz", "3.10")
mocked.assert_called_with(
None, "./baz", "pip install baz", "python -m baz", "3.10", False
)

with mock.patch.object(packaged.cli, "create_package") as mocked:
packaged.cli.cli(
Expand All @@ -45,6 +51,53 @@ def test_cli() -> None:
"pip install -rrequirements.txt",
"python src/mypackage/cli.py",
packaged.DEFAULT_PYTHON_VERSION,
False,
)
args = mocked.call_args[0]
assert args[0].endswith("/mypackage")

# Test --quiet
with mock.patch.object(packaged.cli, "create_package") as mocked:
packaged.cli.cli(
["./some", "pip install some", "python some.py", "./some.bin", "--quiet"]
)

# quiet is True
mocked.assert_called_with(
mock.ANY,
"./some",
"pip install some",
"python some.py",
packaged.DEFAULT_PYTHON_VERSION,
True,
)

# Test --quiet when CI is true, regardless of if the flag is passed
monkeypatch.setattr(os, "environ", {"CI": "1"})
with mock.patch.object(packaged.cli, "create_package") as mocked:
packaged.cli.cli(["./some", "pip install some", "python some.py", "./some.bin"])

# quiet is True
mocked.assert_called_with(
mock.ANY,
"./some",
"pip install some",
"python some.py",
packaged.DEFAULT_PYTHON_VERSION,
True,
)
monkeypatch.setattr(os, "environ", {"CI": "1"})
with mock.patch.object(packaged.cli, "create_package") as mocked:
packaged.cli.cli(
["./some", "pip install some", "python some.py", "./some.bin", "--quiet"]
)

# quiet is True
mocked.assert_called_with(
mock.ANY,
"./some",
"pip install some",
"python some.py",
packaged.DEFAULT_PYTHON_VERSION,
True,
)

0 comments on commit 5f61294

Please sign in to comment.