29
29
from .config_dict_generator import config_generators
30
30
31
31
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 ("""
36
35
INCLUDE include_me
37
36
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 "
43
40
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" )
49
44
50
- ert_config = ErtConfig .from_file ("config.ert" )
51
- assert ert_config .random_seed == rand_seed
45
+ assert ert_config .random_seed == rand_seed
52
46
53
47
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" )
56
52
57
53
assert ert_config is not None
58
54
@@ -64,7 +60,7 @@ def test_init(minimum_case):
64
60
assert ert_config .substitutions ["<CONFIG_PATH>" ] == os .getcwd ()
65
61
66
62
67
- def test_runpath_file (monkeypatch , tmp_path ):
63
+ def test_runpath_file_is_absolute (monkeypatch , tmp_path ):
68
64
"""
69
65
There was an issue relating to `ErtConfig.runpath_file` returning a
70
66
relative path rather than an absolute path. This test simulates the
@@ -80,44 +76,53 @@ def test_runpath_file(monkeypatch, tmp_path):
80
76
workdir_path .mkdir (parents = True )
81
77
monkeypatch .chdir (workdir_path )
82
78
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
+ )
92
88
93
89
config = ErtConfig .from_file (os .path .relpath (config_path , workdir_path ))
94
90
assert config .runpath_file == runpath_path
95
91
96
92
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 ):
98
95
"""
99
96
We use the jobscript field to inject a komodo environment onprem.
100
97
This overwrites the value by appending to the default siteconfig.
101
98
Need to check that the second JOB_SCRIPT is the one that gets used.
102
99
"""
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" )
106
103
st = os .stat (my_script )
107
104
os .chmod (my_script , st .st_mode | stat .S_IEXEC )
108
105
test_site_config .write_text (
109
- f"JOB_SCRIPT job_dispatch.py\n JOB_SCRIPT { my_script } \n QUEUE_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" ,
110
112
)
111
113
monkeypatch .setenv ("ERT_SITE_CONFIG" , str (test_site_config ))
112
114
113
- test_user_config = tmp_path / "user_config.ert"
115
+ test_user_config = Path ( "user_config.ert" )
114
116
115
117
test_user_config .write_text (
116
- "JOBNAME Job%d\n RUNPATH /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" ,
118
123
)
119
124
120
- ert_config = ErtConfig .from_file (str ( test_user_config ) )
125
+ ert_config = ErtConfig .from_file (test_user_config )
121
126
122
127
assert Path (ert_config .queue_config .job_script ).resolve () == my_script
123
128
@@ -132,19 +137,22 @@ def test_that_job_script_can_be_set_in_site_config(monkeypatch, tmp_path):
132
137
HookRuntime .POST_UPDATE ,
133
138
],
134
139
)
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" )
138
144
st = os .stat (my_script )
139
145
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" )
141
147
test_user_config .write_text (
142
- "JOBNAME Job%d\n RUNPATH /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" ,
146
154
)
147
- ert_config = ErtConfig .from_file (str ( test_user_config ) )
155
+ ert_config = ErtConfig .from_file (test_user_config )
148
156
assert len (list (ert_config .hooked_workflows [run_mode ])) == 1
149
157
150
158
@@ -298,82 +306,80 @@ def test_that_get_plugin_jobs_fetches_exactly_ert_plugins():
298
306
"""
299
307
)
300
308
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
307
317
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
340
329
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
+ )
341
347
ert_config = ErtConfig .from_file ("config.ert" )
342
348
343
349
assert ert_config .workflow_jobs ["plugin" ].is_plugin ()
344
350
assert not ert_config .workflow_jobs ["script" ].is_plugin ()
345
351
346
352
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" )
377
383
378
384
379
385
def test_that_double_comments_are_handled ():
@@ -405,8 +411,7 @@ def test_ert_config_parses_date():
405
411
ENSPATH <STORAGE>/ensemble
406
412
"""
407
413
)
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" )
410
415
ert_config = ErtConfig .from_file (test_config_file_name )
411
416
412
417
date_string = date .today ().isoformat ()
@@ -422,8 +427,7 @@ def test_that_subst_list_is_given_default_runpath_file():
422
427
test_config_file_base = "test"
423
428
test_config_file_name = f"{ test_config_file_base } .ert"
424
429
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" )
427
431
ert_config = ErtConfig .from_file (test_config_file_name )
428
432
assert ert_config .substitutions ["<RUNPATH_FILE>" ] == os .path .abspath (
429
433
ErtConfig .DEFAULT_RUNPATH_FILE
0 commit comments