Skip to content

Commit 5ef22c0

Browse files
committed
refactor(cli): move coverage reporting to custom_clusterlib
Replaced `clusterlib` with `custom_clusterlib` for CLI coverage recording. Introduced a new `record_cli_coverage` function in `custom_clusterlib` to handle CLI coverage tracking.
1 parent ad4aa80 commit 5ef22c0

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

cardano_node_tests/cardano_cli_coverage.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import sys
1111
import typing as tp
1212

13-
from cardano_clusterlib import clusterlib
14-
13+
from cardano_node_tests.utils import custom_clusterlib
1514
from cardano_node_tests.utils import helpers
1615

1716
LOGGER = logging.getLogger(__name__)
@@ -179,7 +178,9 @@ def get_log_coverage(log_file: pl.Path) -> dict:
179178
for line in infile:
180179
if not line.startswith("cardano-cli"):
181180
continue
182-
clusterlib.record_cli_coverage(cli_args=line.split(), coverage_dict=coverage_dict)
181+
custom_clusterlib.record_cli_coverage(
182+
cli_args=line.split(), coverage_dict=coverage_dict
183+
)
183184

184185
return coverage_dict
185186

cardano_node_tests/utils/artifacts.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import json
44
import logging
5-
import os
65
import pathlib as pl
76
import shutil
87

@@ -21,7 +20,7 @@
2120
def save_cli_coverage(cluster_obj: clusterlib.ClusterLib, pytest_config: Config) -> pl.Path | None:
2221
"""Save CLI coverage info."""
2322
cli_coverage_dir = pytest_config.getoption(CLI_COVERAGE_ARG)
24-
if not (cli_coverage_dir and cluster_obj.cli_coverage):
23+
if not (cli_coverage_dir and hasattr(cluster_obj, "cli_coverage") and cluster_obj.cli_coverage):
2524
return None
2625

2726
json_file = (
@@ -77,7 +76,7 @@ def save_cluster_artifacts(save_dir: pl.Path, state_dir: pl.Path) -> None:
7776
continue
7877
shutil.copytree(src_dir, destdir / dname, symlinks=True, ignore_dangling_symlinks=True)
7978

80-
if not os.listdir(destdir):
79+
if not destdir.iterdir():
8180
destdir.rmdir()
8281
return
8382

cardano_node_tests/utils/clusterlib_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from cardano_clusterlib import clusterlib
1616
from cardano_clusterlib import txtools as cl_txtools
1717

18+
from cardano_node_tests.utils import custom_clusterlib
1819
from cardano_node_tests.utils import helpers
1920
from cardano_node_tests.utils import submit_utils
2021
from cardano_node_tests.utils.faucet import fund_from_faucet # noqa: F401 # for compatibility
@@ -945,9 +946,10 @@ def _get_ledger_state_cmd(
945946
ledger_state_cmd = " ".join(cardano_cli_args)
946947

947948
# Record cli coverage
948-
clusterlib.record_cli_coverage(
949-
cli_args=cardano_cli_args, coverage_dict=cluster_obj.cli_coverage
950-
)
949+
if hasattr(cluster_obj, "cli_coverage"):
950+
custom_clusterlib.record_cli_coverage(
951+
cli_args=cardano_cli_args, coverage_dict=cluster_obj.cli_coverage
952+
)
951953

952954
return ledger_state_cmd
953955

cardano_node_tests/utils/custom_clusterlib.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,50 @@
33
import logging
44
import os
55
import pathlib as pl
6+
import typing as tp
67

78
from cardano_clusterlib import clusterlib
9+
from cardano_clusterlib import consts
810
from cardano_clusterlib import transaction_group
911

1012
LOGGER = logging.getLogger(__name__)
1113

1214

15+
def record_cli_coverage(cli_args: list[str], coverage_dict: dict) -> None:
16+
"""Record coverage info for CLI commands.
17+
18+
Args:
19+
cli_args: A list of command and it's arguments.
20+
coverage_dict: A dictionary with coverage info.
21+
"""
22+
parent_dict = coverage_dict
23+
prev_arg = ""
24+
for arg in cli_args:
25+
# If the current argument is a subcommand marker, record it and skip it
26+
if arg == consts.SUBCOMMAND_MARK:
27+
prev_arg = arg
28+
continue
29+
30+
# If the current argument is a parameter to an option, skip it
31+
if prev_arg.startswith("--") and not arg.startswith("--"):
32+
continue
33+
34+
prev_arg = arg
35+
36+
cur_dict = parent_dict.get(arg)
37+
# Initialize record if it doesn't exist yet
38+
if not cur_dict:
39+
parent_dict[arg] = {"_count": 0}
40+
cur_dict = parent_dict[arg]
41+
42+
# Increment count
43+
cur_dict["_count"] += 1
44+
45+
# Set new parent dict
46+
if not arg.startswith("--"):
47+
parent_dict = cur_dict
48+
49+
1350
def create_submitted_file(tx_file: clusterlib.FileType) -> None:
1451
"""Create a `.submitted` status file when the Tx was successfully submitted."""
1552
tx_path = pl.Path(tx_file)
@@ -23,13 +60,53 @@ def create_submitted_file(tx_file: clusterlib.FileType) -> None:
2360

2461

2562
class ClusterLib(clusterlib.ClusterLib):
63+
def __init__(
64+
self,
65+
state_dir: clusterlib.FileType,
66+
slots_offset: int | None = None,
67+
socket_path: clusterlib.FileType = "",
68+
command_era: str = consts.CommandEras.LATEST,
69+
):
70+
super().__init__(
71+
state_dir=state_dir,
72+
slots_offset=slots_offset,
73+
socket_path=socket_path,
74+
command_era=command_era,
75+
)
76+
self.cli_coverage: dict[str, tp.Any] = {}
77+
2678
@property
2779
def g_transaction(self) -> transaction_group.TransactionGroup:
2880
"""Transaction group."""
2981
if not self._transaction_group:
3082
self._transaction_group = TransactionGroup(clusterlib_obj=self)
3183
return self._transaction_group
3284

85+
def cli(
86+
self,
87+
cli_args: list[str],
88+
timeout: float | None = None,
89+
add_default_args: bool = True,
90+
) -> clusterlib.CLIOut:
91+
"""Run the `cardano-cli` command.
92+
93+
Args:
94+
cli_args: A list of arguments for cardano-cli.
95+
timeout: A timeout for the command, in seconds (optional).
96+
add_default_args: Whether to add default arguments to the command (optional).
97+
98+
Returns:
99+
clusterlib.CLIOut: A data container containing command stdout and stderr.
100+
"""
101+
cli_args_strs_all = [str(arg) for arg in cli_args]
102+
if add_default_args:
103+
cli_args_strs_all.insert(0, "cardano-cli")
104+
cli_args_strs_all.insert(1, self.command_era)
105+
106+
record_cli_coverage(cli_args=cli_args_strs_all, coverage_dict=self.cli_coverage)
107+
108+
return super().cli(cli_args=cli_args, timeout=timeout, add_default_args=add_default_args)
109+
33110

34111
class TransactionGroup(transaction_group.TransactionGroup):
35112
def submit_tx(

0 commit comments

Comments
 (0)