2015-05-21 13:39:42 +00:00
|
|
|
import os
|
2014-12-14 18:25:50 +00:00
|
|
|
import subprocess
|
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
from django.test import TestCase
|
2015-08-06 14:07:48 +00:00
|
|
|
from django_dynamic_fixture import get
|
|
|
|
from django_dynamic_fixture import fixture
|
2014-12-06 02:10:15 +00:00
|
|
|
import mock
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-06 14:07:48 +00:00
|
|
|
from readthedocs.projects.models import Project
|
2015-07-25 20:19:51 +00:00
|
|
|
from readthedocs.doc_builder.environments import LocalEnvironment
|
2015-07-28 23:50:58 +00:00
|
|
|
from readthedocs.doc_builder.loader import get_builder_class
|
2015-07-25 20:19:51 +00:00
|
|
|
from readthedocs.projects.tasks import UpdateDocsTask
|
|
|
|
from readthedocs.rtd_tests.factories.projects_factories import ProjectFactory
|
|
|
|
from readthedocs.rtd_tests.mocks.paths import fake_paths_lookup
|
2014-12-06 02:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockProcess(object):
|
|
|
|
returncode = 0
|
|
|
|
communicate_result = None
|
|
|
|
|
|
|
|
def __init__(self, communicate_result):
|
|
|
|
self.communicate_result = communicate_result
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
def communicate(self):
|
|
|
|
return self.communicate_result
|
|
|
|
|
|
|
|
|
|
|
|
def build_subprocess_side_effect(*args, **kwargs):
|
|
|
|
if args == (('git', 'rev-parse', 'HEAD'),):
|
|
|
|
return MockProcess(('SOMEGITHASH', ''))
|
|
|
|
elif 'sphinx-build' in args[0]:
|
|
|
|
return MockProcess(("Here's where our build report goes.", "Here's our error message."))
|
|
|
|
else:
|
|
|
|
return subprocess.Popen(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class BuildTests(TestCase):
|
|
|
|
|
2015-08-04 21:34:13 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.patches = {}
|
|
|
|
self.mocks = {}
|
|
|
|
self.patches['update_build'] = mock.patch.object(LocalEnvironment,
|
|
|
|
'update_build')
|
|
|
|
self.mocks['update_build'] = self.patches['update_build'].start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for patch in self.patches:
|
|
|
|
self.patches[patch].stop()
|
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
@mock.patch('slumber.Resource')
|
|
|
|
@mock.patch('os.chdir')
|
2015-07-29 00:12:24 +00:00
|
|
|
@mock.patch('readthedocs.projects.models.Project.api_versions')
|
2015-08-06 15:03:28 +00:00
|
|
|
@mock.patch('readthedocs.vcs_support.utils.NonBlockingLock.__enter__')
|
2014-12-06 02:10:15 +00:00
|
|
|
@mock.patch('subprocess.Popen')
|
2015-08-06 15:03:28 +00:00
|
|
|
def test_build(self,
|
|
|
|
mock_Popen,
|
|
|
|
mock_NonBlockingLock_enter,
|
|
|
|
mock_api_versions,
|
|
|
|
mock_chdir,
|
|
|
|
mock_apiv2_downloads):
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
# subprocess mock logic
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
mock_process = mock.Mock()
|
2015-08-04 21:34:13 +00:00
|
|
|
process_return_dict = {
|
|
|
|
'communicate.return_value': ('SOMEGITHASH', ''),
|
|
|
|
'returncode': 0
|
|
|
|
}
|
2014-12-06 02:10:15 +00:00
|
|
|
mock_process.configure_mock(**process_return_dict)
|
|
|
|
mock_Popen.return_value = mock_process
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-1',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
|
|
|
versions=[fixture()])
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
version = project.versions.all()[0]
|
|
|
|
mock_api_versions.return_value = [version]
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2014-12-06 02:10:15 +00:00
|
|
|
mock_apiv2_downloads.get.return_value = {'downloads': "no_url_here"}
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-07-13 08:55:50 +00:00
|
|
|
conf_path = os.path.join(
|
|
|
|
project.checkout_path(version.slug),
|
|
|
|
project.conf_py_file)
|
2015-05-21 13:39:42 +00:00
|
|
|
|
2015-06-15 07:41:24 +00:00
|
|
|
# Mock open to simulate existing conf.py file
|
2015-07-25 20:19:51 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version)
|
|
|
|
task = UpdateDocsTask()
|
|
|
|
task.build_env = build_env
|
|
|
|
task.version = version
|
|
|
|
task.project = project
|
2014-12-06 02:10:15 +00:00
|
|
|
with mock.patch('codecs.open', mock.mock_open(), create=True):
|
2015-07-13 08:55:50 +00:00
|
|
|
with fake_paths_lookup({conf_path: True}):
|
2015-08-04 21:34:13 +00:00
|
|
|
built_docs = task.build_docs_html(False)
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-04 21:34:13 +00:00
|
|
|
with build_env:
|
|
|
|
builder_class = get_builder_class(project.documentation_type)
|
|
|
|
builder = builder_class(version)
|
2015-02-26 00:23:21 +00:00
|
|
|
self.assertIn(builder.sphinx_builder,
|
2014-12-06 02:10:15 +00:00
|
|
|
str(mock_Popen.call_args_list[1])
|
2014-12-14 18:25:50 +00:00
|
|
|
)
|
2015-02-26 00:23:21 +00:00
|
|
|
|
|
|
|
def test_builder_comments(self):
|
|
|
|
|
|
|
|
# Normal build
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
documentation_type='sphinx',
|
|
|
|
allow_comments=True,
|
|
|
|
versions=[fixture()])
|
2015-02-26 00:23:21 +00:00
|
|
|
version = project.versions.all()[0]
|
2015-06-11 14:29:26 +00:00
|
|
|
builder_class = get_builder_class(project.documentation_type)
|
|
|
|
builder = builder_class(version)
|
2015-02-26 00:23:21 +00:00
|
|
|
self.assertEqual(builder.sphinx_builder, 'readthedocs-comments')
|
|
|
|
|
|
|
|
def test_builder_no_comments(self):
|
|
|
|
|
|
|
|
# Normal build
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
documentation_type='sphinx',
|
|
|
|
allow_comments=False,
|
|
|
|
versions=[fixture()])
|
2015-02-26 00:23:21 +00:00
|
|
|
version = project.versions.all()[0]
|
2015-06-11 14:29:26 +00:00
|
|
|
builder_class = get_builder_class(project.documentation_type)
|
|
|
|
builder = builder_class(version)
|
2015-02-26 00:23:21 +00:00
|
|
|
self.assertEqual(builder.sphinx_builder, 'readthedocs')
|
2015-06-15 07:41:24 +00:00
|
|
|
|
|
|
|
@mock.patch('slumber.Resource')
|
|
|
|
@mock.patch('os.chdir')
|
|
|
|
@mock.patch('subprocess.Popen')
|
2015-08-06 15:03:28 +00:00
|
|
|
@mock.patch('readthedocs.vcs_support.utils.NonBlockingLock.__enter__')
|
2015-07-29 00:12:24 +00:00
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.build')
|
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.PdfBuilder.build')
|
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.EpubBuilder.build')
|
2015-06-15 07:41:24 +00:00
|
|
|
def test_build_respects_pdf_flag(self,
|
|
|
|
EpubBuilder_build,
|
|
|
|
PdfBuilder_build,
|
|
|
|
HtmlBuilder_build,
|
2015-08-06 15:03:28 +00:00
|
|
|
mock_NonBlockingLock_enter,
|
2015-06-15 07:41:24 +00:00
|
|
|
mock_Popen,
|
|
|
|
mock_chdir,
|
|
|
|
mock_apiv2_downloads):
|
|
|
|
|
|
|
|
# subprocess mock logic
|
|
|
|
|
|
|
|
mock_process = mock.Mock()
|
|
|
|
process_return_dict = {'communicate.return_value': ('SOMEGITHASH', '')}
|
|
|
|
mock_process.configure_mock(**process_return_dict)
|
|
|
|
mock_Popen.return_value = mock_process
|
|
|
|
mock_Popen.side_effect = build_subprocess_side_effect
|
|
|
|
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-1',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
|
|
|
enable_pdf_build=True,
|
|
|
|
enable_epub_build=False,
|
|
|
|
versions=[fixture()])
|
2015-06-15 07:41:24 +00:00
|
|
|
version = project.versions.all()[0]
|
|
|
|
|
|
|
|
conf_path = os.path.join(project.checkout_path(version.slug), project.conf_py_file)
|
|
|
|
|
|
|
|
# Mock open to simulate existing conf.py file
|
2015-07-25 20:19:51 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version)
|
|
|
|
task = UpdateDocsTask()
|
|
|
|
task.build_env = build_env
|
|
|
|
task.version = version
|
|
|
|
task.project = project
|
2015-06-15 07:41:24 +00:00
|
|
|
with mock.patch('codecs.open', mock.mock_open(), create=True):
|
2015-07-13 08:55:50 +00:00
|
|
|
with fake_paths_lookup({conf_path: True}):
|
2015-07-25 20:19:51 +00:00
|
|
|
built_docs = task.build_docs(version,
|
|
|
|
False,
|
|
|
|
False)
|
2015-06-15 07:41:24 +00:00
|
|
|
|
|
|
|
# The HTML and the PDF format were built.
|
|
|
|
self.assertEqual(HtmlBuilder_build.call_count, 1)
|
|
|
|
self.assertEqual(PdfBuilder_build.call_count, 1)
|
|
|
|
# Epub however was disabled and therefore not built.
|
|
|
|
self.assertEqual(EpubBuilder_build.call_count, 0)
|
|
|
|
|
|
|
|
@mock.patch('slumber.Resource')
|
|
|
|
@mock.patch('os.chdir')
|
|
|
|
@mock.patch('subprocess.Popen')
|
2015-08-06 15:03:28 +00:00
|
|
|
@mock.patch('readthedocs.vcs_support.utils.NonBlockingLock.__enter__')
|
2015-07-29 00:12:24 +00:00
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.build')
|
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.PdfBuilder.build')
|
|
|
|
@mock.patch('readthedocs.doc_builder.backends.sphinx.EpubBuilder.build')
|
2015-06-15 07:41:24 +00:00
|
|
|
def test_build_respects_epub_flag(self,
|
|
|
|
EpubBuilder_build,
|
|
|
|
PdfBuilder_build,
|
|
|
|
HtmlBuilder_build,
|
2015-08-06 15:03:28 +00:00
|
|
|
mock_NonBlockingLock_enter,
|
2015-06-15 07:41:24 +00:00
|
|
|
mock_Popen,
|
|
|
|
mock_chdir,
|
|
|
|
mock_apiv2_downloads):
|
|
|
|
|
|
|
|
# subprocess mock logic
|
|
|
|
|
|
|
|
mock_process = mock.Mock()
|
|
|
|
process_return_dict = {'communicate.return_value': ('SOMEGITHASH', '')}
|
|
|
|
mock_process.configure_mock(**process_return_dict)
|
|
|
|
mock_Popen.return_value = mock_process
|
|
|
|
mock_Popen.side_effect = build_subprocess_side_effect
|
|
|
|
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-2',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
|
|
|
enable_pdf_build=False,
|
|
|
|
enable_epub_build=True,
|
|
|
|
versions=[fixture()])
|
2015-06-15 07:41:24 +00:00
|
|
|
version = project.versions.all()[0]
|
|
|
|
|
|
|
|
conf_path = os.path.join(project.checkout_path(version.slug), project.conf_py_file)
|
|
|
|
|
|
|
|
# Mock open to simulate existing conf.py file
|
2015-07-25 20:19:51 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version)
|
|
|
|
task = UpdateDocsTask()
|
|
|
|
task.build_env = build_env
|
|
|
|
task.version = version
|
|
|
|
task.project = project
|
2015-06-15 07:41:24 +00:00
|
|
|
with mock.patch('codecs.open', mock.mock_open(), create=True):
|
2015-07-13 08:55:50 +00:00
|
|
|
with fake_paths_lookup({conf_path: True}):
|
2015-07-25 20:19:51 +00:00
|
|
|
built_docs = task.build_docs(version,
|
|
|
|
False,
|
|
|
|
False)
|
2015-06-15 07:41:24 +00:00
|
|
|
|
|
|
|
# The HTML and the Epub format were built.
|
|
|
|
self.assertEqual(HtmlBuilder_build.call_count, 1)
|
|
|
|
self.assertEqual(EpubBuilder_build.call_count, 1)
|
|
|
|
# PDF however was disabled and therefore not built.
|
|
|
|
self.assertEqual(PdfBuilder_build.call_count, 0)
|