Skip to content

Commit 54979eb

Browse files
committed
Brush up test_ert_config
Test code should not be changed by this commit, this is only cleanup like: * String whitespace formatting for ert config files * Usage of pathlib * Expand some test names * Use fixture for changing to tmp_path
1 parent 35415b9 commit 54979eb

File tree

1 file changed

+122
-118
lines changed

1 file changed

+122
-118
lines changed

tests/ert/unit_tests/config/test_ert_config.py

Lines changed: 122 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,26 @@
2929
from .config_dict_generator import config_generators
3030

3131

32-
def test_include_existing_file(tmpdir):
33-
with tmpdir.as_cwd():
34-
config = """
35-
JOBNAME my_name%d
32+
@pytest.mark.usefixtures("use_tmpdir")
33+
def test_config_can_include_existing_file():
34+
config = dedent("""
3635
INCLUDE include_me
3736
NUM_REALIZATIONS 1
38-
"""
39-
rand_seed = 420
40-
include_me_text = f"""
41-
RANDOM_SEED {rand_seed}
42-
"""
37+
""")
38+
rand_seed = 420
39+
include_me_text = f"RANDOM_SEED {rand_seed}\n"
4340

44-
with open("config.ert", mode="w", encoding="utf-8") as fh:
45-
fh.writelines(config)
46-
47-
with open("include_me", mode="w", encoding="utf-8") as fh:
48-
fh.writelines(include_me_text)
41+
Path("config.ert").write_text(config, encoding="utf-8")
42+
Path("include_me").write_text(include_me_text, encoding="utf-8")
43+
ert_config = ErtConfig.from_file("config.ert")
4944

50-
ert_config = ErtConfig.from_file("config.ert")
51-
assert ert_config.random_seed == rand_seed
45+
assert ert_config.random_seed == rand_seed
5246

5347

54-
def test_init(minimum_case):
55-
ert_config = minimum_case
48+
@pytest.mark.usefixtures("use_tmpdir")
49+
def test_minimal_ert_configuration():
50+
Path("minimal_config.ert").write_text("NUM_REALIZATIONS 1", encoding="utf-8")
51+
ert_config = ErtConfig.from_file("minimal_config.ert")
5652

5753
assert ert_config is not None
5854

@@ -64,7 +60,7 @@ def test_init(minimum_case):
6460
assert ert_config.substitutions["<CONFIG_PATH>"] == os.getcwd()
6561

6662

67-
def test_runpath_file(monkeypatch, tmp_path):
63+
def test_runpath_file_is_absolute(monkeypatch, tmp_path):
6864
"""
6965
There was an issue relating to `ErtConfig.runpath_file` returning a
7066
relative path rather than an absolute path. This test simulates the
@@ -80,44 +76,53 @@ def test_runpath_file(monkeypatch, tmp_path):
8076
workdir_path.mkdir(parents=True)
8177
monkeypatch.chdir(workdir_path)
8278

83-
with config_path.open("w") as f:
84-
f.writelines(
85-
[
86-
"DEFINE <FOO> foo\n",
87-
"RUNPATH_FILE ../output/my_custom_runpath_path.<FOO>\n",
88-
# Required for this to be a valid ErtConfig
89-
"NUM_REALIZATIONS 1\n",
90-
]
91-
)
79+
config_path.write_text(
80+
dedent("""
81+
DEFINE <FOO> foo
82+
RUNPATH_FILE ../output/my_custom_runpath_path.<FOO>
83+
-- Required for this to be a valid ErtConfig
84+
NUM_REALIZATIONS 1
85+
"""),
86+
encoding="utf-8",
87+
)
9288

9389
config = ErtConfig.from_file(os.path.relpath(config_path, workdir_path))
9490
assert config.runpath_file == runpath_path
9591

9692

97-
def test_that_job_script_can_be_set_in_site_config(monkeypatch, tmp_path):
93+
@pytest.mark.usefixtures("use_tmpdir")
94+
def test_that_job_script_can_be_set_in_site_config(monkeypatch):
9895
"""
9996
We use the jobscript field to inject a komodo environment onprem.
10097
This overwrites the value by appending to the default siteconfig.
10198
Need to check that the second JOB_SCRIPT is the one that gets used.
10299
"""
103-
test_site_config = tmp_path / "test_site_config.ert"
104-
my_script = (tmp_path / "my_script").resolve()
105-
my_script.write_text("")
100+
test_site_config = Path("test_site_config.ert")
101+
my_script = Path("my_script").resolve()
102+
my_script.write_text("", encoding="utf-8")
106103
st = os.stat(my_script)
107104
os.chmod(my_script, st.st_mode | stat.S_IEXEC)
108105
test_site_config.write_text(
109-
f"JOB_SCRIPT job_dispatch.py\nJOB_SCRIPT {my_script}\nQUEUE_SYSTEM LOCAL\n"
106+
dedent(f"""
107+
JOB_SCRIPT job_dispatch.py
108+
JOB_SCRIPT {my_script}
109+
QUEUE_SYSTEM LOCAL
110+
"""),
111+
encoding="utf-8",
110112
)
111113
monkeypatch.setenv("ERT_SITE_CONFIG", str(test_site_config))
112114

113-
test_user_config = tmp_path / "user_config.ert"
115+
test_user_config = Path("user_config.ert")
114116

115117
test_user_config.write_text(
116-
"JOBNAME Job%d\nRUNPATH /tmp/simulations/realization-<IENS>/iter-<ITER>\n"
117-
"NUM_REALIZATIONS 10\n"
118+
dedent("""
119+
JOBNAME Job%d
120+
NUM_REALIZATIONS 1
121+
"""),
122+
encoding="utf-8",
118123
)
119124

120-
ert_config = ErtConfig.from_file(str(test_user_config))
125+
ert_config = ErtConfig.from_file(test_user_config)
121126

122127
assert Path(ert_config.queue_config.job_script).resolve() == my_script
123128

@@ -132,19 +137,22 @@ def test_that_job_script_can_be_set_in_site_config(monkeypatch, tmp_path):
132137
HookRuntime.POST_UPDATE,
133138
],
134139
)
135-
def test_that_workflow_run_modes_can_be_selected(tmp_path, run_mode):
136-
my_script = (tmp_path / "my_script").resolve()
137-
my_script.write_text("")
140+
@pytest.mark.usefixtures("use_tmpdir")
141+
def test_that_workflow_run_modes_can_be_selected(run_mode):
142+
my_script = Path("my_script").resolve()
143+
my_script.write_text("", encoding="utf-8")
138144
st = os.stat(my_script)
139145
os.chmod(my_script, st.st_mode | stat.S_IEXEC)
140-
test_user_config = tmp_path / "user_config.ert"
146+
test_user_config = Path("user_config.ert")
141147
test_user_config.write_text(
142-
"JOBNAME Job%d\nRUNPATH /tmp/simulations/realization-<IENS>/iter-<ITER>\n"
143-
"NUM_REALIZATIONS 10\n"
144-
f"LOAD_WORKFLOW {my_script} SCRIPT\n"
145-
f"HOOK_WORKFLOW SCRIPT {run_mode.name}\n"
148+
dedent(f"""JOBNAME Job%d
149+
NUM_REALIZATIONS 10
150+
LOAD_WORKFLOW {my_script} SCRIPT
151+
HOOK_WORKFLOW SCRIPT {run_mode.name}
152+
"""),
153+
encoding="utf-8",
146154
)
147-
ert_config = ErtConfig.from_file(str(test_user_config))
155+
ert_config = ErtConfig.from_file(test_user_config)
148156
assert len(list(ert_config.hooked_workflows[run_mode])) == 1
149157

150158

@@ -298,82 +306,80 @@ def test_that_get_plugin_jobs_fetches_exactly_ert_plugins():
298306
"""
299307
)
300308

301-
script_file_path = os.path.join(os.getcwd(), "script")
302-
plugin_file_path = os.path.join(os.getcwd(), "plugin")
303-
with open(script_file_path, mode="w", encoding="utf-8") as fh:
304-
fh.write(script_file_contents)
305-
with open(plugin_file_path, mode="w", encoding="utf-8") as fh:
306-
fh.write(plugin_file_contents)
309+
script_file_path = Path.cwd() / "script"
310+
plugin_file_path = Path.cwd() / "plugin"
311+
Path(script_file_path).write_text(script_file_contents, encoding="utf-8")
312+
Path(plugin_file_path).write_text(plugin_file_contents, encoding="utf-8")
313+
Path("script.py").write_text(
314+
dedent(
315+
"""
316+
from ert import ErtScript
307317
308-
with open("script.py", mode="w", encoding="utf-8") as fh:
309-
fh.write(
310-
dedent(
311-
"""
312-
from ert import ErtScript
313-
class Script(ErtScript):
314-
def run(self, *args):
315-
pass
316-
"""
317-
)
318-
)
319-
with open("plugin.py", mode="w", encoding="utf-8") as fh:
320-
fh.write(
321-
dedent(
322-
"""
323-
from ert.config import ErtPlugin
324-
class Plugin(ErtPlugin):
325-
def run(self, *args):
326-
pass
327-
"""
328-
)
329-
)
330-
with open("config.ert", mode="w", encoding="utf-8") as fh:
331-
fh.write(
332-
dedent(
333-
f"""
334-
NUM_REALIZATIONS 1
335-
LOAD_WORKFLOW_JOB {plugin_file_path} plugin
336-
LOAD_WORKFLOW_JOB {script_file_path} script
337-
"""
338-
)
339-
)
318+
class Script(ErtScript):
319+
def run(self, *args):
320+
pass
321+
"""
322+
),
323+
encoding="utf-8",
324+
)
325+
Path("plugin.py").write_text(
326+
dedent(
327+
"""
328+
from ert.config import ErtPlugin
340329
330+
class Plugin(ErtPlugin):
331+
def run(self, *args):
332+
pass
333+
"""
334+
),
335+
encoding="utf-8",
336+
)
337+
Path("config.ert").write_text(
338+
dedent(
339+
f"""
340+
NUM_REALIZATIONS 1
341+
LOAD_WORKFLOW_JOB {plugin_file_path} plugin
342+
LOAD_WORKFLOW_JOB {script_file_path} script
343+
"""
344+
),
345+
encoding="utf-8",
346+
)
341347
ert_config = ErtConfig.from_file("config.ert")
342348

343349
assert ert_config.workflow_jobs["plugin"].is_plugin()
344350
assert not ert_config.workflow_jobs["script"].is_plugin()
345351

346352

347-
def test_data_file_with_non_utf_8_character_gives_error_message(tmpdir):
348-
with tmpdir.as_cwd():
349-
data_file = "data_file.DATA"
350-
with open("config.ert", mode="w", encoding="utf-8") as fh:
351-
fh.write(
352-
"""
353-
NUM_REALIZATIONS 1
354-
DATA_FILE data_file.DATA
355-
ECLBASE data_file_<ITER>
356-
"""
357-
)
358-
with open(data_file, mode="w", encoding="utf-8") as fh:
359-
fh.write(
360-
dedent(
361-
"""
362-
START
363-
-- DAY MONTH YEAR
364-
1 'JAN' 2017 /
365-
"""
366-
)
367-
)
368-
with open(data_file, "ab") as f:
369-
f.write(b"\xff")
370-
data_file_path = f"{tmpdir}/{data_file}"
371-
with pytest.raises(
372-
ConfigValidationError,
373-
match="Unsupported non UTF-8 character "
374-
f"'ÿ' found in file: {data_file_path!r}",
375-
), pytest.warns(match="Failed to read NUM_CPU"):
376-
ErtConfig.from_file("config.ert")
353+
@pytest.mark.usefixtures("use_tmpdir")
354+
def test_data_file_with_non_utf_8_character_gives_error_message():
355+
data_file = "data_file.DATA"
356+
Path("config.ert").write_text(
357+
dedent("""
358+
NUM_REALIZATIONS 1
359+
DATA_FILE data_file.DATA
360+
ECLBASE data_file_<ITER>
361+
"""),
362+
encoding="utf-8",
363+
)
364+
Path(data_file).write_text(
365+
dedent(
366+
"""
367+
START
368+
-- DAY MONTH YEAR
369+
1 'JAN' 2017 /
370+
"""
371+
),
372+
encoding="utf-8",
373+
)
374+
with open(data_file, "ab") as f:
375+
f.write(b"\xff")
376+
data_file_path = str(Path.cwd() / data_file)
377+
with pytest.raises(
378+
ConfigValidationError,
379+
match="Unsupported non UTF-8 character "
380+
f"'ÿ' found in file: {data_file_path!r}",
381+
), pytest.warns(match="Failed to read NUM_CPU"):
382+
ErtConfig.from_file("config.ert")
377383

378384

379385
def test_that_double_comments_are_handled():
@@ -405,8 +411,7 @@ def test_ert_config_parses_date():
405411
ENSPATH <STORAGE>/ensemble
406412
"""
407413
)
408-
with open(test_config_file_name, "w", encoding="utf-8") as fh:
409-
fh.write(test_config_contents)
414+
Path(test_config_file_name).write_text(test_config_contents, encoding="utf-8")
410415
ert_config = ErtConfig.from_file(test_config_file_name)
411416

412417
date_string = date.today().isoformat()
@@ -422,8 +427,7 @@ def test_that_subst_list_is_given_default_runpath_file():
422427
test_config_file_base = "test"
423428
test_config_file_name = f"{test_config_file_base}.ert"
424429
test_config_contents = "NUM_REALIZATIONS 1"
425-
with open(test_config_file_name, "w", encoding="utf-8") as fh:
426-
fh.write(test_config_contents)
430+
Path(test_config_file_name).write_text(test_config_contents, encoding="utf-8")
427431
ert_config = ErtConfig.from_file(test_config_file_name)
428432
assert ert_config.substitutions["<RUNPATH_FILE>"] == os.path.abspath(
429433
ErtConfig.DEFAULT_RUNPATH_FILE

0 commit comments

Comments
 (0)