theme_tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os
  2. import tempfile
  3. import unittest
  4. from unittest import mock
  5. import mkdocs
  6. from mkdocs.theme import Theme
  7. abs_path = os.path.abspath(os.path.dirname(__file__))
  8. mkdocs_dir = os.path.abspath(os.path.dirname(mkdocs.__file__))
  9. mkdocs_templates_dir = os.path.join(mkdocs_dir, 'templates')
  10. theme_dir = os.path.abspath(os.path.join(mkdocs_dir, 'themes'))
  11. def get_vars(theme):
  12. """ Return dict of theme vars. """
  13. return {k: theme[k] for k in iter(theme)}
  14. class ThemeTests(unittest.TestCase):
  15. def test_simple_theme(self):
  16. theme = Theme(name='mkdocs')
  17. self.assertEqual(
  18. theme.dirs,
  19. [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir]
  20. )
  21. self.assertEqual(theme.static_templates, {'404.html', 'sitemap.xml'})
  22. self.assertEqual(get_vars(theme), {
  23. 'include_search_page': False,
  24. 'search_index_only': False,
  25. 'highlightjs': True,
  26. 'hljs_style': 'github',
  27. 'hljs_languages': [],
  28. 'navigation_depth': 2,
  29. 'nav_style': 'primary',
  30. 'shortcuts': {'help': 191, 'next': 78, 'previous': 80, 'search': 83}
  31. })
  32. def test_custom_dir(self):
  33. custom = tempfile.mkdtemp()
  34. theme = Theme(name='mkdocs', custom_dir=custom)
  35. self.assertEqual(
  36. theme.dirs,
  37. [
  38. custom,
  39. os.path.join(theme_dir, 'mkdocs'),
  40. mkdocs_templates_dir
  41. ]
  42. )
  43. def test_custom_dir_only(self):
  44. custom = tempfile.mkdtemp()
  45. theme = Theme(name=None, custom_dir=custom)
  46. self.assertEqual(
  47. theme.dirs,
  48. [custom, mkdocs_templates_dir]
  49. )
  50. def static_templates(self):
  51. theme = Theme(name='mkdocs', static_templates='foo.html')
  52. self.assertEqual(
  53. theme.static_templates,
  54. {'404.html', 'sitemap.xml', 'foo.html'}
  55. )
  56. def test_vars(self):
  57. theme = Theme(name='mkdocs', foo='bar', baz=True)
  58. self.assertEqual(theme['foo'], 'bar')
  59. self.assertEqual(theme['baz'], True)
  60. self.assertTrue('new' not in theme)
  61. self.assertRaises(KeyError, lambda t, k: t[k], theme, 'new')
  62. theme['new'] = 42
  63. self.assertTrue('new' in theme)
  64. self.assertEqual(theme['new'], 42)
  65. @mock.patch('mkdocs.utils.yaml_load', return_value=None)
  66. def test_no_theme_config(self, m):
  67. theme = Theme(name='mkdocs')
  68. self.assertEqual(m.call_count, 1)
  69. self.assertEqual(theme.static_templates, {'sitemap.xml'})
  70. def test_inherited_theme(self):
  71. m = mock.Mock(side_effect=[
  72. {'extends': 'readthedocs', 'static_templates': ['child.html']},
  73. {'static_templates': ['parent.html']}
  74. ])
  75. with mock.patch('mkdocs.utils.yaml_load', m) as m:
  76. theme = Theme(name='mkdocs')
  77. self.assertEqual(m.call_count, 2)
  78. self.assertEqual(
  79. theme.dirs,
  80. [
  81. os.path.join(theme_dir, 'mkdocs'),
  82. os.path.join(theme_dir, 'readthedocs'),
  83. mkdocs_templates_dir
  84. ]
  85. )
  86. self.assertEqual(
  87. theme.static_templates, {'sitemap.xml', 'child.html', 'parent.html'}
  88. )