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 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)
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 testOnlyOneTouchedFilePerMatchedCrashedFile(self): """Test that ``CrashMatch`` can only have 1 touched file.""" report = self._GetDummyReport( deps={'src/': Dependency('src/', 'https://repo', '6')}, dep_rolls={'src/': DependencyRoll('src/', 'https://repo', '0', '4')}) frame = _MOCK_FRAME._replace(file_path='file') crashed = CrashedFile('file') matches = { crashed: CrashMatch(crashed, [FileChangeInfo(ChangeType.MODIFY, 'file', 'file'), FileChangeInfo(ChangeType.MODIFY, 'dummy', 'dummy')], [FrameInfo(frame, 0)]) } self.assertEqual( 0.0, min_distance.MinDistanceFeature(self._get_repository, _MAXIMUM)(report)( self._GetMockSuspect(), matches).value)
def testIsLogOneWhenThereIsMatchedFiles(self): """Test that the feature returns log(1) when there is matched file.""" report = self._GetDummyReport() suspect = self._GetMockSuspect() frame = StackFrame(index=0, dep_path=suspect.dep_path, function='func', file_path='a.cc', raw_file_path='a.cc', crashed_line_numbers=[7]) crashed = CrashedFile(frame) matches = { crashed: CrashMatch(crashed, [FileChangeInfo(ChangeType.MODIFY, 'a.cc', 'a.cc')], [FrameInfo(frame=frame, priority=0)]) } self.assertEqual( 1.0, TouchCrashedFileFeature()(report)(suspect, matches).value)
def testTopFrameIndexValueForBottonFrame(self): """Test that feature returns 0 when the frame index is larger than max.""" report = self._GetDummyReport() suspect = self._GetMockSuspect() frame = StackFrame(index=_MAXIMUM + 1, dep_path=suspect.dep_path, function='func', file_path='a.cc', raw_file_path='a.cc', crashed_line_numbers=[7]) crashed = CrashedFile(frame) matches = { crashed: CrashMatch(crashed, [FileChangeInfo(ChangeType.MODIFY, 'a.cc', 'a.cc')], [FrameInfo(frame=frame, priority=0)]) } self.assertEqual( 0.0, top_frame_index.TopFrameIndexFeature(_MAXIMUM)(report)( suspect, matches).value)