SearchCases.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. """ search test cases from a given file or path """
  4. import copy
  5. import fnmatch
  6. import os
  7. import sys
  8. import types
  9. from typing import List
  10. from . import load_source
  11. class Search:
  12. """
  13. This class is used as a class singleton. all the member functions are `classmethod`
  14. """
  15. TEST_CASE_FILE_PATTERN = '*_test.py'
  16. SUPPORT_REPLICATE_CASES_KEY = ['target']
  17. # this attribute would be modified while running
  18. missing_import_warnings: List[str] = []
  19. @classmethod
  20. def _search_cases_from_file(cls, file_name):
  21. """ get test cases from test case .py file """
  22. print('Try to get cases from: ' + file_name)
  23. test_functions = []
  24. try:
  25. mod = load_source(file_name)
  26. for func in [mod.__getattribute__(x) for x in dir(mod)
  27. if isinstance(mod.__getattribute__(x), types.FunctionType)]:
  28. try:
  29. # test method decorator will add test_method attribute to test function
  30. if func.test_method:
  31. test_functions.append(func)
  32. except AttributeError:
  33. continue
  34. except ImportError as e:
  35. warning_str = 'ImortError: \r\n\tFile:' + file_name + '\r\n\tError:' + str(e)
  36. cls.missing_import_warnings.append(warning_str)
  37. test_functions_out = []
  38. for case in test_functions:
  39. test_functions_out += cls.replicate_case(case)
  40. for i, test_function in enumerate(test_functions_out):
  41. print('\t{}. {} <{}>'.format(i + 1, test_function.case_info['name'], test_function.case_info['target']))
  42. test_function.case_info['app_dir'] = os.path.dirname(file_name)
  43. return test_functions_out
  44. @classmethod
  45. def _search_test_case_files(cls, test_case, file_pattern):
  46. """ search all test case files recursively of a path """
  47. if not os.path.exists(test_case):
  48. raise OSError('test case path not exist')
  49. if os.path.isdir(test_case):
  50. test_case_files = []
  51. for root, _, file_names in os.walk(test_case):
  52. for filename in fnmatch.filter(file_names, file_pattern):
  53. test_case_files.append(os.path.join(root, filename))
  54. else:
  55. test_case_files = [test_case]
  56. return test_case_files
  57. @classmethod
  58. def replicate_case(cls, case):
  59. """
  60. Replicate cases according to its filter values.
  61. If one case has specified filter chip=(ESP32, ESP32C),
  62. it will create 2 cases, one for ESP32 and on for ESP32C.
  63. Once the cases are replicated, it's easy to filter those we want to execute.
  64. :param case: the original case
  65. :return: a list of replicated cases
  66. """
  67. replicate_config = []
  68. for key in case.case_info:
  69. if key == 'ci_target': # ci_target is used to filter target, should not be duplicated.
  70. continue
  71. if isinstance(case.case_info[key], (list, tuple)):
  72. replicate_config.append(key)
  73. def _replicate_for_key(cases, replicate_key, replicate_list):
  74. def deepcopy_func(f, name=None):
  75. fn = types.FunctionType(f.__code__, f.__globals__, name if name else f.__name__,
  76. f.__defaults__, f.__closure__)
  77. fn.__dict__.update(copy.deepcopy(f.__dict__))
  78. return fn
  79. case_out = []
  80. for inner_case in cases:
  81. for value in replicate_list:
  82. new_case = deepcopy_func(inner_case)
  83. new_case.case_info[replicate_key] = value
  84. case_out.append(new_case)
  85. return case_out
  86. replicated_cases = [case]
  87. while replicate_config:
  88. if not replicate_config:
  89. break
  90. key = replicate_config.pop()
  91. if key in cls.SUPPORT_REPLICATE_CASES_KEY:
  92. replicated_cases = _replicate_for_key(replicated_cases, key, case.case_info[key])
  93. # mark the cases with targets not in ci_target
  94. for case in replicated_cases:
  95. ci_target = case.case_info['ci_target']
  96. if not ci_target or case.case_info['target'] in ci_target:
  97. case.case_info['supported_in_ci'] = True
  98. else:
  99. case.case_info['supported_in_ci'] = False
  100. return replicated_cases
  101. @classmethod
  102. def search_test_cases(cls, test_case_paths, test_case_file_pattern=None):
  103. """
  104. search all test cases from a folder or file, and then do case replicate.
  105. :param test_case_paths: test case file(s) paths
  106. :param test_case_file_pattern: unix filename pattern
  107. :return: a list of replicated test methods
  108. """
  109. if not isinstance(test_case_paths, list):
  110. test_case_paths = [test_case_paths]
  111. test_case_files = []
  112. for path in test_case_paths:
  113. test_case_files.extend(
  114. cls._search_test_case_files(path, test_case_file_pattern or cls.TEST_CASE_FILE_PATTERN))
  115. test_cases = []
  116. for test_case_file in test_case_files:
  117. test_cases += cls._search_cases_from_file(test_case_file)
  118. if cls.missing_import_warnings:
  119. print('\n\n'.join(cls.missing_import_warnings))
  120. sys.exit(1)
  121. return test_cases