Skip to content

Commit

Permalink
Fix ert crashes if invalid range string in REPORT_STEPS config (#9068)
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonSohoel authored Oct 29, 2024
1 parent 35415b9 commit 7190b6c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/ert/config/gen_data_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ def from_config_dict(cls, config_dict: ConfigDict) -> Optional[Self]:
options = option_dict(gen_data, 1)
name = gen_data[0]
res_file = options.get("RESULT_FILE")
report_steps_value = options.get("REPORT_STEPS", "")

if res_file is None:
raise ConfigValidationError.with_context(
f"Missing or unsupported RESULT_FILE for GEN_DATA key {name!r}",
name,
)
try:
_report_steps: Optional[List[int]] = rangestring_to_list(
report_steps_value
)
except ValueError as e:
raise ConfigValidationError.with_context(
f"The REPORT_STEPS setting: {report_steps_value} for {name} is invalid"
' - must be a valid range string: e.g.: "0-1, 4-6, 8"',
gen_data,
) from e

_report_steps: Optional[List[int]] = rangestring_to_list(
options.get("REPORT_STEPS", "")
)
_report_steps = sorted(_report_steps) if _report_steps else None
if os.path.isabs(res_file):
result_file_context = next(
Expand Down
42 changes: 42 additions & 0 deletions tests/ert/unit_tests/config/test_gen_data_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@ def test_malformed_or_missing_gen_data_result_file(result_file, error_message):
GenDataConfig.from_config_dict({"GEN_DATA": [config_line.split()]})


@pytest.mark.parametrize(
"report_step_arg, error_message",
[
pytest.param(
"H",
"must be a valid range string",
id="Invalid REPORT_STEPS argument",
),
pytest.param(
"H,1-3",
"must be a valid range string",
id="Invalid REPORT_STEPS argument",
),
pytest.param(
"invalid-range-argument",
"must be a valid range string",
id="Invalid REPORT_STEPS argument",
),
pytest.param(
"1-2,5-8",
None,
id="This should not fail",
),
pytest.param(
"1",
None,
id="This should not fail",
),
],
)
def test_malformed_report_step_argument(report_step_arg, error_message):
config_line = f"POLY_RES RESULT_FILE:poly_%d.out REPORT_STEPS:{report_step_arg}"
if error_message:
with pytest.raises(
ConfigValidationError,
match=error_message,
):
GenDataConfig.from_config_dict({"GEN_DATA": [config_line.split()]})
else:
GenDataConfig.from_config_dict({"GEN_DATA": [config_line.split()]})


def test_that_invalid_gendata_outfile_error_propagates(tmp_path):
(tmp_path / "poly.out").write_text("""
4.910405046410615,4.910405046410615
Expand Down

0 comments on commit 7190b6c

Please sign in to comment.