test_sphinx_idf_extensions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import unittest
  5. from tempfile import TemporaryDirectory
  6. from unittest.mock import MagicMock
  7. from sphinx.util import tags
  8. try:
  9. from idf_extensions import exclude_docs
  10. except ImportError:
  11. sys.path.append('..')
  12. from idf_extensions import exclude_docs
  13. from idf_extensions import format_idf_target, gen_idf_tools_links, link_roles
  14. class TestFormatIdfTarget(unittest.TestCase):
  15. def setUp(self):
  16. self.str_sub = format_idf_target.StringSubstituter()
  17. config = MagicMock()
  18. config.idf_target = 'esp32'
  19. self.str_sub.init_sub_strings(config)
  20. def test_add_subs(self):
  21. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_NAME}'], 'ESP32')
  22. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_PATH_NAME}'], 'esp32')
  23. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_TOOLCHAIN_PREFIX}'], 'xtensa-esp32-elf')
  24. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_CFG_PREFIX}'], 'ESP32')
  25. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_TRM_EN_URL}'],
  26. 'https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf')
  27. self.assertEqual(self.str_sub.substitute_strings['{IDF_TARGET_TRM_CN_URL}'],
  28. 'https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_cn.pdf')
  29. def test_sub(self):
  30. content = ('This is a {IDF_TARGET_NAME}, with {IDF_TARGET_PATH_NAME}/soc.c, compiled with '
  31. '{IDF_TARGET_TOOLCHAIN_PREFIX}-gcc with CONFIG_{IDF_TARGET_CFG_PREFIX}_MULTI_DOC. '
  32. 'TRM can be found at {IDF_TARGET_TRM_EN_URL} or {IDF_TARGET_TRM_CN_URL}')
  33. expected = ('This is a ESP32, with esp32/soc.c, compiled with xtensa-esp32-elf-gcc with CONFIG_ESP32_MULTI_DOC. '
  34. 'TRM can be found at https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf '
  35. 'or https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_cn.pdf')
  36. self.assertEqual(self.str_sub.substitute(content), expected)
  37. def test_local_sub(self):
  38. content = ('{IDF_TARGET_TX_PIN:default="IO3", esp32="IO4", esp32s2="IO5"}'
  39. 'The {IDF_TARGET_NAME} UART {IDF_TARGET_TX_PIN} uses for TX')
  40. expected = 'The ESP32 UART IO4 uses for TX'
  41. self.assertEqual(self.str_sub.substitute(content), expected)
  42. def test_local_sub_default(self):
  43. content = ('{IDF_TARGET_TX_PIN:default="IO3", esp32s2="IO5"}'
  44. 'The {IDF_TARGET_NAME} UART {IDF_TARGET_TX_PIN} uses for TX')
  45. expected = 'The ESP32 UART IO3 uses for TX'
  46. self.assertEqual(self.str_sub.substitute(content), expected)
  47. def test_local_sub_no_default(self):
  48. content = ('{IDF_TARGET_TX_PIN: esp32="IO4", esp32s2="IO5"}'
  49. 'The {IDF_TARGET_NAME} UART {IDF_TARGET_TX_PIN} uses for TX')
  50. self.assertRaises(ValueError, self.str_sub.substitute, content)
  51. class TestExclude(unittest.TestCase):
  52. def setUp(self):
  53. self.app = MagicMock()
  54. self.app.tags = tags.Tags()
  55. self.app.config.conditional_include_dict = {'esp32':['esp32.rst', 'bt.rst'], 'esp32s2':['esp32s2.rst']}
  56. self.app.config.docs_to_build = None
  57. self.app.config.exclude_patterns = []
  58. def test_update_exclude_pattern(self):
  59. self.app.tags.add('esp32')
  60. exclude_docs.update_exclude_patterns(self.app, self.app.config)
  61. docs_to_build = set(self.app.config.conditional_include_dict['esp32'])
  62. # Check that the set of docs to build and the set of docs to exclude do not overlap
  63. self.assertFalse(docs_to_build & set(self.app.config.exclude_patterns))
  64. class TestGenIDFToolLinks(unittest.TestCase):
  65. def setUp(self):
  66. self.app = MagicMock()
  67. self.app.config.build_dir = '_build'
  68. self.app.config.idf_path = os.environ['IDF_PATH']
  69. def test_gen_idf_tool_links(self):
  70. with TemporaryDirectory() as temp_dir:
  71. self.app.config.build_dir = temp_dir
  72. gen_idf_tools_links.generate_idf_tools_links(self.app, None)
  73. self.assertTrue(os.path.isfile(os.path.join(self.app.config.build_dir, 'inc', 'idf-tools-inc.rst')))
  74. class TestLinkRoles(unittest.TestCase):
  75. def test_get_submodules(self):
  76. submod_dict = link_roles.get_submodules()
  77. # Test a known submodule to see if it's in the dict
  78. test_submod_name = 'components/asio/asio'
  79. self.assertIn(test_submod_name, submod_dict)
  80. self.assertIsNotNone(submod_dict[test_submod_name].url)
  81. self.assertIsNotNone(submod_dict[test_submod_name].rev)
  82. self.assertIsNotNone(submod_dict[test_submod_name].url)
  83. if __name__ == '__main__':
  84. unittest.main()