Skip to content

Commit

Permalink
Raise ConfigValidationError on short parameter file lines
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Jan 24, 2025
1 parent 1e6ced0 commit cef932f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,34 @@ def from_config_list(cls, gen_kw: list[str]) -> Self:
"Loading GEN_KW from files requires %d in file format", gen_kw
)
)

if errors:
raise ConfigValidationError.from_collected(errors)

transform_function_definitions: list[TransformFunctionDefinition] = []
with open(parameter_file, encoding="utf-8") as file:
for item in file:
for line_number, item in enumerate(file):
item = item.split("--")[0] # remove comments
if item.strip(): # only lines with content
items = item.split()
transform_function_definitions.append(
TransformFunctionDefinition(
name=items[0],
param_name=items[1],
values=items[2:],
if len(items) < 2:
errors.append(
ConfigValidationError.with_context(
f"Too few values on line {line_number} in parameter file {parameter_file}",
gen_kw,
)
)
)
else:
transform_function_definitions.append(
TransformFunctionDefinition(
name=items[0],
param_name=items[1],
values=items[2:],
)
)

if errors:
raise ConfigValidationError.from_collected(errors)

if gen_kw_key == "PRED" and update_parameter:
ConfigWarning.warn(
Expand Down
14 changes: 14 additions & 0 deletions tests/ert/unit_tests/config/test_gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ def test_gen_kw_config_duplicate_keys_raises():
)


def test_short_definition_raises_config_error(tmp_path):
parameter_file = tmp_path / "parameter.txt"
parameter_file.write_text("incorrect", encoding="utf-8")

with pytest.raises(ConfigValidationError, match="Too few values"):
GenKwConfig.from_config_list(
[
"GEN",
str(parameter_file),
"INIT_FILES:%dgen_init.txt",
]
)


def test_gen_kw_config_get_priors():
conf = GenKwConfig(
name="KW_NAME",
Expand Down

0 comments on commit cef932f

Please sign in to comment.