Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Unit tests for breakpad.py.""" | |
| 7 | |
| 8 # pylint: disable=W0403 | |
| 9 | |
| 10 # Fixes include path. | |
| 11 from super_mox import SuperMoxTestBase | |
| 12 | |
| 13 import breakpad | |
| 14 | |
| 15 | |
| 16 class Breakpad(SuperMoxTestBase): | |
| 17 """Setups and tear downs the mocks but doesn't test anything as-is.""" | |
| 18 def setUp(self): | |
| 19 super(Breakpad, self).setUp() | |
| 20 self.mox.StubOutWithMock(breakpad.atexit, 'register') | |
| 21 self.mox.StubOutWithMock(breakpad.getpass, 'getuser') | |
| 22 self.mox.StubOutWithMock(breakpad.urllib2, 'urlopen') | |
| 23 breakpad._HOST_NAME = 'bozo' | |
| 24 self._old_sys_argv = breakpad.sys.argv | |
| 25 breakpad.sys.argv = ['my_test'] | |
| 26 self._old_sys_version = breakpad.sys.version | |
| 27 breakpad.sys.version = 'random python' | |
| 28 | |
| 29 def tearDown(self): | |
| 30 breakpad.sys.version = self._old_sys_version | |
| 31 breakpad.sys.argv = self._old_sys_argv | |
| 32 super(Breakpad, self).tearDown() | |
| 33 | |
| 34 def testMembersChanged(self): | |
| 35 members = [ | |
| 36 'CheckForException', 'DEFAULT_URL', 'FormatException', 'Register', | |
| 37 'SendProfiling', 'SendStack', | |
| 38 'atexit', 'getpass', 'os', 'post', 'socket', 'sys', 'time', 'traceback', | |
| 39 'urllib', 'urllib2', | |
| 40 ] | |
| 41 # If this test fails, you should add the relevant test. | |
| 42 self.compareMembers(breakpad, members) | |
| 43 | |
| 44 def _part1(self, exception): | |
|
Dirk Pranke
2011/10/03 19:09:16
Nit: can you come up with better names than "part1
| |
| 45 breakpad.os.getcwd().AndReturn('/tmp/asdf') | |
| 46 breakpad.getpass.getuser().AndReturn('georges') | |
| 47 obj = self.mox.CreateMockAnything() | |
| 48 breakpad.urllib2.urlopen( | |
| 49 'https://chromium-status.appspot.com/breakpad', | |
| 50 breakpad.urllib.urlencode([('exception', exception)]) + ( | |
| 51 '&args=%5B%27my_test%27%5D' | |
| 52 '&stack=bar' | |
| 53 '&host=bozo' | |
| 54 '&version=random+python' | |
| 55 '&user=georges' | |
| 56 '&cwd=%2Ftmp%2Fasdf') | |
| 57 ).AndReturn(obj) | |
| 58 obj.read().AndReturn('ok') | |
| 59 obj.close() | |
| 60 | |
| 61 def _part2(self, exception): | |
| 62 self.checkstdout( | |
| 63 ( "Sending crash report ...\n" | |
| 64 " args: ['my_test']\n" | |
| 65 " cwd: /tmp/asdf\n" | |
| 66 " exception: %s\n" | |
| 67 " host: bozo\n" | |
| 68 " stack: bar\n" | |
| 69 " user: georges\n" | |
| 70 " version: random python\n" | |
| 71 "ok\n") % exception) | |
| 72 | |
| 73 def _check(self, obj, result): | |
| 74 self._part1(result) | |
| 75 self.mox.ReplayAll() | |
| 76 breakpad.SendStack(obj, 'bar') | |
| 77 self._part2(result) | |
| 78 | |
| 79 def testSendBase(self): | |
| 80 self._check('foo', 'foo') | |
| 81 | |
| 82 def testSendReprThrows(self): | |
| 83 class Throws(object): | |
| 84 def __repr__(self): | |
| 85 raise NotImplementedError() | |
| 86 def __str__(self): | |
| 87 return '[foo]' | |
| 88 self._check(Throws(), '[foo]') | |
| 89 | |
| 90 def testSendStrThrows(self): | |
| 91 class Throws(object): | |
| 92 def __repr__(self): | |
| 93 return '[foo]' | |
| 94 def __str__(self): | |
| 95 raise NotImplementedError() | |
| 96 self._check(Throws(), '[foo]') | |
| 97 | |
| 98 def testSendBoth(self): | |
| 99 class Both(object): | |
| 100 def __repr__(self): | |
| 101 return '[foo]' | |
| 102 def __str__(self): | |
| 103 return '[bar]' | |
| 104 self._check(Both(), '[bar]') | |
| 105 | |
| 106 def testSendException(self): | |
| 107 obj = Exception('foo') | |
| 108 obj.msg = 'a message' | |
| 109 self._check(obj, 'foo\nMsg: a message') | |
| 110 | |
| 111 | |
| 112 | |
| 113 if __name__ == '__main__': | |
| 114 import unittest | |
| 115 unittest.main() | |
| OLD | NEW |