From 4538a1bcafd39ad198885e06cc8158395a8a9b84 Mon Sep 17 00:00:00 2001 From: Joey Vagedes Date: Tue, 20 Feb 2024 13:00:41 -0800 Subject: [PATCH] edk2_invocable.py: catch invalid arguments (#753) updates edk2_invocable.py to catch any unexpected argument that start with either a "-" or "/" rather than just "-". This is due to the fact that "/" is a common argument prefix for executables. --- edk2toolext/edk2_invocable.py | 5 +++-- tests.unit/test_edk2_setup.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/edk2toolext/edk2_invocable.py b/edk2toolext/edk2_invocable.py index 4412a7fb..ec648a3e 100644 --- a/edk2toolext/edk2_invocable.py +++ b/edk2toolext/edk2_invocable.py @@ -493,12 +493,13 @@ def ParseCommandLineOptions(self) -> None: if argument.count("=") == 1: tokens = argument.strip().split("=") env.SetValue(tokens[0].strip().upper(), tokens[1].strip(), "From CmdLine") - elif argument.count("=") == 0 and not argument.startswith("-"): + elif argument.count("=") == 0 and not argument.startswith(("-", "/")): env.SetValue(argument.strip().upper(), ''.join(choice(ascii_letters) for _ in range(20)), "Non valued variable set From cmdLine") else: - raise RuntimeError(f"Unknown variable passed in via CLI: {argument}") + print(f"error: unexpected argument: [{argument}]. Pass --help for command information.") + sys.exit(-1) unknown_args.clear() # remove the arguments we've already consumed diff --git a/tests.unit/test_edk2_setup.py b/tests.unit/test_edk2_setup.py index bc29c379..56b0c1ba 100644 --- a/tests.unit/test_edk2_setup.py +++ b/tests.unit/test_edk2_setup.py @@ -5,14 +5,14 @@ # # SPDX-License-Identifier: BSD-2-Clause-Patent ## -import pytest -import git +import logging import pathlib import sys -import logging -from edk2toolext.invocables import edk2_setup +import git +import pytest from edk2toolext.environment import shell_environment +from edk2toolext.invocables import edk2_setup MIN_BUILD_FILE = r""" from edk2toolext.invocables.edk2_setup import SetupSettingsManager, RequiredSubmodule @@ -296,8 +296,8 @@ def test_parse_command_line_options(tree: pathlib.Path): ] try: edk2_setup.main() - except RuntimeError as e: - assert str(e).startswith(f"Unknown variable passed in via CLI: {arg}") + except SystemExit as e: + assert e.code == -1 def test_conf_file(tree: pathlib.Path): @@ -341,8 +341,8 @@ def test_conf_file(tree: pathlib.Path): ] try: edk2_setup.main() - except RuntimeError as e: - assert str(e).startswith(f"Unknown variable passed in via CLI: {arg}") + except SystemExit as e: + assert e.code == -1 @pytest.mark.skipif(sys.platform.startswith("win"), reason="Linux only")