2
2
from __future__ import (
3
3
absolute_import , division , print_function , unicode_literals )
4
4
5
+ import tempfile
5
6
from os import path
6
7
7
8
import mock
@@ -53,18 +54,30 @@ def inner(path=None, env_config=None):
53
54
return inner
54
55
55
56
56
- @mock .patch ('readthedocs.doc_builder.config.load_config' )
57
+ def create_config_file (config , file_name = 'readthedocs.yml' , base_path = None ):
58
+ """
59
+ Creates a readthedocs configuration file with name
60
+ ``file_name`` in ``base_path``. If ``base_path`` is not given
61
+ a temporal directory is created.
62
+ """
63
+ if not base_path :
64
+ base_path = tempfile .mkdtemp ()
65
+ full_path = path .join (base_path , file_name )
66
+ yaml .safe_dump (config , open (full_path , 'w' ))
67
+ return full_path
68
+
69
+
57
70
class LoadConfigTests (TestCase ):
58
71
59
72
def setUp (self ):
60
73
self .project = get (
61
74
Project ,
62
75
main_language_project = None ,
63
76
install_project = False ,
64
- requirements_file = '__init__.py'
65
77
)
66
78
self .version = get (Version , project = self .project )
67
79
80
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
68
81
def test_python_supported_versions_default_image_1_0 (self , load_config ):
69
82
load_config .side_effect = create_load ()
70
83
self .project .container_image = 'readthedocs/build:1.0'
@@ -100,6 +113,7 @@ def test_python_supported_versions_default_image_1_0(self, load_config):
100
113
])
101
114
self .assertEqual (config .python .version , 2 )
102
115
116
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
103
117
def test_python_supported_versions_image_1_0 (self , load_config ):
104
118
load_config .side_effect = create_load ()
105
119
self .project .container_image = 'readthedocs/build:1.0'
@@ -108,6 +122,7 @@ def test_python_supported_versions_image_1_0(self, load_config):
108
122
self .assertEqual (config .get_valid_python_versions (),
109
123
[2 , 2.7 , 3 , 3.4 ])
110
124
125
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
111
126
def test_python_supported_versions_image_2_0 (self , load_config ):
112
127
load_config .side_effect = create_load ()
113
128
self .project .container_image = 'readthedocs/build:2.0'
@@ -116,6 +131,7 @@ def test_python_supported_versions_image_2_0(self, load_config):
116
131
self .assertEqual (config .get_valid_python_versions (),
117
132
[2 , 2.7 , 3 , 3.5 ])
118
133
134
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
119
135
def test_python_supported_versions_image_latest (self , load_config ):
120
136
load_config .side_effect = create_load ()
121
137
self .project .container_image = 'readthedocs/build:latest'
@@ -124,12 +140,14 @@ def test_python_supported_versions_image_latest(self, load_config):
124
140
self .assertEqual (config .get_valid_python_versions (),
125
141
[2 , 2.7 , 3 , 3.3 , 3.4 , 3.5 , 3.6 ])
126
142
143
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
127
144
def test_python_default_version (self , load_config ):
128
145
load_config .side_effect = create_load ()
129
146
config = load_yaml_config (self .version )
130
147
self .assertEqual (config .python .version , 2 )
131
148
self .assertEqual (config .python_interpreter , 'python2.7' )
132
149
150
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
133
151
def test_python_set_python_version_on_project (self , load_config ):
134
152
load_config .side_effect = create_load ()
135
153
self .project .container_image = 'readthedocs/build:2.0'
@@ -139,6 +157,7 @@ def test_python_set_python_version_on_project(self, load_config):
139
157
self .assertEqual (config .python .version , 3 )
140
158
self .assertEqual (config .python_interpreter , 'python3.5' )
141
159
160
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
142
161
def test_python_set_python_version_in_config (self , load_config ):
143
162
load_config .side_effect = create_load ({
144
163
'python' : {'version' : 3.5 },
@@ -149,6 +168,7 @@ def test_python_set_python_version_in_config(self, load_config):
149
168
self .assertEqual (config .python .version , 3.5 )
150
169
self .assertEqual (config .python_interpreter , 'python3.5' )
151
170
171
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
152
172
def test_python_invalid_version_in_config (self , load_config ):
153
173
load_config .side_effect = create_load ({
154
174
'python' : {'version' : 2.6 }
@@ -158,6 +178,7 @@ def test_python_invalid_version_in_config(self, load_config):
158
178
with self .assertRaises (InvalidConfig ):
159
179
load_yaml_config (self .version )
160
180
181
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
161
182
def test_install_project (self , load_config ):
162
183
load_config .side_effect = create_load ()
163
184
config = load_yaml_config (self .version )
@@ -172,6 +193,7 @@ def test_install_project(self, load_config):
172
193
config = load_yaml_config (self .version )
173
194
self .assertEqual (config .python .install_with_setup , True )
174
195
196
+ @mock .patch ('readthedocs.doc_builder.config.load_config' )
175
197
def test_extra_requirements (self , load_config ):
176
198
load_config .side_effect = create_load ({
177
199
'python' : {
@@ -203,33 +225,69 @@ def test_extra_requirements(self, load_config):
203
225
config = load_yaml_config (self .version )
204
226
self .assertEqual (config .python .extra_requirements , [])
205
227
206
- def test_conda (self , load_config ):
207
- to_find = '__init__.py'
208
- load_config .side_effect = create_load ({
209
- 'conda' : {
210
- 'file' : to_find
211
- }
212
- })
228
+ @mock .patch ('readthedocs.projects.models.Project.checkout_path' )
229
+ def test_conda_with_cofig (self , checkout_path ):
230
+ base_path = tempfile .mkdtemp ()
231
+ checkout_path .return_value = base_path
232
+ conda_file = 'environmemt.yml'
233
+ full_conda_file = path .join (base_path , conda_file )
234
+ with open (full_conda_file , 'w' ) as f :
235
+ f .write ('conda' )
236
+ create_config_file (
237
+ {
238
+ 'conda' : {
239
+ 'file' : conda_file ,
240
+ }
241
+ },
242
+ base_path = base_path ,
243
+ )
213
244
config = load_yaml_config (self .version )
214
245
self .assertTrue (config .conda is not None )
215
- self .assertTrue (config .conda .environment [ - len ( to_find ):] == to_find )
246
+ self .assertEqual (config .conda .environment , full_conda_file )
216
247
217
- load_config .side_effect = create_load ()
248
+ @mock .patch ('readthedocs.projects.models.Project.checkout_path' )
249
+ def test_conda_without_cofig (self , checkout_path ):
250
+ base_path = tempfile .mkdtemp ()
251
+ checkout_path .return_value = base_path
218
252
config = load_yaml_config (self .version )
219
253
self .assertIsNone (config .conda )
220
254
221
- def test_requirements_file (self , load_config ):
222
- requirements_file = '__init__.py'
223
- load_config .side_effect = create_load ({
224
- 'requirements_file' : requirements_file
225
- })
255
+ @mock .patch ('readthedocs.projects.models.Project.checkout_path' )
256
+ def test_requirements_file_from_project_setting (self , checkout_path ):
257
+ base_path = tempfile .mkdtemp ()
258
+ checkout_path .return_value = base_path
259
+
260
+ requirements_file = 'requirements.txt'
261
+ self .project .requirements_file = requirements_file
262
+ self .project .save ()
263
+
264
+ full_requirements_file = path .join (base_path , requirements_file )
265
+ with open (full_requirements_file , 'w' ) as f :
266
+ f .write ('pip' )
267
+
226
268
config = load_yaml_config (self .version )
227
269
self .assertEqual (config .python .requirements , requirements_file )
228
270
229
- # Respects the requirements file from the project settings
230
- load_config .side_effect = create_load ()
271
+ @mock .patch ('readthedocs.projects.models.Project.checkout_path' )
272
+ def test_requirements_file_from_yml (self , checkout_path ):
273
+ base_path = tempfile .mkdtemp ()
274
+ checkout_path .return_value = base_path
275
+
276
+ self .project .requirements_file = 'no-existent-file.txt'
277
+ self .project .save ()
278
+
279
+ requirements_file = 'requirements.txt'
280
+ full_requirements_file = path .join (base_path , requirements_file )
281
+ with open (full_requirements_file , 'w' ) as f :
282
+ f .write ('pip' )
283
+ create_config_file (
284
+ {
285
+ 'requirements_file' : requirements_file ,
286
+ },
287
+ base_path = base_path ,
288
+ )
231
289
config = load_yaml_config (self .version )
232
- self .assertEqual (config .python .requirements , '__init__.py' )
290
+ self .assertEqual (config .python .requirements , requirements_file )
233
291
234
292
235
293
@pytest .mark .django_db
0 commit comments