Skip to content

Validate docs dir before writing custom js #5569

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 5 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions readthedocs/doc_builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import shutil
from functools import wraps

from readthedocs.doc_builder.exceptions import MkDocsYAMLParseError


log = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,6 +86,13 @@ def clean(self, **__):
def docs_dir(self, docs_dir=None, **__):
"""Handle creating a custom docs_dir if it doesn't exist."""
checkout_path = self.project.checkout_path(self.version.slug)
if docs_dir:
possible_path = os.path.join(checkout_path, docs_dir)
# if user puts an invalid `docs_dir` path raise Exception
if not os.path.exists(possible_path):
raise MkDocsYAMLParseError(
MkDocsYAMLParseError.INVALID_DOCS_DIR_PATH,
)
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't put special cases in the base class, the base class should know nothing about mkdocs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it :)

Copy link
Member Author

Choose a reason for hiding this comment

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

@stsewd Updated the PR

if not docs_dir:
for doc_dir_name in ['docs', 'doc', 'Doc', 'book']:
possible_path = os.path.join(checkout_path, doc_dir_name)
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/doc_builder/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class MkDocsYAMLParseError(BuildEnvironmentError):
'string with relative or absolute path.',
)

INVALID_DOCS_DIR_PATH = ugettext_noop(
'The "docs_dir" config from your MkDocs YAML config file does not '
'contain a valid path.',
)

INVALID_EXTRA_CONFIG = ugettext_noop(
'The "{config}" config from your MkDocs YAML config file has to be a '
'a list of relative paths.',
Expand Down
36 changes: 36 additions & 0 deletions readthedocs/rtd_tests/tests/test_doc_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,42 @@ def test_write_js_data_docs_dir(self, checkout_path, run, generate_rtd_data):
mkdocs_config=mock.ANY,
)

@patch('readthedocs.doc_builder.backends.mkdocs.BaseMkdocs.generate_rtd_data')
@patch('readthedocs.projects.models.Project.checkout_path')
def test_write_js_data_on_invalid_docs_dir(self, checkout_path, generate_rtd_data):
tmpdir = tempfile.mkdtemp()
os.mkdir(os.path.join(tmpdir, 'docs'))
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
yaml.safe_dump(
{
'site_name': 'mkdocs',
'google_analytics': ['UA-1234-5', 'mkdocs.org'],
'docs_dir': 'invalid_docs_dir',
'extra_css': [
'http://readthedocs.org/static/css/badge_only.css'
],
'extra_javascript': ['readthedocs-data.js'],
},
open(yaml_file, 'w'),
)
checkout_path.return_value = tmpdir

python_env = Virtualenv(
version=self.version,
build_env=self.build_env,
config=None,
)
self.searchbuilder = MkdocsHTML(
build_env=self.build_env,
python_env=python_env,
)
with self.assertRaises(MkDocsYAMLParseError):
self.searchbuilder.append_conf()
generate_rtd_data.assert_called_with(
Copy link
Member

Choose a reason for hiding this comment

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

I guess we don't need this assert

Copy link
Member Author

Choose a reason for hiding this comment

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

@stsewd then how should we check if invalid docs_path raises an error or not?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we only need that check. The second assert wouldn't happen anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes that's True. 👍 updated the PR.

docs_dir='docs',
mkdocs_config=mock.ANY,
)

@patch('readthedocs.doc_builder.base.BaseBuilder.run')
@patch('readthedocs.projects.models.Project.checkout_path')
def test_append_conf_existing_yaml_with_extra(self, checkout_path, run):
Expand Down