OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Simplify unit tests based on pymox.""" | 6 """Simplify unit tests based on pymox.""" |
7 | 7 |
8 import __builtin__ | 8 import __builtin__ |
9 import os | 9 import os |
10 import random | 10 import random |
| 11 import shutil |
11 import string | 12 import string |
12 import subprocess | 13 import subprocess |
13 import sys | 14 import sys |
14 from pymox import mox | 15 |
| 16 sys.path.append(os.path.dirname(os.path.dirname(__file__))) |
| 17 from third_party.pymox import mox |
15 | 18 |
16 | 19 |
17 class IsOneOf(mox.Comparator): | 20 class IsOneOf(mox.Comparator): |
18 def __init__(self, keys): | 21 def __init__(self, keys): |
19 self._keys = keys | 22 self._keys = keys |
20 | 23 |
21 def equals(self, rhs): | 24 def equals(self, rhs): |
22 return rhs in self._keys | 25 return rhs in self._keys |
23 | 26 |
24 def __repr__(self): | 27 def __repr__(self): |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 #self.mox.StubOutWithMock(__builtin__, 'open') | 92 #self.mox.StubOutWithMock(__builtin__, 'open') |
90 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', | 93 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', |
91 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek', | 94 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek', |
92 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', | 95 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', |
93 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', | 96 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', |
94 'system', 'tmpfile', 'walk', 'write') | 97 'system', 'tmpfile', 'walk', 'write') |
95 self.MockList(os, os_to_mock) | 98 self.MockList(os, os_to_mock) |
96 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', | 99 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', |
97 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') | 100 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') |
98 self.MockList(os.path, os_path_to_mock) | 101 self.MockList(os.path, os_path_to_mock) |
| 102 self.MockList(shutil, ('rmtree')) |
99 self.MockList(subprocess, ('call', 'Popen')) | 103 self.MockList(subprocess, ('call', 'Popen')) |
100 # Don't mock stderr since it confuses unittests. | 104 # Don't mock stderr since it confuses unittests. |
101 self.MockList(sys, ('stdin', 'stdout')) | 105 self.MockList(sys, ('stdin', 'stdout')) |
102 | 106 |
103 def MockList(self, parent, items_to_mock): | 107 def MockList(self, parent, items_to_mock): |
104 for item in items_to_mock: | 108 for item in items_to_mock: |
105 # Skip over items not present because of OS-specific implementation, | 109 # Skip over items not present because of OS-specific implementation, |
106 # implemented only in later python version, etc. | 110 # implemented only in later python version, etc. |
107 if hasattr(parent, item): | 111 if hasattr(parent, item): |
108 self.mox.StubOutWithMock(parent, item) | 112 try: |
| 113 self.mox.StubOutWithMock(parent, item) |
| 114 except TypeError: |
| 115 raise TypeError('Couldn\'t mock %s in %s' % (item, parent.__name__)) |
OLD | NEW |