Skip to content

Commit 84a9b6c

Browse files
stsewdagjohnson
authored andcommitted
Tests with real files for requirements_file and conda_file (#4394)
1 parent df715de commit 84a9b6c

File tree

1 file changed

+77
-19
lines changed

1 file changed

+77
-19
lines changed

readthedocs/rtd_tests/tests/test_config_integration.py

+77-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import (
33
absolute_import, division, print_function, unicode_literals)
44

5+
import tempfile
56
from os import path
67

78
import mock
@@ -53,18 +54,30 @@ def inner(path=None, env_config=None):
5354
return inner
5455

5556

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+
5770
class LoadConfigTests(TestCase):
5871

5972
def setUp(self):
6073
self.project = get(
6174
Project,
6275
main_language_project=None,
6376
install_project=False,
64-
requirements_file='__init__.py'
6577
)
6678
self.version = get(Version, project=self.project)
6779

80+
@mock.patch('readthedocs.doc_builder.config.load_config')
6881
def test_python_supported_versions_default_image_1_0(self, load_config):
6982
load_config.side_effect = create_load()
7083
self.project.container_image = 'readthedocs/build:1.0'
@@ -100,6 +113,7 @@ def test_python_supported_versions_default_image_1_0(self, load_config):
100113
])
101114
self.assertEqual(config.python.version, 2)
102115

116+
@mock.patch('readthedocs.doc_builder.config.load_config')
103117
def test_python_supported_versions_image_1_0(self, load_config):
104118
load_config.side_effect = create_load()
105119
self.project.container_image = 'readthedocs/build:1.0'
@@ -108,6 +122,7 @@ def test_python_supported_versions_image_1_0(self, load_config):
108122
self.assertEqual(config.get_valid_python_versions(),
109123
[2, 2.7, 3, 3.4])
110124

125+
@mock.patch('readthedocs.doc_builder.config.load_config')
111126
def test_python_supported_versions_image_2_0(self, load_config):
112127
load_config.side_effect = create_load()
113128
self.project.container_image = 'readthedocs/build:2.0'
@@ -116,6 +131,7 @@ def test_python_supported_versions_image_2_0(self, load_config):
116131
self.assertEqual(config.get_valid_python_versions(),
117132
[2, 2.7, 3, 3.5])
118133

134+
@mock.patch('readthedocs.doc_builder.config.load_config')
119135
def test_python_supported_versions_image_latest(self, load_config):
120136
load_config.side_effect = create_load()
121137
self.project.container_image = 'readthedocs/build:latest'
@@ -124,12 +140,14 @@ def test_python_supported_versions_image_latest(self, load_config):
124140
self.assertEqual(config.get_valid_python_versions(),
125141
[2, 2.7, 3, 3.3, 3.4, 3.5, 3.6])
126142

143+
@mock.patch('readthedocs.doc_builder.config.load_config')
127144
def test_python_default_version(self, load_config):
128145
load_config.side_effect = create_load()
129146
config = load_yaml_config(self.version)
130147
self.assertEqual(config.python.version, 2)
131148
self.assertEqual(config.python_interpreter, 'python2.7')
132149

150+
@mock.patch('readthedocs.doc_builder.config.load_config')
133151
def test_python_set_python_version_on_project(self, load_config):
134152
load_config.side_effect = create_load()
135153
self.project.container_image = 'readthedocs/build:2.0'
@@ -139,6 +157,7 @@ def test_python_set_python_version_on_project(self, load_config):
139157
self.assertEqual(config.python.version, 3)
140158
self.assertEqual(config.python_interpreter, 'python3.5')
141159

160+
@mock.patch('readthedocs.doc_builder.config.load_config')
142161
def test_python_set_python_version_in_config(self, load_config):
143162
load_config.side_effect = create_load({
144163
'python': {'version': 3.5},
@@ -149,6 +168,7 @@ def test_python_set_python_version_in_config(self, load_config):
149168
self.assertEqual(config.python.version, 3.5)
150169
self.assertEqual(config.python_interpreter, 'python3.5')
151170

171+
@mock.patch('readthedocs.doc_builder.config.load_config')
152172
def test_python_invalid_version_in_config(self, load_config):
153173
load_config.side_effect = create_load({
154174
'python': {'version': 2.6}
@@ -158,6 +178,7 @@ def test_python_invalid_version_in_config(self, load_config):
158178
with self.assertRaises(InvalidConfig):
159179
load_yaml_config(self.version)
160180

181+
@mock.patch('readthedocs.doc_builder.config.load_config')
161182
def test_install_project(self, load_config):
162183
load_config.side_effect = create_load()
163184
config = load_yaml_config(self.version)
@@ -172,6 +193,7 @@ def test_install_project(self, load_config):
172193
config = load_yaml_config(self.version)
173194
self.assertEqual(config.python.install_with_setup, True)
174195

196+
@mock.patch('readthedocs.doc_builder.config.load_config')
175197
def test_extra_requirements(self, load_config):
176198
load_config.side_effect = create_load({
177199
'python': {
@@ -203,33 +225,69 @@ def test_extra_requirements(self, load_config):
203225
config = load_yaml_config(self.version)
204226
self.assertEqual(config.python.extra_requirements, [])
205227

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+
)
213244
config = load_yaml_config(self.version)
214245
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)
216247

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
218252
config = load_yaml_config(self.version)
219253
self.assertIsNone(config.conda)
220254

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+
226268
config = load_yaml_config(self.version)
227269
self.assertEqual(config.python.requirements, requirements_file)
228270

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+
)
231289
config = load_yaml_config(self.version)
232-
self.assertEqual(config.python.requirements, '__init__.py')
290+
self.assertEqual(config.python.requirements, requirements_file)
233291

234292

235293
@pytest.mark.django_db

0 commit comments

Comments
 (0)