Skip to content

More robusts tests for requirements_file and conda_file #4394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 77 additions & 19 deletions readthedocs/rtd_tests/tests/test_config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import (
absolute_import, division, print_function, unicode_literals)

import tempfile
from os import path

import mock
Expand Down Expand Up @@ -53,18 +54,30 @@ def inner(path=None, env_config=None):
return inner


@mock.patch('readthedocs.doc_builder.config.load_config')
Copy link
Member Author

@stsewd stsewd Jul 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to remove the class level mock and only used it in some methods

def create_config_file(config, file_name='readthedocs.yml', base_path=None):
"""
Creates a readthedocs configuration file with name
``file_name`` in ``base_path``. If ``base_path`` is not given
a temporal directory is created.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not to change now, but just to consider that PEP257 (https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings) says that we need one summary line, a blank line, and a more elaborate description.

I know that you love following PEPs ;)

NOTE: this is checked automatically by one of our linters and it will fail in that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, yeah

"""
if not base_path:
base_path = tempfile.mkdtemp()
full_path = path.join(base_path, file_name)
yaml.safe_dump(config, open(full_path, 'w'))
return full_path


class LoadConfigTests(TestCase):

def setUp(self):
self.project = get(
Project,
main_language_project=None,
install_project=False,
requirements_file='__init__.py'
)
self.version = get(Version, project=self.project)

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_supported_versions_default_image_1_0(self, load_config):
load_config.side_effect = create_load()
self.project.container_image = 'readthedocs/build:1.0'
Expand Down Expand Up @@ -100,6 +113,7 @@ def test_python_supported_versions_default_image_1_0(self, load_config):
])
self.assertEqual(config.python.version, 2)

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_supported_versions_image_1_0(self, load_config):
load_config.side_effect = create_load()
self.project.container_image = 'readthedocs/build:1.0'
Expand All @@ -108,6 +122,7 @@ def test_python_supported_versions_image_1_0(self, load_config):
self.assertEqual(config.get_valid_python_versions(),
[2, 2.7, 3, 3.4])

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_supported_versions_image_2_0(self, load_config):
load_config.side_effect = create_load()
self.project.container_image = 'readthedocs/build:2.0'
Expand All @@ -116,6 +131,7 @@ def test_python_supported_versions_image_2_0(self, load_config):
self.assertEqual(config.get_valid_python_versions(),
[2, 2.7, 3, 3.5])

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_supported_versions_image_latest(self, load_config):
load_config.side_effect = create_load()
self.project.container_image = 'readthedocs/build:latest'
Expand All @@ -124,12 +140,14 @@ def test_python_supported_versions_image_latest(self, load_config):
self.assertEqual(config.get_valid_python_versions(),
[2, 2.7, 3, 3.3, 3.4, 3.5, 3.6])

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_default_version(self, load_config):
load_config.side_effect = create_load()
config = load_yaml_config(self.version)
self.assertEqual(config.python.version, 2)
self.assertEqual(config.python_interpreter, 'python2.7')

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_set_python_version_on_project(self, load_config):
load_config.side_effect = create_load()
self.project.container_image = 'readthedocs/build:2.0'
Expand All @@ -139,6 +157,7 @@ def test_python_set_python_version_on_project(self, load_config):
self.assertEqual(config.python.version, 3)
self.assertEqual(config.python_interpreter, 'python3.5')

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_set_python_version_in_config(self, load_config):
load_config.side_effect = create_load({
'python': {'version': 3.5},
Expand All @@ -149,6 +168,7 @@ def test_python_set_python_version_in_config(self, load_config):
self.assertEqual(config.python.version, 3.5)
self.assertEqual(config.python_interpreter, 'python3.5')

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_python_invalid_version_in_config(self, load_config):
load_config.side_effect = create_load({
'python': {'version': 2.6}
Expand All @@ -158,6 +178,7 @@ def test_python_invalid_version_in_config(self, load_config):
with self.assertRaises(InvalidConfig):
load_yaml_config(self.version)

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_install_project(self, load_config):
load_config.side_effect = create_load()
config = load_yaml_config(self.version)
Expand All @@ -172,6 +193,7 @@ def test_install_project(self, load_config):
config = load_yaml_config(self.version)
self.assertEqual(config.python.install_with_setup, True)

@mock.patch('readthedocs.doc_builder.config.load_config')
def test_extra_requirements(self, load_config):
load_config.side_effect = create_load({
'python': {
Expand Down Expand Up @@ -203,33 +225,69 @@ def test_extra_requirements(self, load_config):
config = load_yaml_config(self.version)
self.assertEqual(config.python.extra_requirements, [])

def test_conda(self, load_config):
to_find = '__init__.py'
load_config.side_effect = create_load({
'conda': {
'file': to_find
}
})
@mock.patch('readthedocs.projects.models.Project.checkout_path')
def test_conda_with_cofig(self, checkout_path):
base_path = tempfile.mkdtemp()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to reuse the apply_fs function (only works with pytest) or fake_paths as the check is very strict.

checkout_path.return_value = base_path
conda_file = 'environmemt.yml'
full_conda_file = path.join(base_path, conda_file)
with open(full_conda_file, 'w') as f:
f.write('conda')
create_config_file(
{
'conda': {
'file': conda_file,
}
},
base_path=base_path,
)
config = load_yaml_config(self.version)
self.assertTrue(config.conda is not None)
self.assertTrue(config.conda.environment[-len(to_find):] == to_find)
self.assertEqual(config.conda.environment, full_conda_file)

load_config.side_effect = create_load()
@mock.patch('readthedocs.projects.models.Project.checkout_path')
def test_conda_without_cofig(self, checkout_path):
base_path = tempfile.mkdtemp()
checkout_path.return_value = base_path
config = load_yaml_config(self.version)
self.assertIsNone(config.conda)

def test_requirements_file(self, load_config):
requirements_file = '__init__.py'
load_config.side_effect = create_load({
'requirements_file': requirements_file
})
@mock.patch('readthedocs.projects.models.Project.checkout_path')
def test_requirements_file_from_project_setting(self, checkout_path):
base_path = tempfile.mkdtemp()
checkout_path.return_value = base_path

requirements_file = 'requirements.txt'
self.project.requirements_file = requirements_file
self.project.save()

full_requirements_file = path.join(base_path, requirements_file)
with open(full_requirements_file, 'w') as f:
f.write('pip')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to create this file at all for the test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the validation is very strict, and it checks that the file exists, I wasn't able to mock this one. But I'm using pytest for our new tests, and we don't need to use this hacks anymore


config = load_yaml_config(self.version)
self.assertEqual(config.python.requirements, requirements_file)

# Respects the requirements file from the project settings
load_config.side_effect = create_load()
@mock.patch('readthedocs.projects.models.Project.checkout_path')
def test_requirements_file_from_yml(self, checkout_path):
base_path = tempfile.mkdtemp()
checkout_path.return_value = base_path

self.project.requirements_file = 'no-existent-file.txt'
self.project.save()

requirements_file = 'requirements.txt'
full_requirements_file = path.join(base_path, requirements_file)
with open(full_requirements_file, 'w') as f:
f.write('pip')
create_config_file(
{
'requirements_file': requirements_file,
},
base_path=base_path,
)
config = load_yaml_config(self.version)
self.assertEqual(config.python.requirements, '__init__.py')
self.assertEqual(config.python.requirements, requirements_file)


@pytest.mark.django_db
Expand Down