Skip to content

Commit

Permalink
Validate same doctype (web and config file)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Sep 17, 2018
1 parent 6ad9ffd commit 80e3904
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
19 changes: 19 additions & 0 deletions readthedocs/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ def validate(self):
self.validate_doc_types()
self._config['mkdocs'] = self.validate_mkdocs()
self._config['sphinx'] = self.validate_sphinx()
# TODO: remove later
self.validate_final_doc_type()
self._config['submodules'] = self.validate_submodules()

def validate_formats(self):
Expand Down Expand Up @@ -813,6 +815,23 @@ def validate_sphinx(self):

return sphinx

def validate_final_doc_type(self):
"""
Validates that the doctype is the same as the admin panel.
This a temporal validation, as the configuration file
should support per version doctype, but we need to
adapt the rtd code for that.
"""
if self.doctype != self.defaults.get('doctype', 'sphinx'):
key = 'mkdocs' if self.doctype == 'mkdocs' else 'sphinx'
self.error(
key,
'Your documentation type should match '
'the one from the admin panel of your project.',
code=INVALID_KEYS_COMBINATION,
)

def validate_submodules(self):
"""
Validates the submodules key.
Expand Down
46 changes: 39 additions & 7 deletions readthedocs/config/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,10 @@ def test_sphinx_is_default_doc_type(self):
('htmldir', 'sphinx_htmldir'),
('singlehtml', 'sphinx_singlehtml')])
def test_sphinx_builder_check_valid(self, value, expected):
build = self.get_build_config({'sphinx': {'builder': value}})
build = self.get_build_config(
{'sphinx': {'builder': value}},
{'defaults': {'doctype': expected}},
)
build.validate()
assert build.sphinx.builder == expected
assert build.doctype == expected
Expand All @@ -1333,6 +1336,7 @@ def test_sphinx_builder_default(self):
build.validate()
build.sphinx.builder == 'sphinx'

@pytest.mark.skip
def test_sphinx_builder_ignores_default(self):
build = self.get_build_config(
{},
Expand Down Expand Up @@ -1454,6 +1458,7 @@ def test_mkdocs_configuration_check_valid(self, tmpdir):
apply_fs(tmpdir, {'mkdocs.yml': ''})
build = self.get_build_config(
{'mkdocs': {'configuration': 'mkdocs.yml'}},
{'defaults': {'doctype': 'mkdocs'}},
source_file=str(tmpdir.join('readthedocs.yml')),
)
build.validate()
Expand All @@ -1472,40 +1477,67 @@ def test_mkdocs_configuration_check_invalid(self, tmpdir):
assert excinfo.value.key == 'mkdocs.configuration'

def test_mkdocs_configuration_allow_null(self):
build = self.get_build_config({'mkdocs': {'configuration': None}},)
build = self.get_build_config(
{'mkdocs': {'configuration': None}},
{'defaults': {'doctype': 'mkdocs'}},
)
build.validate()
assert build.mkdocs.configuration is None

def test_mkdocs_configuration_check_default(self):
build = self.get_build_config({'mkdocs': {}})
build = self.get_build_config(
{'mkdocs': {}},
{'defaults': {'doctype': 'mkdocs'}},
)
build.validate()
assert build.mkdocs.configuration is None

@pytest.mark.parametrize('value', [[], True, 0, {}])
def test_mkdocs_configuration_validate_type(self, value):
build = self.get_build_config({'mkdocs': {'configuration': value}},)
build = self.get_build_config(
{'mkdocs': {'configuration': value}},
{'defaults': {'doctype': 'mkdocs'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'mkdocs.configuration'

@pytest.mark.parametrize('value', [True, False])
def test_mkdocs_fail_on_warning_check_valid(self, value):
build = self.get_build_config({'mkdocs': {'fail_on_warning': value}})
build = self.get_build_config(
{'mkdocs': {'fail_on_warning': value}},
{'defaults': {'doctype': 'mkdocs'}},
)
build.validate()
assert build.mkdocs.fail_on_warning is value

@pytest.mark.parametrize('value', [[], 'invalid', 5])
def test_mkdocs_fail_on_warning_check_invalid(self, value):
build = self.get_build_config({'mkdocs': {'fail_on_warning': value}})
build = self.get_build_config(
{'mkdocs': {'fail_on_warning': value}},
{'defaults': {'doctype': 'mkdocs'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'mkdocs.fail_on_warning'

def test_mkdocs_fail_on_warning_check_default(self):
build = self.get_build_config({'mkdocs': {}})
build = self.get_build_config(
{'mkdocs': {}},
{'defaults': {'doctype': 'mkdocs'}},
)
build.validate()
assert build.mkdocs.fail_on_warning is False

def test_validates_different_filetype(self):
build = self.get_build_config(
{'mkdocs': {}},
{'defaults': {'doctype': 'sphinx'}},
)
with raises(InvalidConfig) as excinfo:
build.validate()
assert excinfo.value.key == 'mkdocs'

@pytest.mark.parametrize('value', [[], 'invalid', 0])
def test_submodules_check_invalid_type(self, value):
build = self.get_build_config({'submodules': value})
Expand Down
7 changes: 6 additions & 1 deletion readthedocs/rtd_tests/tests/test_config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,15 @@ def test_sphinx_builder(
checkout_path.return_value = str(tmpdir)
self.create_config_file(tmpdir, {'sphinx': {'builder': value}})

self.project.documentation_type = 'mkdocs'
self.project.documentation_type = result
self.project.save()

update_docs = self.get_update_docs_task()
update_docs.build_docs_html()

get_builder_class.assert_called_with(result)

@pytest.mark.skip
@patch('readthedocs.projects.tasks.get_builder_class')
def test_sphinx_builder_default(
self, get_builder_class, checkout_path, tmpdir):
Expand Down Expand Up @@ -771,6 +772,8 @@ def test_mkdocs_configuration(
},
}
)
self.project.documentation_type = 'mkdocs'
self.project.save()

update_docs = self.get_update_docs_task()
config = update_docs.config
Expand Down Expand Up @@ -809,6 +812,8 @@ def test_mkdocs_fail_on_warning(
},
}
)
self.project.documentation_type = 'mkdocs'
self.project.save()

update_docs = self.get_update_docs_task()
config = update_docs.config
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/rtd_tests/tests/test_doc_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from mock import patch

from readthedocs.builds.models import Version
from readthedocs.doc_builder.backends.mkdocs import BaseMkdocs, MkdocsHTML
from readthedocs.doc_builder.backends.mkdocs import MkdocsHTML
from readthedocs.doc_builder.backends.sphinx import BaseSphinx
from readthedocs.doc_builder.python_environments import Virtualenv
from readthedocs.projects.exceptions import ProjectConfigurationError
Expand Down

0 comments on commit 80e3904

Please sign in to comment.