| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 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 import os | |
| 9 import sys | |
| 10 | |
| 11 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| 12 | |
| 13 from testing_support.super_mox import SuperMoxTestBase | |
| 14 | |
| 15 import breakpad | |
| 16 | |
| 17 | |
| 18 class Breakpad(SuperMoxTestBase): | |
| 19 """Setups and tear downs the mocks but doesn't test anything as-is.""" | |
| 20 def setUp(self): | |
| 21 super(Breakpad, self).setUp() | |
| 22 self.mox.StubOutWithMock(breakpad.atexit, 'register') | |
| 23 self.mox.StubOutWithMock(breakpad.getpass, 'getuser') | |
| 24 self.mox.StubOutWithMock(breakpad.urllib2, 'urlopen') | |
| 25 breakpad._HOST_NAME = 'bozo' | |
| 26 self.assertEquals(False, breakpad.IS_ENABLED) | |
| 27 breakpad.IS_ENABLED = True | |
| 28 self._old_sys_argv = breakpad.sys.argv | |
| 29 breakpad.sys.argv = ['my_test'] | |
| 30 self._old_sys_version = breakpad.sys.version | |
| 31 breakpad.sys.version = 'random python' | |
| 32 | |
| 33 def tearDown(self): | |
| 34 breakpad.IS_ENABLED = False | |
| 35 breakpad.sys.version = self._old_sys_version | |
| 36 breakpad.sys.argv = self._old_sys_argv | |
| 37 super(Breakpad, self).tearDown() | |
| 38 | |
| 39 def testMembersChanged(self): | |
| 40 members = [ | |
| 41 'CheckForException', 'DEFAULT_URL', 'FormatException', 'IS_ENABLED', | |
| 42 'Register', 'SendProfiling', 'SendStack', | |
| 43 'atexit', 'getpass', 'os', 'post', 'socket', 'sys', 'time', 'traceback', | |
| 44 'urllib', 'urllib2', | |
| 45 ] | |
| 46 # If this test fails, you should add the relevant test. | |
| 47 self.compareMembers(breakpad, members) | |
| 48 | |
| 49 def _part_1_setup_mocks(self, exception): | |
| 50 breakpad.os.getcwd().AndReturn('/tmp/asdf') | |
| 51 breakpad.getpass.getuser().AndReturn('georges') | |
| 52 obj = self.mox.CreateMockAnything() | |
| 53 kwargs = {} | |
| 54 if (breakpad.sys.version_info[0] * 10 + breakpad.sys.version_info[1]) >= 26: | |
| 55 kwargs['timeout'] = 4 | |
| 56 breakpad.urllib2.urlopen( | |
| 57 'https://chromium-status.appspot.com/breakpad', | |
| 58 breakpad.urllib.urlencode([('exception', exception)]) + ( | |
| 59 '&args=%5B%27my_test%27%5D' | |
| 60 '&stack=bar' | |
| 61 '&host=bozo' | |
| 62 '&version=random+python' | |
| 63 '&user=georges' | |
| 64 '&cwd=%2Ftmp%2Fasdf'), | |
| 65 **kwargs).AndReturn(obj) | |
| 66 obj.read().AndReturn('ok') | |
| 67 obj.close() | |
| 68 | |
| 69 def _part_2_verify_stdout(self, exception): | |
| 70 self.checkstdout( | |
| 71 ( "Sending crash report ...\n" | |
| 72 " args: ['my_test']\n" | |
| 73 " cwd: /tmp/asdf\n" | |
| 74 " exception: %s\n" | |
| 75 " host: bozo\n" | |
| 76 " stack: bar\n" | |
| 77 " user: georges\n" | |
| 78 " version: random python\n" | |
| 79 "ok\n") % exception) | |
| 80 | |
| 81 def _check(self, obj, result): | |
| 82 self._part_1_setup_mocks(result) | |
| 83 self.mox.ReplayAll() | |
| 84 breakpad.SendStack(obj, 'bar') | |
| 85 self._part_2_verify_stdout(result) | |
| 86 | |
| 87 def testSendBase(self): | |
| 88 self._check('foo', 'foo') | |
| 89 | |
| 90 def testSendReprThrows(self): | |
| 91 class Throws(object): | |
| 92 def __repr__(self): | |
| 93 raise NotImplementedError() | |
| 94 def __str__(self): | |
| 95 return '[foo]' | |
| 96 self._check(Throws(), '[foo]') | |
| 97 | |
| 98 def testSendStrThrows(self): | |
| 99 class Throws(object): | |
| 100 def __repr__(self): | |
| 101 return '[foo]' | |
| 102 def __str__(self): | |
| 103 raise NotImplementedError() | |
| 104 self._check(Throws(), '[foo]') | |
| 105 | |
| 106 def testSendBoth(self): | |
| 107 class Both(object): | |
| 108 def __repr__(self): | |
| 109 return '[foo]' | |
| 110 def __str__(self): | |
| 111 return '[bar]' | |
| 112 self._check(Both(), '[bar]') | |
| 113 | |
| 114 def testSendException(self): | |
| 115 obj = Exception('foo') | |
| 116 obj.msg = 'a message' | |
| 117 self._check(obj, 'foo\nMsg: a message') | |
| 118 | |
| 119 | |
| 120 | |
| 121 if __name__ == '__main__': | |
| 122 import unittest | |
| 123 unittest.main() | |
| OLD | NEW |