Skip to content

Commit

Permalink
Add json output option to komodo-non-deployed
Browse files Browse the repository at this point in the history
This eases integration with Github actions
  • Loading branch information
berland committed Jan 16, 2023
1 parent fb261c7 commit 061dcbe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
19 changes: 15 additions & 4 deletions komodo/deployed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import argparse
import json
import os
from typing import List, Optional

from komodo.matrix import get_matrix_base

Expand Down Expand Up @@ -29,18 +31,27 @@ def _fetch_non_deployed_releases(install_root, release_folder):
return list(set(releases) - set(deployed))


def fetch_non_deployed(install_root, releases_folder, limit=None):
def fetch_non_deployed(
install_root: str, releases_folder: str, limit: Optional[int] = None
) -> List[str]:
non_deployed = _fetch_non_deployed_releases(install_root, releases_folder)
return non_deployed[:limit]


def output_formatter(release_list: List[str], do_json: bool = False) -> str:
if do_json:
return json.dumps(release_list)
return "\n".join(release_list)


def deployed_main():
parser = argparse.ArgumentParser(
description=(
"""Outputs the name of undeployed matrices given an installation
root and a release folder. A partially deployed matrix is
considered deployed."""
)
),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"install_root",
Expand All @@ -62,6 +73,7 @@ def deployed_main():
default=None,
help="The maximum number of undeployed matrices to list.",
)
parser.add_argument("--json", action="store_true", help="Get output in JSON format")

args = parser.parse_args()
non_deployed = fetch_non_deployed(
Expand All @@ -70,5 +82,4 @@ def deployed_main():
limit=args.limit,
)

if non_deployed:
print("\n".join(non_deployed))
print(output_formatter(non_deployed, do_json=args.json))
17 changes: 16 additions & 1 deletion tests/test_non_deployed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

from komodo.deployed import fetch_non_deployed
import pytest

from komodo.deployed import fetch_non_deployed, output_formatter


def _create_links(links, root=""):
Expand Down Expand Up @@ -75,3 +77,16 @@ def test_links_ignored(tmpdir):

non_deployed = fetch_non_deployed(install_root, release_folder)
assert len(non_deployed) == 0, non_deployed


@pytest.mark.parametrize(
"release_list, do_json, expected",
[
(["a"], False, "a"),
(["a"], True, '["a"]'),
(["a", "b"], False, "a\nb"),
(["a", "b"], True, '["a", "b"]'),
],
)
def test_output_formatter(release_list, do_json, expected):
assert output_formatter(release_list, do_json) == expected

0 comments on commit 061dcbe

Please sign in to comment.