Skip to content

Commit 7e4892a

Browse files
authored
Fix documentation of draft_docs and exclude_docs, add tests (#3859)
1 parent bb7e8b6 commit 7e4892a

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

docs/user-guide/configuration.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,17 @@ Example:
305305

306306
```yaml
307307
exclude_docs: |
308-
api-config.json # A file with this name anywhere.
309-
/requirements.txt # Top-level "docs/requirements.txt".
310-
*.py # Any file with this extension anywhere.
311-
!/foo/example.py # But keep this particular file.
308+
# A file with this name anywhere.
309+
api-config.json
310+
311+
# Top-level "docs/requirements.txt".
312+
/requirements.txt
313+
314+
# Any file with this extension anywhere.
315+
*.py
316+
317+
# But keep this particular file.
318+
!/foo/example.py
312319
```
313320

314321
This follows the [.gitignore pattern format](https://git-scm.com/docs/gitignore#_pattern_format).
@@ -327,7 +334,8 @@ Otherwise you could for example opt only certain dot-files back into the site:
327334

328335
```yaml
329336
exclude_docs: |
330-
!.assets # Don't exclude '.assets' although all other '.*' are excluded
337+
# Don't exclude '.assets' although all other '.*' are excluded
338+
!.assets
331339
```
332340

333341
### draft_docs
@@ -340,9 +348,14 @@ Example:
340348

341349
```yaml
342350
draft_docs: |
343-
drafts/ # A "drafts" directory anywhere.
344-
_unpublished.md # A md file ending in _unpublished.md
345-
!/foo_unpublished.md # But keep this particular file.
351+
# A "drafts" directory anywhere.
352+
drafts/
353+
354+
# A Markdown file ending in _unpublished.md anywhere.
355+
*_unpublished.md
356+
357+
# But keep this particular file.
358+
!/foo_unpublished.md
346359
```
347360

348361
This follows the [.gitignore pattern format](https://git-scm.com/docs/gitignore#_pattern_format).

mkdocs/tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import unittest.util
33

4-
unittest.util._MAX_LENGTH = 100000
4+
unittest.util._MAX_LENGTH = 100000 # type: ignore[misc]
55

66

77
class DisallowLogsHandler(logging.Handler):

mkdocs/tests/build_tests.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,66 @@ def _assert_build_logs(self, expected):
571571
del msgs[-1]
572572
self.assertEqual('\n'.join(msgs), textwrap.dedent(expected).strip('\n'))
573573

574+
@tempdir(
575+
files={
576+
'foo_unpublished.md': 'unpublished content to include anyway',
577+
'other_unpublished.md': 'unpublished content',
578+
'normal_file.md': 'should not be affected',
579+
'test/other_unpublished.md': 'more unpublished content',
580+
'test/normal_file.md': 'should not be affected',
581+
}
582+
)
583+
@tempdir()
584+
def test_draft_docs_with_comments_from_user_guide(self, site_dir, docs_dir):
585+
cfg = load_config(
586+
docs_dir=docs_dir,
587+
site_dir=site_dir,
588+
use_directory_urls=False,
589+
draft_docs='''
590+
# A "drafts" directory anywhere.
591+
drafts/
592+
593+
# A Markdown file ending in _unpublished.md anywhere.
594+
*_unpublished.md
595+
596+
# But keep this particular file.
597+
!/foo_unpublished.md
598+
''',
599+
)
600+
601+
with self.subTest(serve_url=None):
602+
build.build(cfg)
603+
self.assertPathIsFile(site_dir, 'foo_unpublished.html')
604+
self.assertPathIsFile(site_dir, 'normal_file.html')
605+
self.assertPathIsFile(site_dir, 'test', 'normal_file.html')
606+
self.assertPathNotExists(site_dir, 'other_unpublished.html')
607+
self.assertPathNotExists(site_dir, 'test', 'other_unpublished.html')
608+
609+
serve_url = 'http://localhost:123/documentation/'
610+
with self.subTest(serve_url=serve_url):
611+
expected_logs = '''
612+
INFO:The following pages are being built only for the preview but will be excluded from `mkdocs build` per `draft_docs` config:
613+
- http://localhost:123/documentation/other_unpublished.html
614+
- http://localhost:123/documentation/test/other_unpublished.html
615+
'''
616+
with self._assert_build_logs(expected_logs):
617+
build.build(cfg, serve_url=serve_url)
618+
619+
top_other_path = Path(site_dir, 'other_unpublished.html')
620+
self.assertTrue(top_other_path.is_file())
621+
self.assertIn('DRAFT', top_other_path.read_text())
622+
623+
sub_other_path = Path(site_dir, 'test', 'other_unpublished.html')
624+
self.assertPathIsFile(sub_other_path)
625+
self.assertIn('DRAFT', sub_other_path.read_text())
626+
627+
self.assertPathIsFile(site_dir, 'foo_unpublished.html')
628+
self.assertPathIsFile(site_dir, 'normal_file.html')
629+
630+
good_path = Path(site_dir, 'test', 'normal_file.html')
631+
self.assertPathIsFile(good_path)
632+
self.assertNotIn('DRAFT', good_path.read_text())
633+
574634
@tempdir(
575635
files={
576636
'test/foo.md': 'page1 content, [bar](bar.md)',

0 commit comments

Comments
 (0)