Esempio n. 1
0
    def testMinDistanceFeatureIsOverMax(self):
        """Test that we return log(0) when the min_distance is too large."""
        report = self._GetDummyReport(
            deps={'src/': Dependency('src/', 'https://repo', '6')},
            dep_rolls={
                'src/': DependencyRoll('src/', 'https://repo', '0', '4')
            })

        distance = _MAXIMUM + 1
        frame = _MOCK_FRAME._replace(file_path='file')
        crashed = CrashedFile('file')
        matches = {
            crashed:
            CrashMatch(crashed,
                       [FileChangeInfo(ChangeType.MODIFY, 'file', 'file')],
                       [FrameInfo(frame, 0)])
        }
        with mock.patch(
                'analysis.linear.changelist_features.'
                'min_distance.MinDistanceFeature.'
                'DistanceBetweenTouchedFileAndFrameInfos') as mock_distance:
            mock_distance.return_value = min_distance.Distance(distance, None)
            self.assertEqual(
                0.0,
                min_distance.MinDistanceFeature(
                    self._get_repository,
                    _MAXIMUM)(report)(self._GetMockSuspect(), matches).value)
Esempio n. 2
0
 def testUpdate(self):
     """Tests that ``Update`` updates distance and frame."""
     distance_info = min_distance.Distance(100, None)
     distance_info.Update(50, None)
     self.assertEqual(distance_info.distance, 50)
     distance_info.Update(80, None)
     self.assertEqual(distance_info.distance, 50)
Esempio n. 3
0
    def testMinDistanceFeatureMiddling(self):
        """Test that the feature returns middling scores for middling distances."""
        report = self._GetDummyReport(
            deps={'src/': Dependency('src/', 'https://repo', '6')},
            dep_rolls={
                'src/': DependencyRoll('src/', 'https://repo', '0', '4')
            })

        frame = StackFrame(0, 'src/', 'func', 'f.cc', 'f.cc', [232],
                           'https://repo')
        distance = 42.
        crashed = CrashedFile('file')
        matches = {
            crashed:
            CrashMatch(crashed,
                       [FileChangeInfo(ChangeType.MODIFY, 'file', 'file')],
                       [FrameInfo(frame, 0)])
        }
        with mock.patch(
                'analysis.linear.changelist_features.'
                'min_distance.MinDistanceFeature.'
                'DistanceBetweenTouchedFileAndFrameInfos') as mock_distance:
            mock_distance.return_value = min_distance.Distance(distance, frame)
            self.assertEqual((_MAXIMUM - distance) / _MAXIMUM,
                             min_distance.MinDistanceFeature(
                                 self._get_repository,
                                 _MAXIMUM)(report)(self._GetMockSuspect(),
                                                   matches).value)
  def testMinDistanceChangedFiles(self):
    """Tests ``ChangedFile`` method."""
    report = self._GetDummyReport(
        deps={'src/': Dependency('src/', 'https://repo', '6')},
        dep_rolls={'src/': DependencyRoll('src/', 'https://repo', '0', '4')})

    distance = 42
    crashed = CrashedFile(_MOCK_FRAME)
    matches = {
        crashed:
        CrashMatch(crashed,
                   [FileChangeInfo(ChangeType.MODIFY, 'file', 'file')],
                   [FrameInfo(_MOCK_FRAME, 0)])
    }
    frame = StackFrame(0, 'src/', 'func', 'f.cc', 'f.cc', [7], 'https://repo')
    with mock.patch('analysis.linear.changelist_features.min_distance.'
                    'MinDistanceFeature.'
                    'DistanceBetweenTouchedFileAndFrameInfos') as mock_distance:
      mock_distance.return_value = min_distance.Distance(distance, frame)
      self.assertEqual(
          min_distance.MinDistanceFeature(
              self._get_repository, _MAXIMUM)(report)(
              self._GetMockSuspect(), matches).changed_files,
              [ChangedFile(name='file',
                           blame_url=('%s/+blame/%s/f.cc#%d' %
                                      (frame.repo_url,
                                       report.crashed_version,
                                       frame.crashed_line_numbers[0])),
                           reasons=['Distance between touched lines and crashed'
                                    ' lines is %d, in frame #%d' % (
                                        distance, frame.index)])])
  def testMinDistanceFeatureInfinityDistance(self):
    """Test that we return log(0) when the min_distance is infinity.

    The infinity distance means the touched file get overwritten by other
    cls, and the change didn't show in the final blame file.
    """
    report = self._GetDummyReport(
        deps={'src/': Dependency('src/', 'https://repo', '6')},
        dep_rolls={'src/': DependencyRoll('src/', 'https://repo', '0', '4')})
    suspect = self._GetMockSuspect()
    crashed = CrashedFile(_MOCK_FRAME)
    matches = {
        crashed:
        CrashMatch(crashed,
                   [FileChangeInfo(ChangeType.MODIFY, 'file', 'file')],
                   [FrameInfo(_MOCK_FRAME, 0)])
    }

    with mock.patch('analysis.linear.changelist_features.min_distance.'
                    'MinDistanceFeature.'
                    'DistanceBetweenTouchedFileAndFrameInfos') as mock_distance:
      mock_distance.return_value = None
      self.assertEqual(
          0.0,
          min_distance.MinDistanceFeature(
              self._get_repository, _MAXIMUM)(report)(suspect, matches).value)

    with mock.patch('analysis.linear.changelist_features.min_distance.'
                    'MinDistanceFeature.'
                    'DistanceBetweenTouchedFileAndFrameInfos') as mock_distance:
      mock_distance.return_value = min_distance.Distance(float('inf'), None)
      self.assertEqual(
          0.0,
          min_distance.MinDistanceFeature(self._get_repository, 100)(report)(
              suspect, matches).value)
  def testDistanceBetweenTouchedFileAndFrameInfos(self):
    """Tests ``DistanceBetweenTouchedFileAndFrameInfos`` method."""
    feature = min_distance.MinDistanceFeature(self._get_repository, _MAXIMUM)
    frame1 = StackFrame(0, 'src/', 'func', 'a.cc', 'src/a.cc', [7],
                        repo_url='https://repo_url')
    frame2 = StackFrame(0, 'src/', 'func', 'a.cc', 'src/a.cc', [17],
                        repo_url='https://repo_url')
    touched_file = FileChangeInfo(ChangeType.MODIFY, 'file', 'file')

    blame = Blame('rev', 'src/')
    blame.AddRegions([Region(0, 10, 'rev', 'a1', 'e1', 't1'),
                      Region(11, 20, 'dummy_rev', 'a2', 'e2', 't2')])

    url_to_blame = {'rev/file': blame}

    def _MockGetBlame(path, revision):
      revision_path = '%s/%s' % (revision, path)
      return url_to_blame.get(revision_path)

    with mock.patch('libs.gitiles.gitiles_repository.GitilesRepository.'
                    'GetBlame') as mock_get_blame:
      mock_get_blame.side_effect = _MockGetBlame

      distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
          'rev', touched_file, [FrameInfo(frame1, 0), FrameInfo(frame2, 0)],
           Dependency('src/', 'https://repo', 'rev'))
      self.assertEqual(distance_info, min_distance.Distance(0, frame1))

      distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
          'wrong_rev', touched_file, [FrameInfo(frame1, 0),
                                      FrameInfo(frame2, 0)],
           Dependency('src/', 'https://repo', 'wrong_rev'))
      self.assertIsNone(distance_info)
Esempio n. 7
0
 def testIsInfinity(self):
     """Tests that ``IsInfinity`` checks if distance is infinity."""
     distance_info = min_distance.Distance(float('inf'), None)
     self.assertTrue(distance_info.IsInfinity())
Esempio n. 8
0
    def testDistanceBetweenTouchedFileAndFrameInfos(self):
        """Tests ``DistanceBetweenTouchedFileAndFrameInfos`` method."""
        feature = min_distance.MinDistanceFeature(self._get_repository,
                                                  _MAXIMUM)
        frame1 = StackFrame(0,
                            'src/',
                            'func',
                            'a.cc',
                            'src/a.cc', [7],
                            repo_url='https://repo_url')
        frame2 = StackFrame(0,
                            'src/',
                            'func',
                            'a.cc',
                            'src/a.cc', [17],
                            repo_url='https://repo_url')
        profiler_frame = ProfilerStackFrame(0,
                                            -0.1,
                                            -5.3,
                                            True,
                                            function_start_line=13)
        profiler_frame_without_line_number = ProfilerStackFrame(
            0, -0.1, -5.3, True, function_start_line=None)
        touched_file = FileChangeInfo(ChangeType.MODIFY, 'file', 'file')

        blame = Blame('rev', 'src/')
        blame.AddRegions([
            Region(0, 10, 'rev', 'a1', 'e1', 't1'),
            Region(11, 20, 'dummy_rev', 'a2', 'e2', 't2')
        ])

        url_to_blame = {'rev/file': blame}

        def _MockGetBlame(path, revision):
            revision_path = '%s/%s' % (revision, path)
            return url_to_blame.get(revision_path)

        with mock.patch('libs.gitiles.gitiles_repository.GitilesRepository.'
                        'GetBlame') as mock_get_blame:
            mock_get_blame.side_effect = _MockGetBlame

            distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
                'rev', touched_file,
                [FrameInfo(frame1, 0),
                 FrameInfo(frame2, 0)],
                Dependency('src/', 'https://repo', 'rev'))
            self.assertEqual(distance_info, min_distance.Distance(0, frame1))

            distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
                'wrong_rev', touched_file,
                [FrameInfo(frame1, 0),
                 FrameInfo(frame2, 0)],
                Dependency('src/', 'https://repo', 'wrong_rev'))
            self.assertIsNone(distance_info)

            # Test with a ProfilerStackFrame
            distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
                'rev', touched_file, [
                    FrameInfo(profiler_frame, 0),
                    FrameInfo(profiler_frame_without_line_number, 0)
                ], Dependency('src/', 'https://repo', 'rev'))
            self.assertEqual(distance_info,
                             min_distance.Distance(4, profiler_frame))

            # Test that the distance remains at ``inf`` if the ProfilerStackFrames
            # passed in do not have line numbers.
            distance_info = feature.DistanceBetweenTouchedFileAndFrameInfos(
                'rev', touched_file,
                [FrameInfo(profiler_frame_without_line_number, 0)],
                Dependency('src/', 'https://repo', 'rev'))
            self.assertEqual(distance_info,
                             min_distance.Distance(float('inf'), None))