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 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 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 testFilePathIdfFeatureCallForMatches(self): """Tests ``__call__`` of ``FilePathIdfFeature`` for non-empty matches.""" report = CrashReport(None, None, None, None, None, None, None) frame1 = StackFrame(0, '', 'func', 'keyword1', 'src/keyword1', [2], 'http://repo') frame2 = StackFrame(0, '', 'func', 'keyword2', 'src/keyword2', [9], 'http://repo') matches = { CrashedFile('keyword1'): CrashMatch(CrashedFile('keyword1'), ['keyword1'], [FrameInfo(frame1, 0)]), CrashedFile('keyword2'): CrashMatch(CrashedFile('keyword2'), ['keyword2'], [FrameInfo(frame2, 0)]) } feature_value = self.feature(report)(None, matches) self.assertEqual( feature_value.value, file_path_idf.LogRegressNomalize(math.log(9 / float(1 + 3))))
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 testMatchSuspectWithFrameInfos(self): """Tests ``MatchSuspectWithFrameInfos`` function.""" frame1 = StackFrame(0, 'src/', 'func', 'f.cc', 'src/f.cc', [2, 3], 'h://repo') frame2 = StackFrame(1, 'src/', 'func', 'a.cc', 'src/a.cc', [31, 32], 'h://repo') grouped_frame_infos = { MockCrashedGroup('src/f.cc'): [FrameInfo(frame1, 0)], MockCrashedGroup('src/a.cc'): [FrameInfo(frame2, 0)] } suspect = Suspect(_CHANGELOG, 'src/') matches = crash_util.MatchSuspectWithFrameInfos( suspect, grouped_frame_infos, Match) crashed = MockCrashedGroup('src/a.cc') expected_matches = { crashed: CrashMatch(crashed, _CHANGELOG.touched_files, [FrameInfo(frame2, 0)]) } self.assertDictEqual(matches, expected_matches)
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)
def MatchSuspectWithFrameInfos(suspect, grouped_frame_infos, match_func): """Matches touched files of suspect with frames in stacktrace. Args: suspect (Suspect): The suspect to match with frames. grouped_frame_infos (dict of FrameInfo): Dict mapping a crashed group ( For example, CrashedFile('f.cc'), CrashedDirectory('dir/'). Returns: A dict mapping crashed group to a ``CrashMatch``. """ # Dict mapping files in stacktrace touched by suspect to there # corresponding stacktrace frames information. matched_touched_files = defaultdict(list) for crashed in grouped_frame_infos: for touched_file in suspect.changelog.touched_files: if match_func(crashed, touched_file): matched_touched_files[crashed].append(touched_file) return {crashed: CrashMatch(crashed_group=crashed, touched_files=matched_touched_files[crashed], frame_infos=grouped_frame_infos[crashed]) for crashed in matched_touched_files}