From 061dcbef8280c7b77c994b2a09c00fb5b08103bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Berland?= Date: Fri, 13 Jan 2023 15:34:28 +0100 Subject: [PATCH] Add json output option to komodo-non-deployed This eases integration with Github actions --- komodo/deployed.py | 19 +++++++++++++++---- tests/test_non_deployed.py | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/komodo/deployed.py b/komodo/deployed.py index 3ad2f207b..dbded43b7 100644 --- a/komodo/deployed.py +++ b/komodo/deployed.py @@ -1,5 +1,7 @@ import argparse +import json import os +from typing import List, Optional from komodo.matrix import get_matrix_base @@ -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", @@ -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( @@ -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)) diff --git a/tests/test_non_deployed.py b/tests/test_non_deployed.py index 8e161b82f..8167cc8a8 100644 --- a/tests/test_non_deployed.py +++ b/tests/test_non_deployed.py @@ -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=""): @@ -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