@@ -103,20 +103,21 @@ def test_dist_info_file(self):
103
103
os .path .join (distribution .dist_dir , "dist_info.json" )
104
104
)
105
105
106
- @mock .patch ("pythonforandroid.distribution.open" , create = True )
107
- def test_get_dist_info (self , mock_open ):
106
+ def test_dist_info (self ):
108
107
"""Test that method
109
- :meth:`~pythonforandroid.distribution.Distribution.get_dist_info `
108
+ :meth:`~pythonforandroid.distribution.Distribution.dist_info `
110
109
calls the proper methods and returns the proper value."""
111
110
self .setUp_distribution_with_bootstrap (
112
111
Bootstrap ().get_bootstrap ("sdl2" , self .ctx )
113
112
)
114
- mock_open .side_effect = [
115
- mock .mock_open (read_data = json .dumps (dist_info_data )).return_value
116
- ]
117
- dist_info = self .ctx .bootstrap .distribution .get_dist_info ("/fake_dir" )
118
- mock_open .assert_called_once_with ("/fake_dir/dist_info.json" , "r" )
119
- self .assertIsInstance (dist_info , dict )
113
+
114
+ mock_open = mock .mock_open (read_data = json .dumps (dist_info_data ))
115
+ with mock .patch ("pythonforandroid.distribution.open" , mock_open ):
116
+ dist_info = self .ctx .bootstrap .distribution .dist_info
117
+ mock_open .assert_called_once_with (
118
+ self .ctx .bootstrap .distribution .dist_info_file , "r" ,
119
+ )
120
+ self .assertEqual (dist_info , dist_info_data )
120
121
121
122
@mock .patch ("pythonforandroid.distribution.json.dump" )
122
123
@mock .patch ("pythonforandroid.distribution.open" , create = True )
@@ -132,24 +133,22 @@ def test_update_dist_info(self, mock_open, mock_json):
132
133
expected_json_file = mock .mock_open (
133
134
read_data = json .dumps (new_info_data )
134
135
).return_value
135
- mock_open .side_effect = [
136
- # first call to open, when we read the file
137
- mock .mock_open (read_data = json .dumps (dist_info_data )).return_value ,
138
- # second call to open, when we update the file
139
- expected_json_file ,
140
- ]
136
+ mock_open .side_effect = [expected_json_file ]
137
+
138
+ with mock .patch (
139
+ 'pythonforandroid.distribution.Distribution.dist_info' ,
140
+ new_callable = mock .PropertyMock ,
141
+ return_value = dist_info_data ,
142
+ ) as mock_dist_info :
143
+ self .ctx .bootstrap .distribution .update_dist_info (
144
+ "android_api" , new_info_data ['android_api' ]
145
+ )
146
+ # We expect two calls to property `dist_info` (read and write)
147
+ self .assertEqual (len (mock_dist_info .call_args ), 2 )
141
148
142
- self .ctx .bootstrap .distribution .update_dist_info (
143
- "android_api" , new_info_data ['android_api' ]
144
- )
145
- # Note: call_args only contemplates the last mocked call, see also:
146
- # https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args # noqa
147
- self .assertTrue (mock_open .call_args [0 ][0 ].endswith (
148
- 'dists/test_prj__armeabi-v7a/dist_info.json' )
149
- )
150
- self .assertEqual (mock_open .call_args [0 ][1 ], 'w' )
151
- mock_json .assert_called_once_with (
152
- new_info_data , expected_json_file , indent = 4 , sort_keys = True ,
149
+ mock_open .assert_called_once_with (
150
+ self .ctx .bootstrap .distribution .dist_info_file ,
151
+ "w" ,
153
152
)
154
153
155
154
@mock .patch ("pythonforandroid.distribution.open" , create = True )
@@ -197,16 +196,22 @@ def test_update_dist_android_api(self, mock_open):
197
196
]
198
197
199
198
dist = self .ctx .bootstrap .distribution
200
- dist .update_dist_android_api (new_android_api )
199
+ with mock .patch (
200
+ 'pythonforandroid.distribution.Distribution.dist_info' ,
201
+ new_callable = mock .PropertyMock ,
202
+ return_value = dist_info_data ,
203
+ ) as mock_dist_info :
204
+ dist .update_dist_android_api (new_android_api )
205
+ # We expect two calls to property `dist_info` (read and write)
206
+ self .assertEqual (len (mock_dist_info .call_args ), 2 )
207
+
201
208
mock_open .assert_has_calls (
202
209
[
203
210
mock .call (os .path .join (dist .dist_dir , "build.gradle" ), "r" ),
204
211
mock .call (os .path .join (dist .dist_dir , "build.gradle" ), "w" ),
205
212
mock .call (
206
213
os .path .join (dist .dist_dir , "project.properties" ), "w"
207
214
),
208
- mock .call (os .path .join (dist .dist_dir , "dist_info.json" ), "r" ),
209
- mock .call (os .path .join (dist .dist_dir , "dist_info.json" ), "w" ),
210
215
]
211
216
)
212
217
0 commit comments