Skip to content

Commit 0585d1d

Browse files
authored
Merge pull request #5569 from saadmk11/validate-docs-dir
Validate docs dir before writing custom js
2 parents 77ef333 + 69bcd82 commit 0585d1d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

readthedocs/doc_builder/backends/mkdocs.py

+7
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def append_conf(self, **__):
119119
)
120120

121121
docs_dir = self.docs_dir(docs_dir=user_docs_dir)
122+
122123
self.create_index(extension='md')
123124
user_config['docs_dir'] = docs_dir
124125

@@ -161,6 +162,12 @@ def append_conf(self, **__):
161162
docs_dir,
162163
)
163164

165+
# if user puts an invalid `docs_dir` path raise an Exception
166+
if not os.path.exists(docs_path):
167+
raise MkDocsYAMLParseError(
168+
MkDocsYAMLParseError.INVALID_DOCS_DIR_PATH,
169+
)
170+
164171
# RTD javascript writing
165172
rtd_data = self.generate_rtd_data(
166173
docs_dir=os.path.relpath(docs_path, self.root_path),

readthedocs/doc_builder/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class MkDocsYAMLParseError(BuildEnvironmentError):
6767
'string with relative or absolute path.',
6868
)
6969

70+
INVALID_DOCS_DIR_PATH = ugettext_noop(
71+
'The "docs_dir" config from your MkDocs YAML config file does not '
72+
'contain a valid path.',
73+
)
74+
7075
INVALID_EXTRA_CONFIG = ugettext_noop(
7176
'The "{config}" config from your MkDocs YAML config file has to be a '
7277
'a list of relative paths.',

readthedocs/rtd_tests/tests/test_doc_builder.py

+32
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,38 @@ def test_write_js_data_docs_dir(self, checkout_path, run, generate_rtd_data):
488488
mkdocs_config=mock.ANY,
489489
)
490490

491+
@patch('readthedocs.doc_builder.backends.mkdocs.BaseMkdocs.generate_rtd_data')
492+
@patch('readthedocs.projects.models.Project.checkout_path')
493+
def test_write_js_data_on_invalid_docs_dir(self, checkout_path, generate_rtd_data):
494+
tmpdir = tempfile.mkdtemp()
495+
os.mkdir(os.path.join(tmpdir, 'docs'))
496+
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
497+
yaml.safe_dump(
498+
{
499+
'site_name': 'mkdocs',
500+
'google_analytics': ['UA-1234-5', 'mkdocs.org'],
501+
'docs_dir': 'invalid_docs_dir',
502+
'extra_css': [
503+
'http://readthedocs.org/static/css/badge_only.css'
504+
],
505+
'extra_javascript': ['readthedocs-data.js'],
506+
},
507+
open(yaml_file, 'w'),
508+
)
509+
checkout_path.return_value = tmpdir
510+
511+
python_env = Virtualenv(
512+
version=self.version,
513+
build_env=self.build_env,
514+
config=None,
515+
)
516+
self.searchbuilder = MkdocsHTML(
517+
build_env=self.build_env,
518+
python_env=python_env,
519+
)
520+
with self.assertRaises(MkDocsYAMLParseError):
521+
self.searchbuilder.append_conf()
522+
491523
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
492524
@patch('readthedocs.projects.models.Project.checkout_path')
493525
def test_append_conf_existing_yaml_with_extra(self, checkout_path, run):

0 commit comments

Comments
 (0)