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

Fixing check_arguments for consistency; added UT #168

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/fprime_gds/common/communication/adapters/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ def register_communication_plugin(cls):
return cls

@classmethod
def check_arguments(cls, args):
def check_arguments(cls, address, port, server=True):
"""
Code that should check arguments of this adapter. If there is a problem with this code, then a "ValueError"
should be raised describing the problem with these arguments.

:param args: arguments as dictionary
"""
check_port(args["address"], args["port"])
check_port(address, port)


class IpHandler(abc.ABC):
Expand Down
10 changes: 5 additions & 5 deletions src/fprime_gds/common/communication/adapters/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,24 @@ def register_communication_plugin(cls):
return cls

@classmethod
def check_arguments(cls, args):
def check_arguments(cls, device, baud):
"""
Code that should check arguments of this adapter. If there is a problem with this code, then a "ValueError"
should be raised describing the problem with these arguments.

:param args: arguments as dictionary
"""
ports = map(lambda info: info.device, list_ports.comports(include_links=True))
if args["device"] not in ports:
msg = f"Serial port '{args['device']}' not valid. Available ports: {ports}"
if device not in ports:
msg = f"Serial port '{device}' not valid. Available ports: {ports}"
raise ValueError(
msg
)
# Note: baud rate may not *always* work. These are a superset
try:
baud = int(args["baud"])
baud = int(baud)
except ValueError:
msg = f"Serial baud rate '{args['baud']}' not integer. Use one of: {SerialAdapter.BAUDS}"
msg = f"Serial baud rate '{baud}' not integer. Use one of: {SerialAdapter.BAUDS}"
raise ValueError(
msg
)
Expand Down
2 changes: 1 addition & 1 deletion src/fprime_gds/executables/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def extract_plugin_arguments(args, plugin) -> Dict[str, Any]:
}
# Check arguments or yield a Value error
if hasattr(plugin, "check_arguments"):
plugin.check_arguments(filled_arguments)
plugin.check_arguments(**filled_arguments)
return filled_arguments


Expand Down
15 changes: 15 additions & 0 deletions test/fprime_gds/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def get_arguments(cls):
},
}

@classmethod
def check_arguments(cls, my_fancy_arg, fancy_2):
""" Check arguments to raise ValueError """
if fancy_2 < 0:
raise ValueError("Must be positive")


class StartFunction(GdsFunction):
""" A plugin implementation that starts a function
Expand Down Expand Up @@ -275,6 +281,15 @@ def test_plugin_arguments(plugins):
assert args.framing_selection_instance.fancy_2 == int(a_number), "Integer argument did not process"


def test_plugin_check_arguments(plugins):
""" Tests that arguments are validated in plugins """
a_string = "a_string"
a_number = "-20"
to_parse = ["--framing", "good-with-args", "--my-fancy-arg", a_string, "--my-fancy-arg-with-dest", a_number]
with pytest.raises(SystemExit):
args, _ = ParserBase.parse_args([PluginArgumentParser,], arguments=to_parse)


@pytest.mark.parametrize("start_up", [(f"{__name__}:StartFunction", [])], indirect=True)
def test_start_function(start_up):
""" Test start-up functions """
Expand Down
Loading