test_sphinx_idf_extensions.py 4.9 KB

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