Skip to content

Commit

Permalink
Tests with real files for requirements_file and conda_file
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Aug 29, 2018
1 parent dde104d commit 773cbff
Showing 1 changed file with 77 additions and 19 deletions.
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')
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.
"""
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()
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')

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.skip
Expand Down

0 comments on commit 773cbff

Please sign in to comment.