Skip to content

Commit

Permalink
Change --json option to --output tree|json
Browse files Browse the repository at this point in the history
  • Loading branch information
txels committed Jun 27, 2022
1 parent b7c6c6f commit 8874266
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/pypi_changes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def main(args: Sequence[str] | None = None) -> int:
options = parse_cli_arguments(args)
distributions = collect_distributions(options)
info = pypi_info(distributions, options)
if options.json:
print_json(info, options)
else:

if options.output == "tree":
print_tree(info, options)
elif options.output == "json":
print_json(info, options)
return 0


Expand Down
9 changes: 8 additions & 1 deletion src/pypi_changes/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ def _define_cli_arguments() -> ArgumentParser:
nargs="?",
)

parser.add_argument("--json", action="store_true", default=False, help="Show output in JSON format")
parser.add_argument(
"--output",
"-o",
help="Choose output format",
choices=["tree", "json"],
default="tree",
dest="output",
)

parser.add_argument("python", help="python interpreter to inspect", metavar="PYTHON_EXE", action=_Python)

Expand Down
1 change: 1 addition & 0 deletions src/pypi_changes/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ def print_json(distributions: Iterable[Package], options: Options) -> None:

__all__ = [
"print_tree",
"print_json",
]
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_cli_ok_default(tmp_path: Path, mocker: MockerFixture) -> None:
"cache_duration": 3600,
"python": tmp_path,
"sort": "updated",
"json": False,
"output": "tree",
}
assert user_cache_path.call_args == call(appname="pypi_changes", appauthor="gaborbernat", version=__version__)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def test_info_pypi_server_timeout(
tmp_path: Path, mocker: MockerFixture, option_simple: Options, make_dist: MakeDist
) -> None:
dist = make_dist(tmp_path, "a", "1.0")
mocker.patch("requests.Session.get", side_effect=TimeoutError)
mock_cached_session = mocker.patch("pypi_changes._info.CachedSession")
mock_cached_session.return_value.__enter__.return_value.get.side_effect = TimeoutError

packages = list(pypi_info([dist], option_simple))

assert isinstance(packages, list)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_pypi_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from pypi_changes import main


def test_pypi_changes_self(tmp_path: Path) -> None:
def test_pypi_changes_self_output_default(tmp_path: Path) -> None:
venv = cli_run([str(tmp_path / "venv")], setup_logging=False)
main([str(venv.creator.exe), "--cache-path", str(tmp_path / "a.sqlite")])


def test_pypi_changes_self_json(tmp_path: Path) -> None:
def test_pypi_changes_self_output_json(tmp_path: Path) -> None:
venv = cli_run([str(tmp_path / "venv")], setup_logging=False)
main([str(venv.creator.exe), "--json"])
main([str(venv.creator.exe), "--output", "json"])

0 comments on commit 8874266

Please sign in to comment.