| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from collections import namedtuple |
| 6 |
| 7 from common.dependency import Dependency |
| 5 from crash import crash_util | 8 from crash import crash_util |
| 6 from testing_utils import testing | 9 from crash.crash_match import CrashMatch |
| 10 from crash.crash_match import FrameInfo |
| 11 from crash.stacktrace import CallStack |
| 12 from crash.stacktrace import StackFrame |
| 13 from crash.stacktrace import Stacktrace |
| 14 from crash.suspect import Suspect |
| 15 from crash.test.predator_testcase import PredatorTestCase |
| 16 from libs.gitiles.change_log import ChangeLog |
| 17 |
| 18 _CHANGELOG = ChangeLog.FromDict({ |
| 19 'author': { |
| 20 'name': 'r@chromium.org', |
| 21 'email': 'r@chromium.org', |
| 22 'time': 'Thu Mar 31 21:24:43 2016', |
| 23 }, |
| 24 'committer': { |
| 25 'name': 'example@chromium.org', |
| 26 'email': 'r@chromium.org', |
| 27 'time': 'Thu Mar 31 21:28:39 2016', |
| 28 }, |
| 29 'message': 'dummy', |
| 30 'commit_position': 175900, |
| 31 'touched_files': [ |
| 32 { |
| 33 'change_type': 'modify', |
| 34 'new_path': 'src/a.cc', |
| 35 'old_path': 'src/a.cc', |
| 36 }, |
| 37 ], |
| 38 'commit_url': |
| 39 'https://repo.test/+/1', |
| 40 'code_review_url': 'https://codereview.chromium.org/3281', |
| 41 'revision': '1', |
| 42 'reverted_revision': None |
| 43 }) |
| 7 | 44 |
| 8 | 45 |
| 9 class CrashUtilTest(testing.AppengineTestCase): | 46 class MockCrashedGroup(namedtuple('MockCrashedGroup', ['value'])): |
| 47 |
| 48 __slots__ = () |
| 49 |
| 50 @staticmethod |
| 51 def Factory(frame): |
| 52 return MockCrashedGroup(frame.raw_file_path if frame else None) |
| 53 |
| 54 def MatchTouchedFile(self, touched_file): |
| 55 return touched_file.new_path == self.value |
| 56 |
| 57 |
| 58 class CrashUtilTest(PredatorTestCase): |
| 10 | 59 |
| 11 def testIsSameFilePath(self): | 60 def testIsSameFilePath(self): |
| 12 path_1 = 'third_party/a/b/c/file.cc' | 61 path_1 = 'third_party/a/b/c/file.cc' |
| 13 path_2 = 'third_party/a/file.cc' | 62 path_2 = 'third_party/a/file.cc' |
| 14 | 63 |
| 15 self.assertTrue(crash_util.IsSameFilePath(path_1, path_2)) | 64 self.assertTrue(crash_util.IsSameFilePath(path_1, path_2)) |
| 16 | 65 |
| 17 path_1 = 'a/b/c/file.cc' | 66 path_1 = 'a/b/c/file.cc' |
| 18 path_2 = 'a/b/c/file2.cc' | 67 path_2 = 'a/b/c/file2.cc' |
| 19 | 68 |
| 20 self.assertFalse(crash_util.IsSameFilePath(path_1, path_2)) | 69 self.assertFalse(crash_util.IsSameFilePath(path_1, path_2)) |
| 21 | 70 |
| 22 path_1 = 'a/b/c/d/e/file.cc' | 71 path_1 = 'a/b/c/d/e/file.cc' |
| 23 path_2 = 'f/g/file.cc' | 72 path_2 = 'f/g/file.cc' |
| 24 | 73 |
| 25 self.assertTrue(crash_util.IsSameFilePath(None, None)) | 74 self.assertTrue(crash_util.IsSameFilePath(None, None)) |
| 26 self.assertFalse(crash_util.IsSameFilePath(path_1, path_2)) | 75 self.assertFalse(crash_util.IsSameFilePath(path_1, path_2)) |
| 27 self.assertFalse(crash_util.IsSameFilePath(None, path_2)) | 76 self.assertFalse(crash_util.IsSameFilePath(None, path_2)) |
| 28 self.assertFalse(crash_util.IsSameFilePath(path_1, None)) | 77 self.assertFalse(crash_util.IsSameFilePath(path_1, None)) |
| 78 |
| 79 def testIndexFramesWithCrashedGroup(self): |
| 80 """Tests ``IndexFramesWithCrashedGroup`` function.""" |
| 81 frame1 = StackFrame(0, 'src/', 'func', 'f.cc', |
| 82 'src/f.cc', [2, 3], 'h://repo') |
| 83 frame2 = StackFrame(1, 'src/', 'func', 'a.cc', |
| 84 'src/a.cc', [31, 32], 'h://repo') |
| 85 frame3 = StackFrame(1, 'src/dummy', 'func', 'a.cc', |
| 86 'src/dummy/a.cc', [131, 132], 'h://repo') |
| 87 stack = CallStack(0, frame_list=[frame1, frame2, frame3]) |
| 88 stack_trace = Stacktrace([stack], stack) |
| 89 deps = {'src/': Dependency('src/', 'h://repo', 'rev3')} |
| 90 |
| 91 indexed_frame_infos = crash_util.IndexFramesWithCrashedGroup( |
| 92 stack_trace, MockCrashedGroup.Factory, deps) |
| 93 expected_frame_infos = {'src/': {MockCrashedGroup('src/f.cc'): |
| 94 [FrameInfo(frame1, 0)], |
| 95 MockCrashedGroup('src/a.cc'): |
| 96 [FrameInfo(frame2, 0)]}} |
| 97 self.assertEqual(indexed_frame_infos, expected_frame_infos) |
| 98 |
| 99 def testMatchSuspectWithFrameInfos(self): |
| 100 """Tests ``MatchSuspectWithFrameInfos`` function.""" |
| 101 frame1 = StackFrame(0, 'src/', 'func', 'f.cc', |
| 102 'src/f.cc', [2, 3], 'h://repo') |
| 103 frame2 = StackFrame(1, 'src/', 'func', 'a.cc', |
| 104 'src/a.cc', [31, 32], 'h://repo') |
| 105 grouped_frame_infos = { |
| 106 MockCrashedGroup('src/f.cc'): [FrameInfo(frame1, 0)], |
| 107 MockCrashedGroup('src/a.cc'): [FrameInfo(frame2, 0)] |
| 108 } |
| 109 suspect = Suspect(_CHANGELOG, 'src/') |
| 110 matches = crash_util.MatchSuspectWithFrameInfos(suspect, |
| 111 grouped_frame_infos) |
| 112 crashed = MockCrashedGroup('src/a.cc') |
| 113 expected_matches = { |
| 114 crashed: CrashMatch(crashed, _CHANGELOG.touched_files, |
| 115 [FrameInfo(frame2, 0)]) |
| 116 } |
| 117 self.assertDictEqual(matches, expected_matches) |
| OLD | NEW |