Coverage for tests/test_plugin.py: 100.00%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-14 19:41 +0100

1"""Tests for the mkdocstrings plugin.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7from mkdocs.commands.build import build 

8from mkdocs.config import load_config 

9 

10from mkdocstrings.plugin import MkdocstringsPlugin 

11 

12if TYPE_CHECKING: 

13 from pathlib import Path 

14 

15 

16def test_disabling_plugin(tmp_path: Path) -> None: 

17 """Test disabling plugin.""" 

18 docs_dir = tmp_path / "docs" 

19 site_dir = tmp_path / "site" 

20 docs_dir.mkdir() 

21 site_dir.mkdir() 

22 docs_dir.joinpath("index.md").write_text("::: mkdocstrings") 

23 

24 mkdocs_config = load_config() 

25 mkdocs_config["docs_dir"] = str(docs_dir) 

26 mkdocs_config["site_dir"] = str(site_dir) 

27 mkdocs_config["plugins"]["mkdocstrings"].config["enabled"] = False 

28 mkdocs_config["plugins"].run_event("startup", command="build", dirty=False) 

29 try: 

30 build(mkdocs_config) 

31 finally: 

32 mkdocs_config["plugins"].run_event("shutdown") 

33 

34 # make sure the instruction was not processed 

35 assert "::: mkdocstrings" in site_dir.joinpath("index.html").read_text() 

36 

37 

38def test_plugin_default_config(tmp_path: Path) -> None: 

39 """Test default config options are set for Plugin.""" 

40 config_file_path = tmp_path / "mkdocs.yml" 

41 plugin = MkdocstringsPlugin() 

42 errors, warnings = plugin.load_config({}, config_file_path=str(config_file_path)) 

43 assert errors == [] 

44 assert warnings == [] 

45 assert plugin.config == { 

46 "handlers": {}, 

47 "default_handler": "python", 

48 "custom_templates": None, 

49 "enable_inventory": None, 

50 "enabled": True, 

51 } 

52 

53 

54def test_plugin_config_custom_templates(tmp_path: Path) -> None: 

55 """Test custom_templates option is relative to config file.""" 

56 config_file_path = tmp_path / "mkdocs.yml" 

57 options = {"custom_templates": "docs/templates"} 

58 template_dir = tmp_path / options["custom_templates"] 

59 # Path must exist or config validation will fail. 

60 template_dir.mkdir(parents=True) 

61 plugin = MkdocstringsPlugin() 

62 errors, warnings = plugin.load_config(options, config_file_path=str(config_file_path)) 

63 assert errors == [] 

64 assert warnings == [] 

65 assert plugin.config == { 

66 "handlers": {}, 

67 "default_handler": "python", 

68 "custom_templates": str(template_dir), 

69 "enable_inventory": None, 

70 "enabled": True, 

71 }