Skip to content

Commit

Permalink
Raise error on faulty result file and missing report steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngve S. Kristiansen committed Jul 20, 2023
1 parent e4fae78 commit ec8f100
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
27 changes: 9 additions & 18 deletions src/ert/_c_wrappers/enkf/ensemble_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from ert._c_wrappers.enkf.config.surface_config import SurfaceConfig
from ert._c_wrappers.enkf.config_keys import ConfigKeys
from ert.parsing import ConfigValidationError, ConfigWarning, ErrorInfo
from ert.parsing.context_values import ContextList, ContextValue
from ert.storage.field_utils.field_utils import Shape, get_shape

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -242,7 +241,7 @@ def __init__(
self.addNode(self.get_field_node(field, grid_file, dims))

@staticmethod
def gen_data_node(gen_data: ContextList[ContextValue]) -> Optional[GenDataConfig]:
def gen_data_node(gen_data: List[str]) -> Optional[GenDataConfig]:
options = _option_dict(gen_data, 1)
name = gen_data[0]
res_file = options.get(ConfigKeys.RESULT_FILE)
Expand All @@ -255,25 +254,17 @@ def gen_data_node(gen_data: ContextList[ContextValue]) -> Optional[GenDataConfig
report_steps = rangestring_to_list(options.get(ConfigKeys.REPORT_STEPS, ""))

if os.path.isabs(res_file) or "%d" not in res_file:
result_file_context: ContextValue = next(
x for x in gen_data if x.startswith("RESULT_FILE:")
)
raise ConfigValidationError.from_info(
ErrorInfo(
filename=result_file_context.token.filename,
message=f"The RESULT_FILE:{res_file} setting for {name} is "
f"invalid - must have an embedded %d and be a relative path",
).set_context(result_file_context)
raise ConfigValidationError(
f"The RESULT_FILE:{res_file} setting for {name} is "
f"invalid - must have an embedded %d and be a relative path"
)

if not report_steps:
raise ConfigValidationError.from_info(
ErrorInfo(
filename=gen_data.keyword_token.filename,
message="The GEN_DATA keywords must have REPORT_STEPS:xxxx"
" defined. Several report steps separated with ',' "
"and ranges with '-' can be listed",
).set_context_keyword(gen_data.keyword_token)
raise ConfigValidationError(
"The GEN_DATA keywords must have "
"REPORT_STEPS:xxxx defined. Several "
"report steps separated with ',' "
"and ranges with '-' can be listed"
)

gdc = GenDataConfig(name=name, input_file=res_file, report_steps=report_steps)
Expand Down
41 changes: 18 additions & 23 deletions tests/test_config_parsing/test_parser_error_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,43 +1069,38 @@ def test_that_deprecations_are_handled(contents, expected_errors):
)


@pytest.mark.parametrize("use_new_parser", [True, False])
@pytest.mark.usefixtures("use_tmpdir")
def test_that_invalid_ensemble_result_file_errors():
assert_that_config_leads_to_error(
config_file_contents=dedent(
"""
def test_that_invalid_ensemble_result_file_errors(use_new_parser):
write_files(
{
"test.ert": """
NUM_REALIZATIONS 1
GEN_DATA RFT_3-1_R_DATA INPUT_FORMAT:ASCII REPORT_STEPS:100 RESULT_FILE:RFT_3-1_R_<ITER>
"""
),
expected_error=ExpectedErrorInfo(
match="must have an embedded %d",
line=3,
column=61,
end_column=89,
),
}
)

with pytest.raises(ConfigValidationError, match="must have an embedded %d"):
_ = ErtConfig.from_file("test.ert", use_new_parser=use_new_parser)


@pytest.mark.parametrize("use_new_parser", [True, False])
@pytest.mark.usefixtures("use_tmpdir")
def test_that_missing_report_steps_errors():
assert_that_config_leads_to_error(
config_file_contents=dedent(
"""
def test_that_missing_report_steps_errors(use_new_parser):
write_files(
{
"test.ert": """
NUM_REALIZATIONS 1
GEN_DATA RFT_3-1_R_DATA INPUT_FORMAT:ASCII RESULT_FILE:RFT_3-1_R%d
"""
),
expected_error=ExpectedErrorInfo(
match="REPORT_STEPS",
line=3,
column=1,
end_column=9,
),
}
)

with pytest.raises(ConfigValidationError, match="REPORT_STEPS"):
_ = ErtConfig.from_file("test.ert", use_new_parser=use_new_parser)


@pytest.mark.usefixtures("use_tmpdir")
def test_that_valid_gen_data_does_not_error():
Expand Down

0 comments on commit ec8f100

Please sign in to comment.