Skip to content

Commit

Permalink
Fix bug where template file was interpreted as parameters file
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Aug 20, 2024
1 parent bc8c383 commit 4749629
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
6 changes: 0 additions & 6 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,6 @@ def _validate_dict(
) -> List[Union[ErrorInfo, ConfigValidationError]]:
errors = []

gen_kws = config_dict.get("GEN_KW", [])
for gen_kw in gen_kws:
file_path = Path(gen_kw[1])
if file_path.stat().st_size == 0:
raise ConfigValidationError(f"No parameters specified in {file_path}")

if ConfigKeys.SUMMARY in config_dict and ConfigKeys.ECLBASE not in config_dict:
errors.append(
ErrorInfo(
Expand Down
12 changes: 10 additions & 2 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ def from_config_list(cls, gen_kw: List[str]) -> Self:

if len(positional_args) == 2:
parameter_file = _get_abs_path(positional_args[1])
parameter_file_context = positional_args[1]
template_file = None
output_file = None
elif len(positional_args) == 4:
output_file = positional_args[2]
parameter_file = _get_abs_path(positional_args[3])

parameter_file_context = positional_args[3]
template_file = _get_abs_path(positional_args[1])
if not os.path.isfile(template_file):
errors.append(
Expand All @@ -125,7 +126,14 @@ def from_config_list(cls, gen_kw: List[str]) -> Self:
if not os.path.isfile(parameter_file):
errors.append(
ConfigValidationError.with_context(
f"No such parameter file: {parameter_file}", positional_args[3]
f"No such parameter file: {parameter_file}", parameter_file_context
)
)
elif Path(parameter_file).stat().st_size == 0:
errors.append(
ConfigValidationError.with_context(
f"No parameters specified in {parameter_file}",
parameter_file_context,
)
)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/config/test_ensemble_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_that_files_for_refcase_exists(existing_suffix, expected_suffix):
@pytest.mark.usefixtures("use_tmpdir")
def test_ensemble_config_duplicate_node_names():
duplicate_name = "Test_name"
Path("MULTFLT.TXT").write_text("", encoding="utf-8")
Path("MULTFLT.TXT").write_text("a UNIFORM 0 1", encoding="utf-8")
Path("FAULT_TEMPLATE").write_text("", encoding="utf-8")
config_dict = {
ConfigKeys.GEN_DATA: [
Expand Down
13 changes: 10 additions & 3 deletions tests/unit_tests/config/test_ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,12 +1618,15 @@ def test_using_relative_path_to_eclbase_sets_jobname_to_basename(tmp_path):
)


def test_that_empty_params_file_gives_reasonable_error(tmpdir):
@pytest.mark.parametrize(
"param_config", ["coeffs_priors", "template.txt output.txt coeffs_priors"]
)
def test_that_empty_params_file_gives_reasonable_error(tmpdir, param_config):
with tmpdir.as_cwd():
config = """
NUM_REALIZATIONS 1
GEN_KW COEFFS coeffs_priors
"""
GEN_KW COEFFS """
config += param_config

with open("config.ert", mode="w", encoding="utf-8") as fh:
fh.writelines(config)
Expand All @@ -1632,5 +1635,9 @@ def test_that_empty_params_file_gives_reasonable_error(tmpdir):
with open("coeffs_priors", mode="w", encoding="utf-8") as fh:
pass

# Create an empty file named 'template.txt'
with open("template.txt", mode="w", encoding="utf-8") as fh:
pass

with pytest.raises(ConfigValidationError, match="No parameters specified in"):
ErtConfig.from_file("config.ert")

0 comments on commit 4749629

Please sign in to comment.