1
1
import pytest
2
- import filecmp
2
+ from unittest .mock import mock_open , patch
3
+ from pathlib import Path
3
4
4
- from test_utils import MockFile
5
5
from access .parsers .payu_config_yaml import read_payu_config_yaml , write_payu_config_yaml
6
6
7
7
@@ -25,9 +25,8 @@ def simple_payu_config():
25
25
26
26
27
27
@pytest .fixture ()
28
- def simple_payu_config_file (tmp_path ):
29
- file = tmp_path / "simple_payu_config_file.yaml"
30
- payu_file_str = """project: x77
28
+ def simple_payu_config_file ():
29
+ return """project: x77
31
30
ncpus: 48
32
31
jobfs: 10GB
33
32
mem: 192GB
@@ -40,13 +39,11 @@ def simple_payu_config_file(tmp_path):
40
39
- /some/path/to/inputs/1deg/cice
41
40
- /some/path/to/inputs/1deg/share
42
41
"""
43
- return MockFile (file , payu_file_str )
44
42
45
43
46
44
@pytest .fixture ()
47
- def complex_payu_config_file (tmp_path ):
48
- file = tmp_path / "complex_payu_config_file.yaml"
49
- payu_file_str = """# PBS configuration
45
+ def complex_payu_config_file ():
46
+ return """# PBS configuration
50
47
51
48
# If submitting to a different project to your default, uncomment line below
52
49
# and change project code as appropriate; also set shortpath below
@@ -71,13 +68,11 @@ def complex_payu_config_file(tmp_path):
71
68
- /some/path/to/inputs/1deg/share # shared inputs
72
69
73
70
"""
74
- return MockFile (file , payu_file_str )
75
71
76
72
77
73
@pytest .fixture ()
78
- def modified_payu_config_file (tmp_path ):
79
- file = tmp_path / "modified_payu_config_file.yaml"
80
- payu_file_str = """# PBS configuration
74
+ def modified_payu_config_file ():
75
+ return """# PBS configuration
81
76
82
77
# If submitting to a different project to your default, uncomment line below
83
78
# and change project code as appropriate; also set shortpath below
@@ -102,29 +97,33 @@ def modified_payu_config_file(tmp_path):
102
97
- /some/path/to/inputs/1deg/share # shared inputs
103
98
104
99
"""
105
- return MockFile (file , payu_file_str )
106
100
107
101
108
- def test_read_payu_config (tmp_path , simple_payu_config , simple_payu_config_file ):
109
- config_from_file = read_payu_config_yaml (file_name = simple_payu_config_file .file )
102
+ @patch ("pathlib.Path.is_file" , new = lambda file : True )
103
+ def test_read_payu_config (simple_payu_config , simple_payu_config_file ):
104
+ with patch ("io.open" , mock_open (read_data = simple_payu_config_file )) as m :
105
+ config = read_payu_config_yaml (file_name = "simple_payu_config_file" )
110
106
111
- assert config_from_file == simple_payu_config
107
+ assert config == simple_payu_config
112
108
113
109
114
- def test_write_payu_config (tmp_path , simple_payu_config , simple_payu_config_file ):
115
- file = tmp_path / "config_file"
116
- write_payu_config_yaml (simple_payu_config , file )
110
+ def test_write_payu_config (simple_payu_config , simple_payu_config_file ):
111
+ with patch ( "io.open" , mock_open ()) as m :
112
+ write_payu_config_yaml (simple_payu_config , Path ( "config_file" ) )
117
113
118
- assert filecmp . cmp ( file , simple_payu_config_file . file )
114
+ assert simple_payu_config_file == "" . join ( call . args [ 0 ] for call in m (). write . mock_calls )
119
115
120
116
121
- def test_round_trip_payu_config (tmp_path , complex_payu_config_file , modified_payu_config_file ):
122
- config = read_payu_config_yaml (complex_payu_config_file .file )
123
- config ["ncpus" ] = 64
124
- config ["input" ][0 ] = "/some/other/path/to/inputs/1deg/mom"
125
- write_payu_config_yaml (config , tmp_path / "config.yaml" )
117
+ @patch ("pathlib.Path.is_file" , new = lambda file : True )
118
+ def test_round_trip_payu_config (complex_payu_config_file , modified_payu_config_file ):
119
+ with patch ("io.open" , mock_open (read_data = complex_payu_config_file )) as m :
120
+ config = read_payu_config_yaml (file_name = "complex_config_file" )
126
121
127
- assert filecmp .cmp (tmp_path / "config.yaml" , modified_payu_config_file .file )
122
+ config ["ncpus" ] = 64
123
+ config ["input" ][0 ] = "/some/other/path/to/inputs/1deg/mom"
124
+ write_payu_config_yaml (config , Path ("some_other_config_file" ))
125
+
126
+ assert modified_payu_config_file == "" .join (call .args [0 ] for call in m ().write .mock_calls )
128
127
129
128
130
129
def test_read_missing_payu_config ():
0 commit comments