Skip to content
Snippets Groups Projects
Commit fc5992e5 authored by Chris Burr's avatar Chris Burr
Browse files

Update tests for lbexec Options type hints

parent d614ecf1
No related branches found
No related tags found
1 merge request!3725Lbexec improvements
......@@ -125,9 +125,8 @@ class FunctionLoader:
sys.exit(1)
OptionsClass = type_hints[options_arg]
if not issubclass(OptionsClass, OptionsBase):
raise TypeError(
f"OptionsClass for {self.spec} should inherit from OptionsBase"
)
log_error(f"OptionsClass for {self.spec} should inherit from OptionsBase")
sys.exit(1)
return OptionsClass
......
......@@ -8,17 +8,20 @@
# granted to it by virtue of its status as an Intergovernmental Organization #
# or submit itself to any jurisdiction. #
###############################################################################
def do_nothing(options):
from GaudiConf.LbExec import Options
def do_nothing(options: Options):
from PyConf.application import configure_input
return configure_input(options)
def bad_function(options):
def bad_function(options: Options):
raise TypeError("Something is wrong")
def execption_with_chain(options):
def execption_with_chain(options: Options):
try:
try:
raise Exception("Exception 1")
......@@ -28,15 +31,15 @@ def execption_with_chain(options):
raise Exception("Exception 3")
def return_none(options):
def return_none(options: Options):
return None
def do_something_2022(options):
def do_something_2022(options: Options):
return do_nothing(None)
def do_something_2023(options, *args):
def do_something_2023(options: Options, *args):
return do_nothing(None)
......@@ -46,3 +49,11 @@ def do_something_2024(arg1, arg2):
def wrong_args():
return do_nothing(None)
def no_type_hint(options):
return do_nothing(options)
def bad_type_hint(options: int):
return do_nothing(options)
......@@ -23,9 +23,8 @@ from GaudiConf.LbExec.tests import examples
LBEXEC_CMD = ["lbexec"]
OPTIONS_FN = str(Path(__file__).parent / "example.yaml")
LBEXEC_EXAMPLE_CMD = LBEXEC_CMD + [
f"{examples.__name__}:do_nothing", OPTIONS_FN
]
FUNCTION_SPEC = f"{examples.__name__}:do_nothing"
LBEXEC_EXAMPLE_CMD = LBEXEC_CMD + [FUNCTION_SPEC, OPTIONS_FN]
@pytest.fixture
......@@ -170,45 +169,56 @@ def test_valid_return_type(capsys, fake_options):
assert captured.out == ""
def test_unknown_app(capsys, monkeypatch):
monkeypatch.delenv("GAUDIAPPNAME")
@pytest.mark.parametrize(
"gaudi_app_name,expected_app_name",
[
("DaVinci", "DaVinci"),
("Moore", "Moore"),
("LHCb", "GaudiConf.LbExec"),
]
)
def test_no_type_hint(capsys, monkeypatch, gaudi_app_name, expected_app_name):
monkeypatch.setenv("GAUDIAPPNAME", gaudi_app_name)
with pytest.raises(SystemExit):
OptionsLoader(OPTIONS_FN)
OptionsLoader(FunctionLoader(f"{examples.__name__}:no_type_hint"), OPTIONS_FN)
captured = capsys.readouterr()
assert captured.out == ""
assert "GAUDIAPPNAME is not set in environment" in captured.err
assert "You probably need to replace" in captured.err
assert "def no_type_hint(options):" in captured.err
assert f"from {expected_app_name} import Options" in captured.err
assert "def no_type_hint(options: Options):" in captured.err
def test_app_unsupported(capsys, monkeypatch):
monkeypatch.setenv("GAUDIAPPNAME", "GaudiConf")
def test_no_type_hint_unknown_app(capsys, monkeypatch):
monkeypatch.delenv("GAUDIAPPNAME")
with pytest.raises(SystemExit):
OptionsLoader(OPTIONS_FN)
OptionsLoader(FunctionLoader(f"{examples.__name__}:no_type_hint"), OPTIONS_FN)
captured = capsys.readouterr()
assert captured.out == ""
assert "GaudiConf doesn't support lbexec" in captured.err
assert "You probably need to replace" in captured.err
assert "def no_type_hint(options):" in captured.err
assert "def no_type_hint(options: Options):" in captured.err
def test_app_override(capsys, monkeypatch):
monkeypatch.setenv("GAUDIAPPNAME", "Gaudi")
def test_bad_type_hint(capsys):
with pytest.raises(SystemExit):
OptionsLoader(OPTIONS_FN)
monkeypatch.setenv("OVERRIDE_LBEXEC_APP", "LHCb")
OptionsLoader(OPTIONS_FN)
OptionsLoader(FunctionLoader(f"{examples.__name__}:bad_type_hint"), OPTIONS_FN)
captured = capsys.readouterr()
assert captured.out == ""
assert "should inherit from OptionsBase" in captured.err
@pytest.mark.parametrize(
"options_arg", [f"{OPTIONS_FN}+i-am-missing.yaml", "i-am-missing.yaml"])
def test_options_file_not_found(capsys, monkeypatch, options_arg):
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
def test_options_file_not_found(capsys, options_arg):
with pytest.raises(SystemExit):
OptionsLoader(options_arg)
OptionsLoader(FunctionLoader(FUNCTION_SPEC), options_arg)
captured = capsys.readouterr()
assert captured.out == ""
assert "i-am-missing.yaml does not exist" in captured.err
def test_options_invalid(capsys, monkeypatch, tmp_path):
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
def test_options_invalid(capsys, tmp_path):
reference_options = yaml.safe_load(Path(OPTIONS_FN).read_text())
options1 = {}
options2 = {}
......@@ -226,7 +236,7 @@ def test_options_invalid(capsys, monkeypatch, tmp_path):
options2_fn.write_text(yaml.safe_dump(options2))
with pytest.raises(SystemExit):
OptionsLoader(f"{options1_fn}+{options2_fn}")
OptionsLoader(FunctionLoader(FUNCTION_SPEC), f"{options1_fn}+{options2_fn}")
captured = capsys.readouterr()
assert captured.out == ""
......@@ -236,7 +246,6 @@ def test_options_invalid(capsys, monkeypatch, tmp_path):
def test_dry_run(capsys, monkeypatch):
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
monkeypatch.setattr(sys, "argv", LBEXEC_EXAMPLE_CMD + ["--dry-run"])
exit_code = parse_args()
......@@ -250,7 +259,6 @@ def test_dry_run_with_output_unknown(capsys, monkeypatch, tmp_path):
output = tmp_path / "name.cpp"
output.unlink(missing_ok=True)
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
monkeypatch.setattr(
sys, "argv",
LBEXEC_EXAMPLE_CMD + ["--dry-run", "--export",
......@@ -270,7 +278,6 @@ def test_dry_run_with_output_unknown(capsys, monkeypatch, tmp_path):
)
def test_dry_run_with_output(capsys, monkeypatch, tmp_path, name):
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
monkeypatch.setattr(sys, "argv",
LBEXEC_EXAMPLE_CMD + ["--dry-run", "--export", name])
......@@ -287,7 +294,6 @@ def test_dry_run_with_output(capsys, monkeypatch, tmp_path, name):
def test_valid(capfd, monkeypatch):
monkeypatch.setenv("GAUDIAPPNAME", "LHCb")
monkeypatch.setattr(sys, "argv", LBEXEC_EXAMPLE_CMD)
exit_code = parse_args()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment