Skip to content

Commit 9adf6b6

Browse files
authored
Merge pull request #167 from input-output-hk/fix_subcommand_args
Distinguish between arguments for nested subcommands
2 parents 8cde110 + e832645 commit 9adf6b6

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

cardano_clusterlib/clusterlib_klass.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ def cli(self, cli_args: List[str]) -> structs.CLIOut:
173173
Returns:
174174
structs.CLIOut: A tuple containing command stdout and stderr.
175175
"""
176-
cli_args_strs = [str(arg) for arg in cli_args]
177-
cli_args_strs.insert(0, "cardano-cli")
176+
cli_args_strs_all = [str(arg) for arg in cli_args]
177+
cli_args_strs_all.insert(0, "cardano-cli")
178+
cli_args_strs = [arg for arg in cli_args_strs_all if arg != consts.SUBCOMMAND_MARK]
178179

179180
cmd_str = clusterlib_helpers._format_cli_args(cli_args=cli_args_strs)
180181
clusterlib_helpers._write_cli_log(clusterlib_obj=self, command=cmd_str)
181182
LOGGER.debug("Running `%s`", cmd_str)
182183

183-
coverage.record_cli_coverage(cli_args=cli_args_strs, coverage_dict=self.cli_coverage)
184+
coverage.record_cli_coverage(cli_args=cli_args_strs_all, coverage_dict=self.cli_coverage)
184185

185186
# re-run the command when running into
186187
# Network.Socket.connect: <socket: X>: resource exhausted (Resource temporarily unavailable)

cardano_clusterlib/consts.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
}
1010

1111

12+
# The SUBCOMMAND_MARK is used to mark the beginning of a subcommand. It is used to differentiate
13+
# between options and subcommands. That is needed for CLI coverage recording.
14+
# For example, the command `cardano-cli query tx-mempool --cardano-mode info`
15+
# has the following arguments:
16+
# ["query", "tx-mempool", "--cardano-mode", SUBCOMMAND_MARK, "info"]
17+
SUBCOMMAND_MARK = "SUBCOMMAND"
18+
19+
1220
class Protocols:
1321
CARDANO = "cardano"
1422
SHELLEY = "shelley"

cardano_clusterlib/coverage.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import List
22

3+
from cardano_clusterlib import consts
4+
35

46
def record_cli_coverage(cli_args: List[str], coverage_dict: dict) -> None:
57
"""Record coverage info for CLI commands.
@@ -11,20 +13,26 @@ def record_cli_coverage(cli_args: List[str], coverage_dict: dict) -> None:
1113
parent_dict = coverage_dict
1214
prev_arg = ""
1315
for arg in cli_args:
14-
# if the current argument is a parameter to an option, skip it
16+
# If the current argument is a subcommand marker, record it and skip it
17+
if arg == consts.SUBCOMMAND_MARK:
18+
prev_arg = arg
19+
continue
20+
21+
# If the current argument is a parameter to an option, skip it
1522
if prev_arg.startswith("--") and not arg.startswith("--"):
1623
continue
24+
1725
prev_arg = arg
1826

1927
cur_dict = parent_dict.get(arg)
20-
# initialize record if it doesn't exist yet
28+
# Initialize record if it doesn't exist yet
2129
if not cur_dict:
2230
parent_dict[arg] = {"_count": 0}
2331
cur_dict = parent_dict[arg]
2432

25-
# increment count
33+
# Increment count
2634
cur_dict["_count"] += 1
2735

28-
# set new parent dict
36+
# Set new parent dict
2937
if not arg.startswith("--"):
3038
parent_dict = cur_dict

cardano_clusterlib/query_group.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ class QueryGroup:
3131
def __init__(self, clusterlib_obj: "types.ClusterLib") -> None:
3232
self._clusterlib_obj = clusterlib_obj
3333

34-
def query_cli(self, cli_args: UnpackableSequence) -> str:
34+
def query_cli(self, cli_args: UnpackableSequence, cli_sub_args: UnpackableSequence = ()) -> str:
3535
"""Run the `cardano-cli query` command."""
3636
stdout = self._clusterlib_obj.cli(
3737
[
3838
"query",
3939
*cli_args,
4040
*self._clusterlib_obj.magic_args,
4141
f"--{self._clusterlib_obj.protocol}-mode",
42+
*cli_sub_args,
4243
]
4344
).stdout
4445
stdout_dec = stdout.decode("utf-8") if stdout else ""
@@ -453,7 +454,9 @@ def get_mempool_info(self) -> Dict[str, Any]:
453454
Returns:
454455
dict: A dictionary containing mempool information.
455456
"""
456-
tx_mempool: Dict[str, Any] = json.loads(self.query_cli(["tx-mempool", "info"]))
457+
tx_mempool: Dict[str, Any] = json.loads(
458+
self.query_cli(["tx-mempool"], cli_sub_args=[consts.SUBCOMMAND_MARK, "info"])
459+
)
457460
return tx_mempool
458461

459462
def get_mempool_next_tx(self) -> Dict[str, Any]:
@@ -462,7 +465,9 @@ def get_mempool_next_tx(self) -> Dict[str, Any]:
462465
Returns:
463466
dict: A dictionary containing mempool information.
464467
"""
465-
tx_mempool: Dict[str, Any] = json.loads(self.query_cli(["tx-mempool", "next-tx"]))
468+
tx_mempool: Dict[str, Any] = json.loads(
469+
self.query_cli(["tx-mempool"], cli_sub_args=[consts.SUBCOMMAND_MARK, "next-tx"])
470+
)
466471
return tx_mempool
467472

468473
def get_mempool_tx_exists(self, txid: str) -> Dict[str, Any]:
@@ -474,7 +479,9 @@ def get_mempool_tx_exists(self, txid: str) -> Dict[str, Any]:
474479
Returns:
475480
dict: A dictionary containing mempool information.
476481
"""
477-
tx_mempool: Dict[str, Any] = json.loads(self.query_cli(["tx-mempool", "tx-exists", txid]))
482+
tx_mempool: Dict[str, Any] = json.loads(
483+
self.query_cli(["tx-mempool"], cli_sub_args=[consts.SUBCOMMAND_MARK, "tx-exists", txid])
484+
)
478485
return tx_mempool
479486

480487
def __repr__(self) -> str:

0 commit comments

Comments
 (0)