Skip to content

Commit d8c2131

Browse files
committed
Refactor test
1 parent 2394d28 commit d8c2131

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

readthedocs/rtd_tests/tests/test_backend.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def test_git_branches(self, checkout_path):
5656
default_branches = [
5757
# comes from ``make_test_git`` function
5858
'submodule',
59-
'relativesubmodule',
6059
'invalidsubmodule',
6160
]
6261
branches = [
@@ -89,7 +88,6 @@ def test_git_branches_unicode(self, checkout_path):
8988
default_branches = [
9089
# comes from ``make_test_git`` function
9190
'submodule',
92-
'relativesubmodule',
9391
'invalidsubmodule',
9492
]
9593
branches = [
@@ -163,15 +161,19 @@ def test_check_submodule_urls(self):
163161
repo.checkout('submodule')
164162
valid, _ = repo.validate_submodules(self.dummy_conf)
165163
self.assertTrue(valid)
166-
repo.checkout('relativesubmodule')
167-
valid, _ = repo.validate_submodules(self.dummy_conf)
168-
self.assertTrue(valid)
169164

170-
@pytest.mark.xfail(strict=True, reason="Fixture is not working correctly")
171165
def test_check_invalid_submodule_urls(self):
166+
repo = self.project.vcs_repo()
167+
repo.update()
168+
r = repo.checkout('invalidsubmodule')
172169
with self.assertRaises(RepositoryError) as e:
173-
repo.checkout('invalidsubmodule')
174-
self.assertEqual(e.msg, RepositoryError.INVALID_SUBMODULES)
170+
repo.update_submodules(self.dummy_conf)
171+
# `invalid` is created in `make_test_git`
172+
# it's a url in ssh form.
173+
self.assertEqual(
174+
str(e.exception),
175+
RepositoryError.INVALID_SUBMODULES.format(['invalid'])
176+
)
175177

176178
@patch('readthedocs.projects.models.Project.checkout_path')
177179
def test_fetch_clean_tags_and_branches(self, checkout_path):
@@ -197,8 +199,7 @@ def test_fetch_clean_tags_and_branches(self, checkout_path):
197199
)
198200
self.assertEqual(
199201
set([
200-
'relativesubmodule', 'invalidsubmodule',
201-
'master', 'submodule', 'newbranch',
202+
'invalidsubmodule', 'master', 'submodule', 'newbranch',
202203
]),
203204
set(vcs.verbose_name for vcs in repo.branches)
204205
)
@@ -212,8 +213,7 @@ def test_fetch_clean_tags_and_branches(self, checkout_path):
212213
)
213214
self.assertEqual(
214215
set([
215-
'relativesubmodule', 'invalidsubmodule',
216-
'master', 'submodule'
216+
'invalidsubmodule', 'master', 'submodule'
217217
]),
218218
set(vcs.verbose_name for vcs in repo.branches)
219219
)

readthedocs/rtd_tests/utils.py

+40-23
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
"""Utility functions for use in tests."""
33

44
from __future__ import (
5-
absolute_import, division, print_function, unicode_literals)
5+
absolute_import,
6+
division,
7+
print_function,
8+
unicode_literals,
9+
)
610

711
import logging
812
import subprocess
13+
import textwrap
914
from os import chdir, environ, mkdir
1015
from os.path import abspath
1116
from os.path import join as pjoin
@@ -55,34 +60,16 @@ def make_test_git():
5560
# URL are not allowed and using a real URL will require Internet to clone
5661
# the repo
5762
check_output(['git', 'checkout', '-b', 'submodule', 'master'], env=env)
58-
# https://stackoverflow.com/a/37378302/2187091
59-
mkdir(pjoin(directory, 'foobar'))
60-
gitmodules_path = pjoin(directory, '.gitmodules')
61-
with open(gitmodules_path, 'w') as fh:
62-
fh.write('''[submodule "foobar"]\n\tpath = foobar\n\turl = https://foobar.com/git\n''')
63-
check_output(
64-
[
65-
'git', 'update-index', '--add', '--cacheinfo', '160000',
66-
'233febf4846d7a0aeb95b6c28962e06e21d13688', 'foobar',
67-
],
68-
env=env,
63+
add_submodule_without_cloning(
64+
directory, 'foobar', 'https://foobar.com/git'
6965
)
7066
check_output(['git', 'add', '.'], env=env)
7167
check_output(['git', 'commit', '-m"Add submodule"'], env=env)
7268

73-
# Add a relative submodule URL in the relativesubmodule branch
74-
check_output(['git', 'checkout', '-b', 'relativesubmodule', 'master'], env=env)
75-
check_output(
76-
['git', 'submodule', 'add', '-b', 'master', './', 'relativesubmodule'],
77-
env=env
78-
)
79-
check_output(['git', 'add', '.'], env=env)
80-
check_output(['git', 'commit', '-m"Add relative submodule"'], env=env)
8169
# Add an invalid submodule URL in the invalidsubmodule branch
8270
check_output(['git', 'checkout', '-b', 'invalidsubmodule', 'master'], env=env)
83-
check_output(
84-
['git', 'submodule', 'add', '-b', 'master', './', 'invalidsubmodule'],
85-
env=env,
71+
add_submodule_without_cloning(
72+
directory, 'invalid', '[email protected]:rtfd/readthedocs.org.git'
8673
)
8774
check_output(['git', 'add', '.'], env=env)
8875
check_output(['git', 'commit', '-m"Add invalid submodule"'], env=env)
@@ -92,6 +79,36 @@ def make_test_git():
9279
return directory
9380

9481

82+
@restoring_chdir
83+
def add_submodule_without_cloning(directory, submodule, url):
84+
"""
85+
Add a submodule without cloning it.
86+
87+
We write directly to the git index, more details in:
88+
https://stackoverflow.com/a/37378302/2187091
89+
"""
90+
env = environ.copy()
91+
env['GIT_DIR'] = pjoin(directory, '.git')
92+
chdir(directory)
93+
94+
mkdir(pjoin(directory, submodule))
95+
gitmodules_path = pjoin(directory, '.gitmodules')
96+
with open(gitmodules_path, 'w+') as fh:
97+
content = textwrap.dedent('''
98+
[submodule "{submodule}"]
99+
path = {submodule}
100+
url = {url}
101+
''')
102+
fh.write(content.format(submodule=submodule, url=url))
103+
check_output(
104+
[
105+
'git', 'update-index', '--add', '--cacheinfo', '160000',
106+
'233febf4846d7a0aeb95b6c28962e06e21d13688', submodule,
107+
],
108+
env=env,
109+
)
110+
111+
95112
@restoring_chdir
96113
def make_git_repo(directory, name='sample_repo'):
97114
path = get_readthedocs_app_path()

0 commit comments

Comments
 (0)