2014-12-14 18:25:50 +00:00
|
|
|
|
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
|
2016-01-21 23:25:23 +00:00
|
|
|
from readthedocs.doc_builder.config import ConfigWrapper
|
2015-07-25 20:19:51 +00:00
|
|
|
from readthedocs.doc_builder.environments import LocalEnvironment
|
2015-12-16 23:54:10 +00:00
|
|
|
from readthedocs.doc_builder.python_environments import Virtualenv
|
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
|
2016-01-21 23:25:23 +00:00
|
|
|
from readthedocs.rtd_tests.tests.test_config_wrapper import get_build_config
|
2014-12-06 02:10:15 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
from ..mocks.environment import EnvironmentMockGroup
|
2014-12-06 02:10:15 +00:00
|
|
|
|
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
class BuildEnvironmentTests(TestCase):
|
2014-12-06 02:10:15 +00:00
|
|
|
|
2015-08-04 21:34:13 +00:00
|
|
|
def setUp(self):
|
2015-08-05 21:21:15 +00:00
|
|
|
self.mocks = EnvironmentMockGroup()
|
|
|
|
self.mocks.start()
|
2015-08-04 21:34:13 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2015-08-05 21:21:15 +00:00
|
|
|
self.mocks.stop()
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
def test_build(self):
|
|
|
|
'''Test full build'''
|
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()])
|
2015-08-05 21:21:15 +00:00
|
|
|
version = project.versions.all()[0]
|
|
|
|
self.mocks.configure_mock('api_versions', {'return_value': [version]})
|
|
|
|
self.mocks.configure_mock('api', {
|
|
|
|
'get.return_value': {'downloads': "no_url_here"}
|
|
|
|
})
|
|
|
|
self.mocks.patches['html_build'].stop()
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2016-01-21 23:25:23 +00:00
|
|
|
yaml_config = get_build_config({})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
|
|
|
version=version, search=False, localmedia=False, config=config)
|
2015-12-16 23:54:10 +00:00
|
|
|
task.build_docs()
|
2015-08-05 21:21:15 +00:00
|
|
|
|
|
|
|
# Get command and check first part of command list is a call to sphinx
|
2016-03-10 21:22:13 +00:00
|
|
|
self.assertEqual(self.mocks.popen.call_count, 3)
|
2016-03-10 21:36:11 +00:00
|
|
|
cmd = self.mocks.popen.call_args_list[2][0]
|
2015-08-12 21:46:57 +00:00
|
|
|
self.assertRegexpMatches(cmd[0][0], r'python')
|
|
|
|
self.assertRegexpMatches(cmd[0][1], r'sphinx-build')
|
2015-08-05 21:21:15 +00:00
|
|
|
|
|
|
|
def test_build_respects_pdf_flag(self):
|
|
|
|
'''Build output format control'''
|
2015-08-07 22:29:25 +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()])
|
2014-12-06 02:10:15 +00:00
|
|
|
version = project.versions.all()[0]
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2016-01-21 23:25:23 +00:00
|
|
|
yaml_config = get_build_config({})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
|
|
|
version=version, search=False, localmedia=False, config=config)
|
|
|
|
|
2015-12-16 23:54:10 +00:00
|
|
|
task.build_docs()
|
2015-08-05 21:21:15 +00:00
|
|
|
|
|
|
|
# The HTML and the Epub format were built.
|
|
|
|
self.mocks.html_build.assert_called_once_with()
|
|
|
|
self.mocks.pdf_build.assert_called_once_with()
|
|
|
|
# PDF however was disabled and therefore not built.
|
|
|
|
self.assertFalse(self.mocks.epub_build.called)
|
2014-12-14 18:25:50 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
def test_build_respects_epub_flag(self):
|
|
|
|
'''Test build with epub enabled'''
|
2015-08-07 22:29:25 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-1',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
|
|
|
enable_pdf_build=False,
|
|
|
|
enable_epub_build=True,
|
|
|
|
versions=[fixture()])
|
2015-08-05 21:21:15 +00:00
|
|
|
version = project.versions.all()[0]
|
2015-05-21 13:39:42 +00:00
|
|
|
|
2015-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2016-01-21 23:25:23 +00:00
|
|
|
yaml_config = get_build_config({})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
|
|
|
version=version, search=False, localmedia=False, config=config)
|
2015-12-16 23:54:10 +00:00
|
|
|
task.build_docs()
|
2015-02-26 00:23:21 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
# The HTML and the Epub format were built.
|
|
|
|
self.mocks.html_build.assert_called_once_with()
|
|
|
|
self.mocks.epub_build.assert_called_once_with()
|
|
|
|
# PDF however was disabled and therefore not built.
|
2016-01-21 23:50:18 +00:00
|
|
|
self.assertFalse(self.mocks.pdf_build.called)
|
|
|
|
|
|
|
|
def test_build_respects_yaml(self):
|
2016-01-29 19:24:23 +00:00
|
|
|
'''Test YAML build options'''
|
2016-01-21 23:50:18 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-1',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
|
|
|
enable_pdf_build=False,
|
|
|
|
enable_epub_build=False,
|
|
|
|
versions=[fixture()])
|
|
|
|
version = project.versions.all()[0]
|
|
|
|
|
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
|
|
|
yaml_config = get_build_config({'formats': ['epub']})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
|
|
|
version=version, search=False, localmedia=False, config=config)
|
|
|
|
task.build_docs()
|
|
|
|
|
|
|
|
# The HTML and the Epub format were built.
|
|
|
|
self.mocks.html_build.assert_called_once_with()
|
|
|
|
self.mocks.epub_build.assert_called_once_with()
|
|
|
|
# PDF however was disabled and therefore not built.
|
2015-08-05 21:21:15 +00:00
|
|
|
self.assertFalse(self.mocks.pdf_build.called)
|
2015-02-26 00:23:21 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
def test_builder_comments(self):
|
|
|
|
'''Normal build with comments'''
|
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-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(version=version, project=project, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2015-06-11 14:29:26 +00:00
|
|
|
builder_class = get_builder_class(project.documentation_type)
|
2015-12-16 23:54:10 +00:00
|
|
|
builder = builder_class(build_env, python_env)
|
2015-02-26 00:23:21 +00:00
|
|
|
self.assertEqual(builder.sphinx_builder, 'readthedocs-comments')
|
|
|
|
|
|
|
|
def test_builder_no_comments(self):
|
2015-08-05 21:21:15 +00:00
|
|
|
'''Test builder without comments'''
|
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-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(version=version, project=project, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2015-06-11 14:29:26 +00:00
|
|
|
builder_class = get_builder_class(project.documentation_type)
|
2015-12-16 23:54:10 +00:00
|
|
|
builder = builder_class(build_env, python_env)
|
2015-02-26 00:23:21 +00:00
|
|
|
self.assertEqual(builder.sphinx_builder, 'readthedocs')
|
2015-06-15 07:41:24 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
def test_build_pdf_latex_failures(self):
|
|
|
|
'''Build failure if latex fails'''
|
|
|
|
self.mocks.patches['html_build'].stop()
|
|
|
|
self.mocks.patches['pdf_build'].stop()
|
2015-06-15 07:41:24 +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',
|
|
|
|
enable_pdf_build=True,
|
|
|
|
enable_epub_build=False,
|
|
|
|
versions=[fixture()])
|
2015-06-15 07:41:24 +00:00
|
|
|
version = project.versions.all()[0]
|
2015-08-05 21:21:15 +00:00
|
|
|
assert project.conf_dir() == '/tmp/rtd'
|
2015-06-15 07:41:24 +00:00
|
|
|
|
2015-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2016-01-21 23:25:23 +00:00
|
|
|
yaml_config = get_build_config({})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
2015-12-16 23:54:10 +00:00
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
2016-01-21 23:25:23 +00:00
|
|
|
version=version, search=False, localmedia=False, config=config)
|
2015-08-05 21:21:15 +00:00
|
|
|
|
|
|
|
# Mock out the separate calls to Popen using an iterable side_effect
|
|
|
|
returns = [
|
|
|
|
(('', ''), 0), # sphinx-build html
|
|
|
|
(('', ''), 0), # sphinx-build pdf
|
|
|
|
(('', ''), 1), # latex
|
|
|
|
(('', ''), 0), # makeindex
|
|
|
|
(('', ''), 0), # latex
|
|
|
|
]
|
|
|
|
mock_obj = mock.Mock()
|
|
|
|
mock_obj.communicate.side_effect = [output for (output, status)
|
|
|
|
in returns]
|
|
|
|
type(mock_obj).returncode = mock.PropertyMock(
|
|
|
|
side_effect=[status for (output, status) in returns])
|
|
|
|
self.mocks.popen.return_value = mock_obj
|
|
|
|
|
|
|
|
with build_env:
|
2015-12-16 23:54:10 +00:00
|
|
|
task.build_docs()
|
2016-03-10 21:22:13 +00:00
|
|
|
self.assertEqual(self.mocks.popen.call_count, 7)
|
2015-08-05 21:21:15 +00:00
|
|
|
self.assertTrue(build_env.failed)
|
|
|
|
|
|
|
|
def test_build_pdf_latex_not_failure(self):
|
|
|
|
'''Test pass during PDF builds and bad latex failure status code'''
|
|
|
|
self.mocks.patches['html_build'].stop()
|
|
|
|
self.mocks.patches['pdf_build'].stop()
|
2015-06-15 07:41:24 +00:00
|
|
|
|
2015-08-06 14:07:48 +00:00
|
|
|
project = get(Project,
|
|
|
|
slug='project-2',
|
|
|
|
documentation_type='sphinx',
|
|
|
|
conf_py_file='test_conf.py',
|
2015-08-05 21:21:15 +00:00
|
|
|
enable_pdf_build=True,
|
|
|
|
enable_epub_build=False,
|
2015-08-06 14:07:48 +00:00
|
|
|
versions=[fixture()])
|
2015-06-15 07:41:24 +00:00
|
|
|
version = project.versions.all()[0]
|
2015-08-05 21:21:15 +00:00
|
|
|
assert project.conf_dir() == '/tmp/rtd'
|
2015-06-15 07:41:24 +00:00
|
|
|
|
2015-08-07 03:19:40 +00:00
|
|
|
build_env = LocalEnvironment(project=project, version=version, build={})
|
2015-12-18 18:26:44 +00:00
|
|
|
python_env = Virtualenv(version=version, build_env=build_env)
|
2016-01-21 23:25:23 +00:00
|
|
|
yaml_config = get_build_config({})
|
|
|
|
config = ConfigWrapper(version=version, yaml_config=yaml_config)
|
2015-12-16 23:54:10 +00:00
|
|
|
task = UpdateDocsTask(build_env=build_env, project=project, python_env=python_env,
|
2016-01-21 23:25:23 +00:00
|
|
|
version=version, search=False, localmedia=False, config=config)
|
2015-06-15 07:41:24 +00:00
|
|
|
|
2015-08-05 21:21:15 +00:00
|
|
|
# Mock out the separate calls to Popen using an iterable side_effect
|
|
|
|
returns = [
|
|
|
|
(('', ''), 0), # sphinx-build html
|
|
|
|
(('', ''), 0), # sphinx-build pdf
|
|
|
|
(('Output written on foo.pdf', ''), 1), # latex
|
|
|
|
(('', ''), 0), # makeindex
|
|
|
|
(('', ''), 0), # latex
|
|
|
|
]
|
|
|
|
mock_obj = mock.Mock()
|
|
|
|
mock_obj.communicate.side_effect = [output for (output, status)
|
|
|
|
in returns]
|
|
|
|
type(mock_obj).returncode = mock.PropertyMock(
|
|
|
|
side_effect=[status for (output, status) in returns])
|
|
|
|
self.mocks.popen.return_value = mock_obj
|
|
|
|
|
|
|
|
with build_env:
|
2015-12-16 23:54:10 +00:00
|
|
|
task.build_docs()
|
2016-03-10 21:22:13 +00:00
|
|
|
self.assertEqual(self.mocks.popen.call_count, 7)
|
2015-08-05 21:21:15 +00:00
|
|
|
self.assertTrue(build_env.successful)
|