| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 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 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if parent == object and child_name == name: | 80 if parent == object and child_name == name: |
| 81 setattr(parent, child_name, old_child) | 81 setattr(parent, child_name, old_child) |
| 82 break | 82 break |
| 83 | 83 |
| 84 | 84 |
| 85 class SuperMoxTestBase(SuperMoxBaseTestBase): | 85 class SuperMoxTestBase(SuperMoxBaseTestBase): |
| 86 def setUp(self): | 86 def setUp(self): |
| 87 """Patch a few functions with know side-effects.""" | 87 """Patch a few functions with know side-effects.""" |
| 88 SuperMoxBaseTestBase.setUp(self) | 88 SuperMoxBaseTestBase.setUp(self) |
| 89 #self.mox.StubOutWithMock(__builtin__, 'open') | 89 #self.mox.StubOutWithMock(__builtin__, 'open') |
| 90 self.mox.StubOutWithMock(os, 'chdir') | 90 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', |
| 91 self.mox.StubOutWithMock(os, 'chown') | 91 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek', |
| 92 self.mox.StubOutWithMock(os, 'close') | 92 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', |
| 93 #self.mox.StubOutWithMock(os, 'closerange') | 93 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', |
| 94 self.mox.StubOutWithMock(os, 'dup') | 94 'system', 'tmpfile', 'walk', 'write') |
| 95 self.mox.StubOutWithMock(os, 'dup2') | 95 self.MockList(os, os_to_mock) |
| 96 self.mox.StubOutWithMock(os, 'fchdir') | 96 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', |
| 97 #self.mox.StubOutWithMock(os, 'fchmod') | 97 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') |
| 98 #self.mox.StubOutWithMock(os, 'fchown') | 98 self.MockList(os.path, os_path_to_mock) |
| 99 self.mox.StubOutWithMock(os, 'fdopen') | 99 self.MockList(subprocess, ('call', 'Popen')) |
| 100 self.mox.StubOutWithMock(os, 'getcwd') | 100 # Don't mock stderr since it confuses unittests. |
| 101 self.mox.StubOutWithMock(os, 'getpid') | 101 self.MockList(sys, ('stdin', 'stdout')) |
| 102 self.mox.StubOutWithMock(os, 'lseek') | 102 |
| 103 self.mox.StubOutWithMock(os, 'makedirs') | 103 def MockList(self, parent, items_to_mock): |
| 104 self.mox.StubOutWithMock(os, 'mkdir') | 104 for item in items_to_mock: |
| 105 self.mox.StubOutWithMock(os, 'open') | 105 # Skip over items not present because of OS-specific implementation, |
| 106 self.mox.StubOutWithMock(os, 'popen') | 106 # implemented only in later python version, etc. |
| 107 self.mox.StubOutWithMock(os, 'popen2') | 107 if hasattr(parent, item): |
| 108 self.mox.StubOutWithMock(os, 'popen3') | 108 self.mox.StubOutWithMock(parent, item) |
| 109 self.mox.StubOutWithMock(os, 'popen4') | |
| 110 self.mox.StubOutWithMock(os, 'read') | |
| 111 self.mox.StubOutWithMock(os, 'remove') | |
| 112 self.mox.StubOutWithMock(os, 'removedirs') | |
| 113 self.mox.StubOutWithMock(os, 'rename') | |
| 114 self.mox.StubOutWithMock(os, 'renames') | |
| 115 self.mox.StubOutWithMock(os, 'rmdir') | |
| 116 self.mox.StubOutWithMock(os, 'symlink') | |
| 117 self.mox.StubOutWithMock(os, 'system') | |
| 118 self.mox.StubOutWithMock(os, 'tmpfile') | |
| 119 self.mox.StubOutWithMock(os, 'walk') | |
| 120 self.mox.StubOutWithMock(os, 'write') | |
| 121 self.mox.StubOutWithMock(os.path, 'abspath') | |
| 122 self.mox.StubOutWithMock(os.path, 'exists') | |
| 123 self.mox.StubOutWithMock(os.path, 'getsize') | |
| 124 self.mox.StubOutWithMock(os.path, 'isdir') | |
| 125 self.mox.StubOutWithMock(os.path, 'isfile') | |
| 126 self.mox.StubOutWithMock(os.path, 'islink') | |
| 127 self.mox.StubOutWithMock(os.path, 'ismount') | |
| 128 self.mox.StubOutWithMock(os.path, 'lexists') | |
| 129 self.mox.StubOutWithMock(os.path, 'realpath') | |
| 130 self.mox.StubOutWithMock(os.path, 'samefile') | |
| 131 self.mox.StubOutWithMock(os.path, 'walk') | |
| 132 self.mox.StubOutWithMock(subprocess, 'call') | |
| 133 self.mox.StubOutWithMock(subprocess, 'Popen') | |
| 134 #self.mox.StubOutWithMock(sys, 'stderr') | |
| 135 self.mox.StubOutWithMock(sys, 'stdin') | |
| 136 self.mox.StubOutWithMock(sys, 'stdout') | |
| OLD | NEW |