Skip to content

Commit 4cab25d

Browse files
committed
Raise ConfigValidationError when absolute path is provided as output file
1 parent e556f82 commit 4cab25d

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/ert/config/gen_kw_config.py

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def templates_from_config(
104104
f"can use GEN_KW with just the distribution file instead: GEN_KW {gen_kw_key} {token}",
105105
positional_args[1],
106106
)
107+
if output_file.startswith("/"):
108+
raise ConfigValidationError.with_context(
109+
f"Output file cannot have an absolute path {output_file}",
110+
positional_args[2],
111+
)
107112
return template_file, output_file
108113
return None
109114

tests/ert/unit_tests/storage/test_parameter_sample_types.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ def test_gen_kw_templating(
505505

506506
@pytest.mark.usefixtures("set_site_config")
507507
@pytest.mark.parametrize(
508-
"relpath",
508+
"relpath, err_msg",
509509
[
510-
"somepath/",
510+
("somepath/", ""),
511511
# This test was added to show current behaviour for Ert.
512512
# If absolute paths should be possible to be used like this is up for debate.
513-
"/tmp/somepath/", # ert removes leading '/'
513+
("/tmp/somepath/", "Output file cannot have an absolute path"),
514514
],
515515
)
516-
def test_gen_kw_outfile_will_use_paths(tmpdir, storage, relpath: str):
516+
def test_gen_kw_outfile_will_use_paths(tmpdir, storage, relpath: str, err_msg: str):
517517
with tmpdir.as_cwd():
518518
config = dedent(
519519
f"""
@@ -530,8 +530,15 @@ def test_gen_kw_outfile_will_use_paths(tmpdir, storage, relpath: str):
530530
with open("prior.txt", mode="w", encoding="utf-8") as fh:
531531
fh.writelines("MY_KEYWORD NORMAL 0 1")
532532
relpath = relpath.removeprefix("/")
533-
create_runpath(storage, "config.ert")
534-
assert os.path.exists(f"simulations/realization-0/iter-0/{relpath}kw.txt")
533+
if err_msg:
534+
with pytest.raises(
535+
ConfigValidationError,
536+
match=err_msg,
537+
):
538+
create_runpath(storage, "config.ert")
539+
else:
540+
create_runpath(storage, "config.ert")
541+
assert os.path.exists(f"simulations/realization-0/iter-0/{relpath}kw.txt")
535542

536543

537544
@pytest.mark.usefixtures("set_site_config")

0 commit comments

Comments
 (0)