diff --git a/readthedocs/projects/tasks.py b/readthedocs/projects/tasks.py index 78543815591..108e74b7750 100644 --- a/readthedocs/projects/tasks.py +++ b/readthedocs/projects/tasks.py @@ -729,6 +729,7 @@ def update_app_instances( args=[ self.project.pk, self.version.pk, + self.config.doctype, ], kwargs=dict( hostname=socket.gethostname(), @@ -812,7 +813,9 @@ def build_docs_html(self): broadcast( type='app', task=move_files, - args=[self.version.pk, socket.gethostname()], + args=[ + self.version.pk, socket.gethostname(), self.config.doctype + ], kwargs=dict(html=True), ) except socket.error: @@ -885,6 +888,7 @@ def is_type_sphinx(self): def sync_files( project_pk, version_pk, + doctype, hostname=None, html=False, localmedia=False, @@ -919,6 +923,7 @@ def sync_files( move_files( version_pk, hostname, + doctype, html=html, localmedia=localmedia, search=search, @@ -937,6 +942,7 @@ def sync_files( def move_files( version_pk, hostname, + doctype, html=False, localmedia=False, search=False, @@ -971,59 +977,58 @@ def move_files( if html: from_path = version.project.artifact_path( version=version.slug, - type_=version.project.documentation_type, + type_=doctype, ) target = version.project.rtd_build_path(version.slug) Syncer.copy(from_path, target, host=hostname) - if 'sphinx' in version.project.documentation_type: - if search: - from_path = version.project.artifact_path( - version=version.slug, - type_='sphinx_search', - ) - to_path = version.project.get_production_media_path( - type_='json', - version_slug=version.slug, - include_file=False, - ) - Syncer.copy(from_path, to_path, host=hostname) + if search: + from_path = version.project.artifact_path( + version=version.slug, + type_='sphinx_search', + ) + to_path = version.project.get_production_media_path( + type_='json', + version_slug=version.slug, + include_file=False, + ) + Syncer.copy(from_path, to_path, host=hostname) - if localmedia: - from_path = version.project.artifact_path( - version=version.slug, - type_='sphinx_localmedia', - ) - to_path = version.project.get_production_media_path( - type_='htmlzip', - version_slug=version.slug, - include_file=False, - ) - Syncer.copy(from_path, to_path, host=hostname) + if localmedia: + from_path = version.project.artifact_path( + version=version.slug, + type_='sphinx_localmedia', + ) + to_path = version.project.get_production_media_path( + type_='htmlzip', + version_slug=version.slug, + include_file=False, + ) + Syncer.copy(from_path, to_path, host=hostname) - # Always move PDF's because the return code lies. - if pdf: - from_path = version.project.artifact_path( - version=version.slug, - type_='sphinx_pdf', - ) - to_path = version.project.get_production_media_path( - type_='pdf', - version_slug=version.slug, - include_file=False, - ) - Syncer.copy(from_path, to_path, host=hostname) - if epub: - from_path = version.project.artifact_path( - version=version.slug, - type_='sphinx_epub', - ) - to_path = version.project.get_production_media_path( - type_='epub', - version_slug=version.slug, - include_file=False, - ) - Syncer.copy(from_path, to_path, host=hostname) + # Always move PDF's because the return code lies. + if pdf: + from_path = version.project.artifact_path( + version=version.slug, + type_='sphinx_pdf', + ) + to_path = version.project.get_production_media_path( + type_='pdf', + version_slug=version.slug, + include_file=False, + ) + Syncer.copy(from_path, to_path, host=hostname) + if epub: + from_path = version.project.artifact_path( + version=version.slug, + type_='sphinx_epub', + ) + to_path = version.project.get_production_media_path( + type_='epub', + version_slug=version.slug, + include_file=False, + ) + Syncer.copy(from_path, to_path, host=hostname) @app.task(queue='web') diff --git a/readthedocs/rtd_tests/tests/test_projects_tasks.py b/readthedocs/rtd_tests/tests/test_projects_tasks.py new file mode 100644 index 00000000000..03beaf87106 --- /dev/null +++ b/readthedocs/rtd_tests/tests/test_projects_tasks.py @@ -0,0 +1,115 @@ +from __future__ import division, print_function, unicode_literals + +from django.test import TestCase +from django_dynamic_fixture import get +from mock import patch + +from readthedocs.builds.models import Version +from readthedocs.projects.models import Project +from readthedocs.projects.tasks import sync_files + + +class SyncFilesTests(TestCase): + + def setUp(self): + self.project = get(Project) + self.version = get(Version, project=self.project) + + @patch('readthedocs.builds.syncers.Syncer.copy') + @patch('readthedocs.projects.tasks.shutil.rmtree') + def test_sync_files_clean_old_artifacts(self, rmtree, copy): + sync_files(self.project.pk, self.version.pk, 'sphinx', html=True) + pdf, epub = rmtree.call_args_list + + # pdf and epub are cleaned + args, _ = pdf + self.assertIn('pdf', args[0]) + args, _ = epub + self.assertIn('epub', args[0]) + + # Artifacts are copied to the rtd-builds directory + copy.assert_called_once() + args, _ = copy.call_args + self.assertIn('artifacts', args[0]) + self.assertIn('sphinx', args[0]) + self.assertIn('rtd-builds', args[1]) + + @patch('readthedocs.builds.syncers.Syncer.copy') + @patch('readthedocs.projects.tasks.shutil.rmtree') + def test_sync_files_pdf(self, rmtree, copy): + sync_files( + self.project.pk, self.version.pk, 'sphinx', pdf=True + ) + + # epub is cleaned + rmtree.assert_called_once() + args, _ = rmtree.call_args + self.assertIn('epub', args[0]) + + # Artifacts are copied to the media directory + copy.assert_called_once() + args, _ = copy.call_args + self.assertIn('artifacts', args[0]) + self.assertIn('sphinx_pdf', args[0]) + self.assertIn('media/pdf', args[1]) + + @patch('readthedocs.builds.syncers.Syncer.copy') + @patch('readthedocs.projects.tasks.shutil.rmtree') + def test_sync_files_epub(self, rmtree, copy): + sync_files( + self.project.pk, self.version.pk, 'sphinx', epub=True + ) + + # pdf is cleaned + rmtree.assert_called_once() + args, _ = rmtree.call_args + self.assertIn('pdf', args[0]) + + # Artifacts are copied to the media directory + copy.assert_called_once() + args, _ = copy.call_args + self.assertIn('artifacts', args[0]) + self.assertIn('sphinx_epub', args[0]) + self.assertIn('media/epub', args[1]) + + @patch('readthedocs.builds.syncers.Syncer.copy') + @patch('readthedocs.projects.tasks.shutil.rmtree') + def test_sync_files_localmedia(self, rmtree, copy): + sync_files( + self.project.pk, self.version.pk, 'sphinx', localmedia=True + ) + pdf, epub = rmtree.call_args_list + + # pdf and epub are cleaned + args, _ = pdf + self.assertIn('pdf', args[0]) + args, _ = epub + self.assertIn('epub', args[0]) + + # Artifacts are copied to the media directory + copy.assert_called_once() + args, _ = copy.call_args + self.assertIn('artifacts', args[0]) + self.assertIn('sphinx_localmedia', args[0]) + self.assertIn('media/htmlzip', args[1]) + + @patch('readthedocs.builds.syncers.Syncer.copy') + @patch('readthedocs.projects.tasks.shutil.rmtree') + def test_sync_files_search(self, rmtree, copy): + sync_files( + self.project.pk, self.version.pk, 'sphinx', search=True + ) + pdf, epub = rmtree.call_args_list + + # pdf and epub are cleaned + args, _ = pdf + self.assertIn('pdf', args[0]) + args, _ = epub + self.assertIn('epub', args[0]) + + # Artifacts are copied to the media directory + copy.assert_called_once() + args, _ = copy.call_args + self.assertIn('artifacts', args[0]) + self.assertIn('sphinx_search', args[0]) + self.assertIn('media/json', args[1])