| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 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 """Simplify unit tests based on pymox.""" | 5 """Simplify unit tests based on pymox.""" |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import random | 8 import random |
| 9 import shutil | 9 import shutil |
| 10 import string | 10 import string |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 # pylint: disable=E1101 | 105 # pylint: disable=E1101 |
| 106 self.assertEquals(expected, value) | 106 self.assertEquals(expected, value) |
| 107 | 107 |
| 108 | 108 |
| 109 class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): | 109 class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): |
| 110 def setUp(self): | 110 def setUp(self): |
| 111 """Patch a few functions with know side-effects.""" | 111 """Patch a few functions with know side-effects.""" |
| 112 TestCaseUtils.setUp(self) | 112 TestCaseUtils.setUp(self) |
| 113 mox.MoxTestBase.setUp(self) | 113 mox.MoxTestBase.setUp(self) |
| 114 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', | 114 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', |
| 115 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'lseek', | 115 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'listdir', 'lseek', |
| 116 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', | 116 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', |
| 117 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', | 117 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', |
| 118 'system', 'tmpfile', 'walk', 'write') | 118 'system', 'tmpfile', 'walk', 'write') |
| 119 self.MockList(os, os_to_mock) | 119 self.MockList(os, os_to_mock) |
| 120 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', | 120 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', |
| 121 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') | 121 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') |
| 122 self.MockList(os.path, os_path_to_mock) | 122 self.MockList(os.path, os_path_to_mock) |
| 123 self.MockList(shutil, ('rmtree')) | 123 self.MockList(shutil, ('rmtree')) |
| 124 self.MockList(subprocess, ('call', 'Popen')) | 124 self.MockList(subprocess, ('call', 'Popen')) |
| 125 # Don't mock stderr since it confuses unittests. | 125 # Don't mock stderr since it confuses unittests. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 except TypeError, e: | 141 except TypeError, e: |
| 142 raise TypeError( | 142 raise TypeError( |
| 143 'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e)) | 143 'Couldn\'t mock %s in %s: %s' % (item, parent.__name__, e)) |
| 144 | 144 |
| 145 def UnMock(self, obj, name): | 145 def UnMock(self, obj, name): |
| 146 """Restore an object inside a test.""" | 146 """Restore an object inside a test.""" |
| 147 for (parent, old_child, child_name) in self.mox.stubs.cache: | 147 for (parent, old_child, child_name) in self.mox.stubs.cache: |
| 148 if parent == obj and child_name == name: | 148 if parent == obj and child_name == name: |
| 149 setattr(parent, child_name, old_child) | 149 setattr(parent, child_name, old_child) |
| 150 break | 150 break |
| OLD | NEW |