Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb086f0d authored by Luca Farsi's avatar Luca Farsi Committed by Gerrit Code Review
Browse files

Merge "Change artifact matching to be more strict" into main

parents 832819e9 99d40d75
Loading
Loading
Loading
Loading
+8 −32
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ class BuildPlanner:

    build_targets = set()
    packaging_functions = set()
    self.file_download_options = self._aggregate_file_download_options()
    for target in self.args.extra_targets:
      if self._unused_target_exclusion_enabled(
          target
@@ -107,49 +108,24 @@ class BuildPlanner:

  def _build_target_used(self, target: str) -> bool:
    """Determines whether this target's outputs are used by the test configurations listed in the build context."""
    file_download_regexes = self._aggregate_file_download_regexes()
    # For all of a targets' outputs, check if any of the regexes used by tests
    # to download artifacts would match it. If any of them do then this target
    # is necessary.
    for artifact in self._get_target_potential_outputs(target):
      for regex in file_download_regexes:
        if re.match(regex, artifact):
          return True
    return False

  def _get_target_potential_outputs(self, target: str) -> set[str]:
    tests_suffix = '-tests'
    if target.endswith('tests'):
      tests_suffix = ''
    # This is a list of all the potential zips output by the test suite targets.
    # If the test downloads artifacts from any of these zips, we will be
    # conservative and avoid skipping the tests.
    return {
        f'{target}.zip',
        f'android-{target}.zip',
        f'android-{target}-verifier.zip',
        f'{target}{tests_suffix}_list.zip',
        f'android-{target}{tests_suffix}_list.zip',
        f'{target}{tests_suffix}_host-shared-libs.zip',
        f'android-{target}{tests_suffix}_host-shared-libs.zip',
        f'{target}{tests_suffix}_configs.zip',
        f'android-{target}{tests_suffix}_configs.zip',
    }
    regex = r'\b(%s)\b' % re.escape(target)
    return any(re.search(regex, opt) for opt in self.file_download_options)

  def _aggregate_file_download_regexes(self) -> set[re.Pattern]:
  def _aggregate_file_download_options(self) -> set[str]:
    """Lists out all test config options to specify targets to download.

    These come in the form of regexes.
    """
    all_regexes = set()
    all_options = set()
    for test_info in self._get_test_infos():
      for opt in test_info.get('extraOptions', []):
        # check the known list of options for downloading files.
        if opt.get('key') in self._DOWNLOAD_OPTS:
          all_regexes.update(
              re.compile(value) for value in opt.get('values', [])
          )
    return all_regexes
          all_options.update(opt.get('values', []))
    return all_options

  def _get_test_infos(self):
    return self.build_context.get('testContext', dict()).get('testInfos', [])
+19 −0
Original line number Diff line number Diff line
@@ -380,6 +380,25 @@ class BuildPlannerTest(unittest.TestCase):

    self.assertSetEqual(build_plan.build_targets, set())

  def test_target_regex_matching_not_too_broad(self):
    build_target = 'test_target'
    test_context = self.get_test_context(build_target)
    test_context['testInfos'][0]['extraOptions'] = [{
        'key': 'additional-files-filter',
        'values': [f'.*a{build_target}.*\.zip'],
    }]
    build_planner = self.create_build_planner(
        build_targets={build_target},
        build_context=self.create_build_context(
            test_context=test_context,
            enabled_build_features={'test_target_unused_exclusion'},
        ),
    )

    build_plan = build_planner.create_build_plan()

    self.assertSetEqual(build_plan.build_targets, set())

  def create_build_planner(
      self,
      build_targets: set[str],