Debian Codegraph @

mkdocs/tests/theme_tests.py

raw
1 import os
2 import unittest
3 from unittest import mock
4
5 import mkdocs
6 from mkdocs.localization import parse_locale
7 from mkdocs.tests.base import tempdir
8 from mkdocs.theme import Theme
9
10 abs_path = os.path.abspath(os.path.dirname(__file__))
11 mkdocs_dir = os.path.abspath(os.path.dirname(mkdocs.__file__))
12 mkdocs_templates_dir = os.path.join(mkdocs_dir, 'templates')
13 theme_dir = os.path.abspath(os.path.join(mkdocs_dir, 'themes'))
14
15
16 class ThemeTests(unittest.TestCase):
17 def test_simple_theme(self):
18 theme = Theme(name='mkdocs')
19 self.assertEqual(
20 theme.dirs,
21 [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir],
22 )
23 self.assertEqual(theme.static_templates, {'404.html', 'sitemap.xml'})
24 self.assertEqual(
25 dict(theme),
26 {
27 'name': 'mkdocs',
28 'color_mode': 'light',
29 'user_color_mode_toggle': False,
30 'locale': parse_locale('en'),
31 'include_search_page': False,
32 'search_index_only': False,
33 'analytics': {'gtag': None},
34 'highlightjs': True,
35 'hljs_style': 'github',
36 'hljs_style_dark': 'github-dark',
37 'hljs_languages': [],
38 'navigation_depth': 2,
39 'nav_style': 'primary',
40 'shortcuts': {'help': 191, 'next': 78, 'previous': 80, 'search': 83},
41 },
42 )
43
44 @tempdir()
45 def test_custom_dir(self, custom):
46 theme = Theme(name='mkdocs', custom_dir=custom)
47 self.assertEqual(
48 theme.dirs,
49 [
50 custom,
51 os.path.join(theme_dir, 'mkdocs'),
52 mkdocs_templates_dir,
53 ],
54 )
55
56 @tempdir()
57 def test_custom_dir_only(self, custom):
58 theme = Theme(name=None, custom_dir=custom)
59 self.assertEqual(
60 theme.dirs,
61 [custom, mkdocs_templates_dir],
62 )
63
64 def static_templates(self):
65 theme = Theme(name='mkdocs', static_templates='foo.html')
66 self.assertEqual(
67 theme.static_templates,
68 {'404.html', 'sitemap.xml', 'foo.html'},
69 )
70
71 def test_vars(self):
72 theme = Theme(name='mkdocs', foo='bar', baz=True)
73 self.assertEqual(theme['foo'], 'bar')
74 self.assertEqual(theme['baz'], True)
75 self.assertTrue('new' not in theme)
76 with self.assertRaises(KeyError):
77 theme['new']
78 theme['new'] = 42
79 self.assertTrue('new' in theme)
80 self.assertEqual(theme['new'], 42)
81
82 @mock.patch('yaml.load', return_value={})
83 def test_no_theme_config(self, m):
84 theme = Theme(name='mkdocs')
85 self.assertEqual(m.call_count, 1)
86 self.assertEqual(theme.static_templates, {'sitemap.xml'})
87
88 def test_inherited_theme(self):
89 m = mock.Mock(
90 side_effect=[
91 {'extends': 'readthedocs', 'static_templates': ['child.html']},
92 {'static_templates': ['parent.html']},
93 ]
94 )
95 with mock.patch('yaml.load', m) as m:
96 theme = Theme(name='mkdocs')
97 self.assertEqual(m.call_count, 2)
98 self.assertEqual(
99 theme.dirs,
100 [
101 os.path.join(theme_dir, 'mkdocs'),
102 os.path.join(theme_dir, 'readthedocs'),
103 mkdocs_templates_dir,
104 ],
105 )
106 self.assertEqual(theme.static_templates, {'sitemap.xml', 'child.html', 'parent.html'})
107
108 def test_empty_config_file(self):
109 # Test for themes with *empty* mkdocs_theme.yml.
110 # See https://github.com/mkdocs/mkdocs/issues/3699
111 m = mock.Mock(
112 # yaml.load returns "None" for an empty file
113 side_effect=[None]
114 )
115 with mock.patch('yaml.load', m) as m:
116 theme = Theme(name='mkdocs')
117 # Should only have the default name and locale __vars set in
118 # Theme.__init__()
119 self.assertEqual(
120 dict(theme),
121 {
122 'name': 'mkdocs',
123 'locale': parse_locale('en'),
124 },
125 )