Skip to content

Commit ccb24e9

Browse files
authored
Build: pass environment explicitly (#10388)
With #10378 we now need to always pass an environment, we can't just create a default one.
1 parent b2deead commit ccb24e9

File tree

5 files changed

+50
-39
lines changed

5 files changed

+50
-39
lines changed

readthedocs/projects/models.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,8 @@ def has_htmlzip(self, version_slug=LATEST, version_type=None):
947947
version_type=version_type
948948
)
949949

950-
# NOTE: if `environment=None` everything fails, because it cannot execute
951-
# any command.
952950
def vcs_repo(
953-
self, version=LATEST, environment=None,
954-
verbose_name=None, version_type=None
951+
self, environment, version=LATEST, verbose_name=None, version_type=None
955952
):
956953
"""
957954
Return a Backend object for this project able to handle VCS commands.

readthedocs/rtd_tests/tests/test_backend.py

+37-24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from readthedocs.builds.constants import EXTERNAL
1313
from readthedocs.builds.models import Version
1414
from readthedocs.config import ALL
15+
from readthedocs.doc_builder.environments import LocalBuildEnvironment
1516
from readthedocs.projects.exceptions import RepositoryError
1617
from readthedocs.projects.models import Feature, Project
1718
from readthedocs.rtd_tests.utils import (
@@ -45,6 +46,7 @@ def setUp(self):
4546
# These are the default values from v1
4647
self.dummy_conf.submodules.include = ALL
4748
self.dummy_conf.submodules.exclude = []
49+
self.build_environment = LocalBuildEnvironment()
4850

4951
def test_git_lsremote(self):
5052
repo_path = self.project.repo
@@ -68,7 +70,7 @@ def test_git_lsremote(self):
6870
create_git_tag(repo_path, 'v02', annotated=True)
6971
create_git_tag(repo_path, 'release-ünîø∂é')
7072

71-
repo = self.project.vcs_repo()
73+
repo = self.project.vcs_repo(environment=self.build_environment)
7274
# create the working dir if it not exists. It's required to ``cwd`` to
7375
# execute the command
7476
repo.check_working_dir()
@@ -91,7 +93,7 @@ def test_git_lsremote_tags_only(self):
9193
create_git_tag(repo_path, "v02", annotated=True)
9294
create_git_tag(repo_path, "release-ünîø∂é")
9395

94-
repo = self.project.vcs_repo()
96+
repo = self.project.vcs_repo(environment=self.build_environment)
9597
# create the working dir if it not exists. It's required to ``cwd`` to
9698
# execute the command
9799
repo.check_working_dir()
@@ -123,7 +125,7 @@ def test_git_lsremote_branches_only(self):
123125
for branch in branches:
124126
create_git_branch(repo_path, branch)
125127

126-
repo = self.project.vcs_repo()
128+
repo = self.project.vcs_repo(environment=self.build_environment)
127129
# create the working dir if it not exists. It's required to ``cwd`` to
128130
# execute the command
129131
repo.check_working_dir()
@@ -160,7 +162,7 @@ def test_git_branches(self, checkout_path):
160162
os.mkdir(local_repo)
161163
checkout_path.return_value = local_repo
162164

163-
repo = self.project.vcs_repo()
165+
repo = self.project.vcs_repo(environment=self.build_environment)
164166
repo.clone()
165167

166168
self.assertEqual(
@@ -188,7 +190,7 @@ def test_git_branches_unicode(self, checkout_path):
188190
os.mkdir(local_repo)
189191
checkout_path.return_value = local_repo
190192

191-
repo = self.project.vcs_repo()
193+
repo = self.project.vcs_repo(environment=self.build_environment)
192194
repo.clone()
193195

194196
self.assertEqual(
@@ -197,7 +199,7 @@ def test_git_branches_unicode(self, checkout_path):
197199
)
198200

199201
def test_git_update_and_checkout(self):
200-
repo = self.project.vcs_repo()
202+
repo = self.project.vcs_repo(environment=self.build_environment)
201203
code, _, _ = repo.update()
202204
self.assertEqual(code, 0)
203205

@@ -217,7 +219,8 @@ def test_git_update_with_external_version(self, fetch):
217219
)
218220
repo = self.project.vcs_repo(
219221
verbose_name=version.verbose_name,
220-
version_type=version.type
222+
version_type=version.type,
223+
environment=self.build_environment,
221224
)
222225
repo.update()
223226
fetch.assert_called_once()
@@ -231,14 +234,15 @@ def test_git_fetch_with_external_version(self):
231234
)
232235
repo = self.project.vcs_repo(
233236
verbose_name=version.verbose_name,
234-
version_type=version.type
237+
version_type=version.type,
238+
environment=self.build_environment,
235239
)
236240
repo.update()
237241
code, _, _ = repo.fetch()
238242
self.assertEqual(code, 0)
239243

240244
def test_git_checkout_invalid_revision(self):
241-
repo = self.project.vcs_repo()
245+
repo = self.project.vcs_repo(environment=self.build_environment)
242246
repo.update()
243247
version = 'invalid-revision'
244248
with self.assertRaises(RepositoryError) as e:
@@ -253,7 +257,7 @@ def test_git_tags(self):
253257
create_git_tag(repo_path, 'v01')
254258
create_git_tag(repo_path, 'v02', annotated=True)
255259
create_git_tag(repo_path, 'release-ünîø∂é')
256-
repo = self.project.vcs_repo()
260+
repo = self.project.vcs_repo(environment=self.build_environment)
257261
# We aren't cloning the repo,
258262
# so we need to hack the repo path
259263
repo.working_dir = repo_path
@@ -264,7 +268,7 @@ def test_git_tags(self):
264268
)
265269

266270
def test_check_for_submodules(self):
267-
repo = self.project.vcs_repo()
271+
repo = self.project.vcs_repo(environment=self.build_environment)
268272

269273
repo.update()
270274
self.assertFalse(repo.are_submodules_available(self.dummy_conf))
@@ -274,13 +278,13 @@ def test_check_for_submodules(self):
274278
self.assertTrue(repo.are_submodules_available(self.dummy_conf))
275279

276280
def test_skip_submodule_checkout(self):
277-
repo = self.project.vcs_repo()
281+
repo = self.project.vcs_repo(environment=self.build_environment)
278282
repo.update()
279283
repo.checkout('submodule')
280284
self.assertTrue(repo.are_submodules_available(self.dummy_conf))
281285

282286
def test_use_shallow_clone(self):
283-
repo = self.project.vcs_repo()
287+
repo = self.project.vcs_repo(environment=self.build_environment)
284288
repo.update()
285289
repo.checkout('submodule')
286290
self.assertTrue(repo.use_shallow_clone())
@@ -293,14 +297,14 @@ def test_use_shallow_clone(self):
293297
self.assertFalse(repo.use_shallow_clone())
294298

295299
def test_check_submodule_urls(self):
296-
repo = self.project.vcs_repo()
300+
repo = self.project.vcs_repo(environment=self.build_environment)
297301
repo.update()
298302
repo.checkout('submodule')
299303
valid, _ = repo.validate_submodules(self.dummy_conf)
300304
self.assertTrue(valid)
301305

302306
def test_check_invalid_submodule_urls(self):
303-
repo = self.project.vcs_repo()
307+
repo = self.project.vcs_repo(environment=self.build_environment)
304308
repo.update()
305309
repo.checkout('invalidsubmodule')
306310
with self.assertRaises(RepositoryError) as e:
@@ -313,7 +317,7 @@ def test_check_invalid_submodule_urls(self):
313317
)
314318

315319
def test_invalid_submodule_is_ignored(self):
316-
repo = self.project.vcs_repo()
320+
repo = self.project.vcs_repo(environment=self.build_environment)
317321
repo.update()
318322
repo.checkout('submodule')
319323
gitmodules_path = os.path.join(repo.working_dir, '.gitmodules')
@@ -341,7 +345,7 @@ def test_fetch_clean_tags_and_branches(self, checkout_path):
341345
os.mkdir(local_repo)
342346
checkout_path.return_value = local_repo
343347

344-
repo = self.project.vcs_repo()
348+
repo = self.project.vcs_repo(environment=self.build_environment)
345349
repo.clone()
346350

347351
delete_git_tag(upstream_repo, 'v02')
@@ -391,20 +395,25 @@ def setUp(self):
391395
repo=hg_repo,
392396
)
393397
self.project.users.add(self.eric)
398+
self.build_environment = LocalBuildEnvironment()
394399

395400
def test_parse_branches(self):
396401
data = """\
397402
stable
398403
default
399404
"""
400405

401-
expected_ids = ['stable', 'default']
402-
given_ids = [x.identifier for x in
403-
self.project.vcs_repo().parse_branches(data)]
406+
expected_ids = ["stable", "default"]
407+
given_ids = [
408+
x.identifier
409+
for x in self.project.vcs_repo(
410+
environment=self.build_environment
411+
).parse_branches(data)
412+
]
404413
self.assertEqual(expected_ids, given_ids)
405414

406415
def test_update_and_checkout(self):
407-
repo = self.project.vcs_repo()
416+
repo = self.project.vcs_repo(environment=self.build_environment)
408417
repo.make_clean_working_dir()
409418
code, _, _ = repo.update()
410419
self.assertEqual(code, 0)
@@ -413,7 +422,7 @@ def test_update_and_checkout(self):
413422
self.assertTrue(exists(repo.working_dir))
414423

415424
def test_checkout_invalid_revision(self):
416-
repo = self.project.vcs_repo()
425+
repo = self.project.vcs_repo(environment=self.build_environment)
417426
repo.update()
418427
version = 'invalid-revision'
419428
with self.assertRaises(RepositoryError) as e:
@@ -436,6 +445,10 @@ def test_parse_tags(self):
436445
('2b2155623ee2', '1.7.5'),
437446
]
438447

439-
given_ids = [(x.identifier, x.verbose_name) for x in
440-
self.project.vcs_repo().parse_tags(data)]
448+
given_ids = [
449+
(x.identifier, x.verbose_name)
450+
for x in self.project.vcs_repo(
451+
environment=self.build_environment
452+
).parse_tags(data)
453+
]
441454
self.assertEqual(expected_tags, given_ids)

readthedocs/rtd_tests/tests/test_backend_svn.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django_dynamic_fixture import get
55

66
from readthedocs.builds.models import Version
7+
from readthedocs.doc_builder.environments import LocalBuildEnvironment
78
from readthedocs.projects.models import Project
89
from readthedocs.vcs_support.backends.svn import Backend as SvnBackend
910

@@ -13,7 +14,8 @@ class TestSvnBackend(TestCase):
1314
def test_get_url(self):
1415
project = get(Project)
1516
version = get(Version, project=project)
16-
backend_obj = SvnBackend(project, version.slug)
17+
environment = LocalBuildEnvironment()
18+
backend_obj = SvnBackend(project, version.slug, environment=environment)
1719

1820
base = 'http://example.com/'
1921
tag = 'xyz/'

readthedocs/rtd_tests/tests/test_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_vcs_url_for_external_version_gitlab(self):
7070
self.assertEqual(self.external_version.vcs_url, expected_url)
7171

7272
def test_vcs_url_for_latest_version(self):
73-
slug = self.pip.default_branch or self.pip.vcs_repo().fallback_branch
73+
slug = self.pip.default_branch or self.pip.vcs_class().fallback_branch
7474
expected_url = f'https://github.com/pypa/pip/tree/{slug}/'
7575
self.assertEqual(self.tag_version.vcs_url, expected_url)
7676

readthedocs/vcs_support/base.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ class BaseVCS:
5555
# Defining a base API, so we'll have unused args
5656
# pylint: disable=unused-argument
5757
def __init__(
58-
self, project, version_slug, environment=None,
59-
verbose_name=None, version_type=None, **kwargs
58+
self,
59+
project,
60+
version_slug,
61+
environment,
62+
verbose_name=None,
63+
version_type=None,
64+
**kwargs
6065
):
6166
self.default_branch = project.default_branch
6267
self.project = project
@@ -67,13 +72,7 @@ def __init__(
6772
self.verbose_name = verbose_name
6873
self.version_type = version_type
6974

70-
# TODO: always pass an explicit environment
71-
# This is only used in tests #6546
72-
#
73-
# TODO: we should not allow ``environment=None`` and always use the
74-
# environment defined by the settings
75-
from readthedocs.doc_builder.environments import LocalBuildEnvironment
76-
self.environment = environment or LocalBuildEnvironment()
75+
self.environment = environment
7776

7877
def check_working_dir(self):
7978
if not os.path.exists(self.working_dir):

0 commit comments

Comments
 (0)