|
2 | 2 | from __future__ import (
|
3 | 3 | absolute_import, division, print_function, unicode_literals)
|
4 | 4 |
|
| 5 | +from os import path |
| 6 | + |
5 | 7 | import mock
|
| 8 | +import pytest |
| 9 | +import yaml |
6 | 10 | from django.test import TestCase
|
7 | 11 | from django_dynamic_fixture import get
|
| 12 | +from mock import MagicMock, PropertyMock, patch |
8 | 13 |
|
9 | 14 | from readthedocs.builds.models import Version
|
10 | 15 | from readthedocs.config import BuildConfigV1, InvalidConfig, ProjectConfig
|
| 16 | +from readthedocs.config.tests.utils import apply_fs |
11 | 17 | from readthedocs.doc_builder.config import load_yaml_config
|
12 |
| -from readthedocs.projects.models import Project |
| 18 | +from readthedocs.doc_builder.environments import LocalBuildEnvironment |
| 19 | +from readthedocs.doc_builder.python_environments import Conda, Virtualenv |
| 20 | +from readthedocs.projects import tasks |
| 21 | +from readthedocs.projects.models import Feature, Project |
13 | 22 |
|
14 | 23 |
|
15 | 24 | def create_load(config=None):
|
@@ -219,3 +228,261 @@ def test_requirements_file(self, load_config):
|
219 | 228 | load_config.side_effect = create_load()
|
220 | 229 | config = load_yaml_config(self.version)
|
221 | 230 | self.assertEqual(config.requirements_file, '__init__.py')
|
| 231 | + |
| 232 | + |
| 233 | +@pytest.mark.django_db |
| 234 | +@mock.patch('readthedocs.projects.models.Project.checkout_path') |
| 235 | +class TestLoadConfigV2(object): |
| 236 | + |
| 237 | + @pytest.fixture(autouse=True) |
| 238 | + def create_project(self): |
| 239 | + self.project = get( |
| 240 | + Project, |
| 241 | + main_language_project=None, |
| 242 | + install_project=False, |
| 243 | + container_image=None, |
| 244 | + ) |
| 245 | + self.version = get(Version, project=self.project) |
| 246 | + # TODO: Remove later |
| 247 | + get( |
| 248 | + Feature, |
| 249 | + projects=[self.project], |
| 250 | + feature_id=Feature.ALLOW_V2_CONFIG_FILE, |
| 251 | + ) |
| 252 | + |
| 253 | + def create_config_file(self, tmpdir, config): |
| 254 | + base_path = apply_fs(tmpdir, { |
| 255 | + 'readthedocs.yml': '', |
| 256 | + }) |
| 257 | + config.setdefault('version', 2) |
| 258 | + config_file = path.join(str(base_path), 'readthedocs.yml') |
| 259 | + yaml.safe_dump(config, open(config_file, 'w')) |
| 260 | + return base_path |
| 261 | + |
| 262 | + def get_update_docs_task(self): |
| 263 | + build_env = LocalBuildEnvironment( |
| 264 | + self.project, self.version, record=False |
| 265 | + ) |
| 266 | + |
| 267 | + update_docs = tasks.UpdateDocsTaskStep( |
| 268 | + build_env=build_env, |
| 269 | + config=load_yaml_config(self.version), |
| 270 | + project=self.project, |
| 271 | + version=self.version, |
| 272 | + ) |
| 273 | + return update_docs |
| 274 | + |
| 275 | + def test_using_v2(self, checkout_path, tmpdir): |
| 276 | + checkout_path.return_value = str(tmpdir) |
| 277 | + self.create_config_file(tmpdir, {}) |
| 278 | + update_docs = self.get_update_docs_task() |
| 279 | + assert update_docs.config.version == '2' |
| 280 | + |
| 281 | + @pytest.mark.parametrize('config', [{}, {'formats': []}]) |
| 282 | + @patch('readthedocs.projects.models.Project.repo_nonblockinglock', new=MagicMock()) |
| 283 | + @patch('readthedocs.doc_builder.backends.sphinx.SearchBuilder.build') |
| 284 | + @patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.build') |
| 285 | + @patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.append_conf') |
| 286 | + def test_build_formats_default_empty( |
| 287 | + self, append_conf, html_build, search_build, |
| 288 | + checkout_path, config, tmpdir): |
| 289 | + """ |
| 290 | + The default value for formats is [], which means no extra |
| 291 | + formats are build. |
| 292 | + """ |
| 293 | + checkout_path.return_value = str(tmpdir) |
| 294 | + self.create_config_file(tmpdir, config) |
| 295 | + |
| 296 | + update_docs = self.get_update_docs_task() |
| 297 | + outcomes = update_docs.build_docs() |
| 298 | + |
| 299 | + # No extra formats were triggered |
| 300 | + assert outcomes['html'] |
| 301 | + assert outcomes['search'] |
| 302 | + assert not outcomes['localmedia'] |
| 303 | + assert not outcomes['pdf'] |
| 304 | + assert not outcomes['epub'] |
| 305 | + |
| 306 | + @patch('readthedocs.projects.models.Project.repo_nonblockinglock', new=MagicMock()) |
| 307 | + @patch('readthedocs.projects.tasks.UpdateDocsTaskStep.build_docs_class') |
| 308 | + @patch('readthedocs.doc_builder.backends.sphinx.SearchBuilder.build') |
| 309 | + @patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.build') |
| 310 | + @patch('readthedocs.doc_builder.backends.sphinx.HtmlBuilder.append_conf') |
| 311 | + def test_build_formats_only_pdf( |
| 312 | + self, append_conf, html_build, search_build, build_docs_class, |
| 313 | + checkout_path, tmpdir): |
| 314 | + """ |
| 315 | + Only the pdf format is build. |
| 316 | + """ |
| 317 | + checkout_path.return_value = str(tmpdir) |
| 318 | + self.create_config_file(tmpdir, {'formats': ['pdf']}) |
| 319 | + |
| 320 | + update_docs = self.get_update_docs_task() |
| 321 | + outcomes = update_docs.build_docs() |
| 322 | + |
| 323 | + # Only pdf extra format was triggered |
| 324 | + assert outcomes['html'] |
| 325 | + assert outcomes['search'] |
| 326 | + build_docs_class.assert_called_with('sphinx_pdf') |
| 327 | + assert outcomes['pdf'] |
| 328 | + assert not outcomes['localmedia'] |
| 329 | + assert not outcomes['epub'] |
| 330 | + |
| 331 | + @patch('readthedocs.projects.tasks.UpdateDocsTaskStep.update_documentation_type', new=MagicMock()) |
| 332 | + @patch('readthedocs.projects.tasks.UpdateDocsTaskStep.setup_python_environment', new=MagicMock()) |
| 333 | + @patch('readthedocs.projects.tasks.UpdateDocsTaskStep.build_docs', new=MagicMock()) |
| 334 | + @patch('readthedocs.doc_builder.environments.BuildEnvironment.failed', new_callable=PropertyMock) |
| 335 | + def test_conda_environment(self, build_failed, checkout_path, tmpdir): |
| 336 | + build_failed.return_value = False |
| 337 | + checkout_path.return_value = str(tmpdir) |
| 338 | + conda_file = 'environmemt.yml' |
| 339 | + apply_fs(tmpdir, {conda_file: ''}) |
| 340 | + base_path = self.create_config_file( |
| 341 | + tmpdir, |
| 342 | + { |
| 343 | + 'conda': {'environment': conda_file} |
| 344 | + } |
| 345 | + ) |
| 346 | + |
| 347 | + update_docs = self.get_update_docs_task() |
| 348 | + update_docs.run_build(docker=False, record=False) |
| 349 | + |
| 350 | + conda_file = path.join(str(base_path), conda_file) |
| 351 | + assert update_docs.config.conda.environment == conda_file |
| 352 | + assert isinstance(update_docs.python_env, Conda) |
| 353 | + |
| 354 | + def test_default_build_image(self, checkout_path, tmpdir): |
| 355 | + checkout_path.return_value = str(tmpdir) |
| 356 | + build_image = 'readthedocs/build:latest' |
| 357 | + self.create_config_file(tmpdir, {}) |
| 358 | + update_docs = self.get_update_docs_task() |
| 359 | + assert update_docs.config.build.image == build_image |
| 360 | + |
| 361 | + def test_build_image(self, checkout_path, tmpdir): |
| 362 | + checkout_path.return_value = str(tmpdir) |
| 363 | + build_image = 'readthedocs/build:stable' |
| 364 | + self.create_config_file( |
| 365 | + tmpdir, |
| 366 | + {'build': {'image': 'stable'}}, |
| 367 | + ) |
| 368 | + update_docs = self.get_update_docs_task() |
| 369 | + assert update_docs.config.build.image == build_image |
| 370 | + |
| 371 | + def test_custom_build_image(self, checkout_path, tmpdir): |
| 372 | + checkout_path.return_value = str(tmpdir) |
| 373 | + |
| 374 | + build_image = 'readthedocs/build:3.0' |
| 375 | + self.project.container_image = build_image |
| 376 | + self.project.save() |
| 377 | + |
| 378 | + self.create_config_file(tmpdir, {}) |
| 379 | + update_docs = self.get_update_docs_task() |
| 380 | + assert update_docs.config.build.image == build_image |
| 381 | + |
| 382 | + def test_python_version(self, checkout_path, tmpdir): |
| 383 | + checkout_path.return_value = str(tmpdir) |
| 384 | + self.create_config_file(tmpdir, {}) |
| 385 | + # The default version is always 3 |
| 386 | + self.project.python_interpreter = 'python2' |
| 387 | + self.project.save() |
| 388 | + |
| 389 | + config = self.get_update_docs_task().config |
| 390 | + assert config.python.version == 3 |
| 391 | + assert config.python_full_version == 3.6 |
| 392 | + |
| 393 | + @patch('readthedocs.doc_builder.environments.BuildEnvironment.run') |
| 394 | + def test_python_requirements(self, run, checkout_path, tmpdir): |
| 395 | + checkout_path.return_value = str(tmpdir) |
| 396 | + requirements_file = 'requirements.txt' |
| 397 | + apply_fs(tmpdir, {requirements_file: ''}) |
| 398 | + base_path = self.create_config_file( |
| 399 | + tmpdir, |
| 400 | + { |
| 401 | + 'python': {'requirements': requirements_file} |
| 402 | + } |
| 403 | + ) |
| 404 | + |
| 405 | + update_docs = self.get_update_docs_task() |
| 406 | + config = update_docs.config |
| 407 | + |
| 408 | + python_env = Virtualenv( |
| 409 | + version=self.version, |
| 410 | + build_env=update_docs.build_env, |
| 411 | + config=config |
| 412 | + ) |
| 413 | + update_docs.python_env = python_env |
| 414 | + update_docs.python_env.install_user_requirements() |
| 415 | + |
| 416 | + args, kwargs = run.call_args |
| 417 | + requirements_file = path.join(str(base_path), requirements_file) |
| 418 | + |
| 419 | + assert config.python.requirements == requirements_file |
| 420 | + assert requirements_file in args |
| 421 | + |
| 422 | + @patch('readthedocs.doc_builder.environments.BuildEnvironment.run') |
| 423 | + def test_python_install_setup(self, run, checkout_path, tmpdir): |
| 424 | + checkout_path.return_value = str(tmpdir) |
| 425 | + self.create_config_file( |
| 426 | + tmpdir, |
| 427 | + { |
| 428 | + 'python': {'install': 'setup.py'} |
| 429 | + } |
| 430 | + ) |
| 431 | + |
| 432 | + update_docs = self.get_update_docs_task() |
| 433 | + config = update_docs.config |
| 434 | + |
| 435 | + python_env = Virtualenv( |
| 436 | + version=self.version, |
| 437 | + build_env=update_docs.build_env, |
| 438 | + config=config |
| 439 | + ) |
| 440 | + update_docs.python_env = python_env |
| 441 | + update_docs.python_env.install_package() |
| 442 | + |
| 443 | + args, kwargs = run.call_args |
| 444 | + |
| 445 | + assert 'setup.py' in args |
| 446 | + assert 'install' in args |
| 447 | + assert config.python.install_with_setup |
| 448 | + assert not config.python.install_with_pip |
| 449 | + |
| 450 | + @patch('readthedocs.doc_builder.environments.BuildEnvironment.run') |
| 451 | + def test_python_install_pip(self, run, checkout_path, tmpdir): |
| 452 | + checkout_path.return_value = str(tmpdir) |
| 453 | + self.create_config_file( |
| 454 | + tmpdir, |
| 455 | + { |
| 456 | + 'python': {'install': 'pip'} |
| 457 | + } |
| 458 | + ) |
| 459 | + |
| 460 | + update_docs = self.get_update_docs_task() |
| 461 | + config = update_docs.config |
| 462 | + |
| 463 | + python_env = Virtualenv( |
| 464 | + version=self.version, |
| 465 | + build_env=update_docs.build_env, |
| 466 | + config=config |
| 467 | + ) |
| 468 | + update_docs.python_env = python_env |
| 469 | + update_docs.python_env.install_package() |
| 470 | + |
| 471 | + args, kwargs = run.call_args |
| 472 | + |
| 473 | + assert 'setup.py' not in args |
| 474 | + assert 'install' in args |
| 475 | + assert config.python.install_with_pip |
| 476 | + assert not config.python.install_with_setup |
| 477 | + |
| 478 | + def test_python_install_project(self, checkout_path, tmpdir): |
| 479 | + checkout_path.return_value = str(tmpdir) |
| 480 | + self.create_config_file(tmpdir, {}) |
| 481 | + |
| 482 | + self.project.install_project = True |
| 483 | + self.project.save() |
| 484 | + |
| 485 | + config = self.get_update_docs_task().config |
| 486 | + |
| 487 | + assert config.python.install_with_setup |
| 488 | + assert not config.python.install_with_pip |
0 commit comments