Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Broken cli_parser Unit Tests #45132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions tests/cli/test_cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
# Only can be `-[a-z]` or `-[A-Z]`
LEGAL_SHORT_OPTION_PATTERN = re.compile("^-[a-zA-z]$")

cli_args = {k: v for k, v in cli_parser.__dict__.items() if k.startswith("ARG_")}
cli_args = {k: v for k, v in cli_config.__dict__.items() if k.startswith("ARG_")}
subcommand = {
var: cli_config.__dict__.get(var)
for var in cli_config.__dict__
if var.isupper() and var.endswith("COMMANDS")
}


class TestCli:
Expand Down Expand Up @@ -82,40 +87,41 @@ def test_subcommand_conflict(self):
"""
Test if each of cli.*_COMMANDS without conflict subcommand
"""
subcommand = {
var: cli_parser.__dict__.get(var)
for var in cli_parser.__dict__
if var.isupper() and var.startswith("COMMANDS")
}
for group_name, sub in subcommand.items():
name = [command.name.lower() for command in sub]
assert len(name) == len(set(name)), f"Command group {group_name} have conflict subcommand"
# no subcommand conflict
subcommand_times = Counter(
[sub.name.lower() for sub in cli_parser.airgraph_commands]
)
conflict_subcommand = [
command for command, count in subcommand_times.items() if count > 1
]
assert conflict_subcommand == [], f"Subcommand {conflict_subcommand} conflicts"
# no conflicts for subcommands of subcommand
for group, commands in subcommand.items():
subcommand_times = Counter(
[sub.name.lower() for sub in commands]
)
conflict_subcommand = [
subcommand for subcommand, count in subcommand_times.items() if count > 1
]
assert conflict_subcommand == [], f"Subcommand {group}'s have conflict subcommands {conflict_subcommand}"

def test_subcommand_arg_name_conflict(self):
"""
Test if each of cli.*_COMMANDS.arg name without conflict
"""
subcommand = {
var: cli_parser.__dict__.get(var)
for var in cli_parser.__dict__
if var.isupper() and var.startswith("COMMANDS")
}
for group, command in subcommand.items():
for com in command:
conflict_arg = [arg for arg, count in Counter(com.args).items() if count > 1]
for group, commands in subcommand.items():
for command in commands:
conflict_arg = [
arg for arg, count in Counter(command.args).items() if count > 1
]
assert (
conflict_arg == []
), f"Command group {group} function {com.name} have conflict args name {conflict_arg}"
), f"Command group {group} function {command.name} have conflict args name {conflict_arg}"

def test_subcommand_arg_flag_conflict(self):
"""
Test if each of cli.*_COMMANDS.arg flags without conflict
"""
subcommand = {
key: val
for key, val in cli_parser.__dict__.items()
if key.isupper() and key.startswith("COMMANDS")
}
for group, command in subcommand.items():
for com in command:
position = [
Expand Down
Loading