SearchCases.py 5.4 KB

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