def test_requirements_file(self): yaml_config = get_build_config({'requirements_file': 'wsgi.py'}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.requirements_file, 'wsgi.py') yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.requirements_file, 'urls.py')
def test_python_interpreter(self): yaml_config = get_build_config({'python': {'version': 3}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.python_interpreter, 'python3') yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.python_interpreter, 'python')
def test_install_project(self): yaml_config = get_build_config({'python': {'setup_py_install': True}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.install_project, True) yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.install_project, False)
def test_python_version(self): yaml_config = get_build_config({'python': {'version': 3}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.python_version, 3) yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.python_version, 2)
def test_conda(self): to_find = 'urls.py' yaml_config = get_build_config({'conda': {'file': to_find}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.use_conda, True) self.assertTrue(config.conda_file[-len(to_find):] == to_find) yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.use_conda, False) self.assertEqual(config.conda_file, None)
def test_build_respects_epub_flag(self): '''Test build with epub enabled''' 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()]) 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({}) 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. self.assertFalse(self.mocks.pdf_build.called)
def test_build(self): '''Test full build''' project = get(Project, slug='project-1', documentation_type='sphinx', conf_py_file='test_conf.py', versions=[fixture()]) 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() build_env = LocalEnvironment(project=project, version=version, build={}) python_env = Virtualenv(version=version, build_env=build_env) 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) task.build_docs() # Get command and check first part of command list is a call to sphinx self.assertEqual(self.mocks.popen.call_count, 1) cmd = self.mocks.popen.call_args_list[0][0] self.assertRegexpMatches(cmd[0][0], r'python') self.assertRegexpMatches(cmd[0][1], r'sphinx-build')
def test_is_obsolete_with_json_missing_build_hash(self): config_data = { 'build': { 'image': '2.0', 'hash': 'a1b2c3', }, 'python': { 'version': 2.7, }, } yaml_config = create_load(config_data)()[0] config = ConfigWrapper(version=self.version, yaml_config=yaml_config) # Set container_image manually self.pip.container_image = 'readthedocs/build:2.0' self.pip.save() python_env = Virtualenv( version=self.version, build_env=self.build_env, config=config, ) env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' # noqa with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa exists.return_value = True self.assertTrue(python_env.is_obsolete)
def test_save_environment_json(self): config_data = { 'build': { 'image': '2.0', }, 'python': { 'version': 2.7, }, } yaml_config = create_load(config_data)()[0] config = ConfigWrapper(version=self.version, yaml_config=yaml_config) python_env = Virtualenv( version=self.version, build_env=self.build_env, config=config, ) with patch( 'readthedocs.doc_builder.python_environments.PythonEnvironment.environment_json_path', return_value=tempfile.mktemp(suffix='envjson'), ): python_env.save_environment_json() json_data = json.load(open(python_env.environment_json_path())) expected_data = { 'build': { 'image': 'readthedocs/build:2.0', 'hash': 'a1b2c3', }, 'python': { 'version': 2.7, }, } self.assertDictEqual(json_data, expected_data)
def __init__(self, version, build_env, config=None): self.version = version self.project = version.project self.build_env = build_env if config: self.config = config else: self.config = ConfigWrapper(version=version, yaml_config={}) # Compute here, since it's used a lot self.checkout_path = self.project.checkout_path(self.version.slug)
def test_extra_requirements(self): yaml_config = get_build_config({'python': { 'pip_install': True, 'extra_requirements': ['tests', 'docs']}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.extra_requirements, ['tests', 'docs']) yaml_config = get_build_config({'python': { 'extra_requirements': ['tests', 'docs']}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.extra_requirements, []) yaml_config = get_build_config({}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.extra_requirements, []) yaml_config = get_build_config({'python': { 'setup_py_install': True, 'extra_requirements': ['tests', 'docs']}}) config = ConfigWrapper(version=self.version, yaml_config=yaml_config) self.assertEqual(config.extra_requirements, [])
def test_is_obsolete_with_invalid_env_json_file(self): yaml_config = create_load()()[0] config = ConfigWrapper(version=self.version, yaml_config=yaml_config) with patch('os.path.exists') as exists: exists.return_value = True python_env = Virtualenv( version=self.version, build_env=self.build_env, config=config, ) self.assertFalse(python_env.is_obsolete)
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() project = get(Project, slug='project-2', documentation_type='sphinx', conf_py_file='test_conf.py', enable_pdf_build=True, enable_epub_build=False, versions=[fixture()]) version = project.versions.all()[0] assert project.conf_dir() == '/tmp/rtd' build_env = LocalEnvironment(project=project, version=version, build={}) python_env = Virtualenv(version=version, build_env=build_env) 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) # 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: task.build_docs() self.assertEqual(self.mocks.popen.call_count, 5) self.assertTrue(build_env.successful)
def test_build_pdf_latex_failures(self): '''Build failure if latex fails''' self.mocks.patches['html_build'].stop() self.mocks.patches['pdf_build'].stop() 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()]) version = project.versions.all()[0] assert project.conf_dir() == '/tmp/rtd' build_env = LocalBuildEnvironment(project=project, version=version, build={}) python_env = Virtualenv(version=version, build_env=build_env) config = ConfigWrapper(version=version, yaml_config=create_load()()[0]) task = UpdateDocsTaskStep(build_env=build_env, project=project, python_env=python_env, version=version, search=False, localmedia=False, config=config) # Mock out the separate calls to Popen using an iterable side_effect returns = [ ((b'', b''), 0), # sphinx-build html ((b'', b''), 0), # sphinx-build pdf ((b'', b''), 1), # latex ((b'', b''), 0), # makeindex ((b'', b''), 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: task.build_docs() self.assertEqual(self.mocks.popen.call_count, 7) self.assertTrue(build_env.failed)
def test_is_obsolete_with_json_same_data_as_version(self): config_data = { 'build': { 'image': '2.0', }, 'python': { 'version': 3.5, }, } yaml_config = create_load(config_data)()[0] config = ConfigWrapper(version=self.version, yaml_config=yaml_config) python_env = Virtualenv( version=self.version, build_env=self.build_env, config=config, ) env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 3.5}}' # noqa with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa exists.return_value = True self.assertFalse(python_env.is_obsolete)
def test_is_obsolete_with_json_different_build_image(self): config_data = { 'build': { 'image': 'latest', }, 'python': { 'version': 2.7, }, } yaml_config = create_load(config_data)()[0] config = ConfigWrapper(version=self.version, yaml_config=yaml_config) python_env = Virtualenv( version=self.version, build_env=None, config=config, ) env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' with patch('os.path.exists') as exists, patch( 'readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa exists.return_value = True self.assertTrue(python_env.is_obsolete)