| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 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 shutil |
| 12 import string | 12 import string |
| 13 import StringIO |
| 13 import subprocess | 14 import subprocess |
| 14 import sys | 15 import sys |
| 15 | 16 |
| 16 sys.path.append(os.path.dirname(os.path.dirname(__file__))) | 17 sys.path.append(os.path.dirname(os.path.dirname(__file__))) |
| 17 from third_party.pymox import mox | 18 from third_party.pymox import mox |
| 18 | 19 |
| 19 | 20 |
| 20 class IsOneOf(mox.Comparator): | 21 class IsOneOf(mox.Comparator): |
| 21 def __init__(self, keys): | 22 def __init__(self, keys): |
| 22 self._keys = keys | 23 self._keys = keys |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 80 |
| 80 def setUp(self): | 81 def setUp(self): |
| 81 self.root_dir = self.Dir() | 82 self.root_dir = self.Dir() |
| 82 self.args = self.Args() | 83 self.args = self.Args() |
| 83 self.relpath = self.String(200) | 84 self.relpath = self.String(200) |
| 84 | 85 |
| 85 def tearDown(self): | 86 def tearDown(self): |
| 86 pass | 87 pass |
| 87 | 88 |
| 88 | 89 |
| 89 class SuperMoxTestBase(TestCaseUtils, mox.MoxTestBase): | 90 class StdoutCheck(object): |
| 91 def setUp(self): |
| 92 # Override the mock with a StringIO, it's much less painful to test. |
| 93 self._old_stdout = sys.stdout |
| 94 sys.stdout = StringIO.StringIO() |
| 95 sys.stdout.flush = lambda: None |
| 96 |
| 97 def tearDown(self): |
| 98 try: |
| 99 # If sys.stdout was used, self.checkstdout() must be called. |
| 100 self.assertEquals('', sys.stdout.getvalue()) |
| 101 except AttributeError: |
| 102 pass |
| 103 sys.stdout = self._old_stdout |
| 104 |
| 105 def checkstdout(self, expected): |
| 106 value = sys.stdout.getvalue() |
| 107 sys.stdout.close() |
| 108 self.assertEquals(expected, value) |
| 109 |
| 110 |
| 111 class SuperMoxTestBase(TestCaseUtils, StdoutCheck, mox.MoxTestBase): |
| 90 def setUp(self): | 112 def setUp(self): |
| 91 """Patch a few functions with know side-effects.""" | 113 """Patch a few functions with know side-effects.""" |
| 92 TestCaseUtils.setUp(self) | 114 TestCaseUtils.setUp(self) |
| 93 mox.MoxTestBase.setUp(self) | 115 mox.MoxTestBase.setUp(self) |
| 94 #self.mox.StubOutWithMock(__builtin__, 'open') | 116 #self.mox.StubOutWithMock(__builtin__, 'open') |
| 95 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', | 117 os_to_mock = ('chdir', 'chown', 'close', 'closerange', 'dup', 'dup2', |
| 96 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek', | 118 'fchdir', 'fchmod', 'fchown', 'fdopen', 'getcwd', 'getpid', 'lseek', |
| 97 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', | 119 'makedirs', 'mkdir', 'open', 'popen', 'popen2', 'popen3', 'popen4', |
| 98 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', | 120 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'symlink', |
| 99 'system', 'tmpfile', 'walk', 'write') | 121 'system', 'tmpfile', 'walk', 'write') |
| 100 self.MockList(os, os_to_mock) | 122 self.MockList(os, os_to_mock) |
| 101 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', | 123 os_path_to_mock = ('abspath', 'exists', 'getsize', 'isdir', 'isfile', |
| 102 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') | 124 'islink', 'ismount', 'lexists', 'realpath', 'samefile', 'walk') |
| 103 self.MockList(os.path, os_path_to_mock) | 125 self.MockList(os.path, os_path_to_mock) |
| 104 self.MockList(shutil, ('rmtree')) | 126 self.MockList(shutil, ('rmtree')) |
| 105 self.MockList(subprocess, ('call', 'Popen')) | 127 self.MockList(subprocess, ('call', 'Popen')) |
| 106 # Don't mock stderr since it confuses unittests. | 128 # Don't mock stderr since it confuses unittests. |
| 107 self.MockList(sys, ('stdin', 'stdout')) | 129 self.MockList(sys, ('stdin')) |
| 130 StdoutCheck.setUp(self) |
| 108 | 131 |
| 109 def tearDown(self): | 132 def tearDown(self): |
| 133 StdoutCheck.tearDown(self) |
| 110 TestCaseUtils.tearDown(self) | 134 TestCaseUtils.tearDown(self) |
| 111 mox.MoxTestBase.tearDown(self) | 135 mox.MoxTestBase.tearDown(self) |
| 112 | 136 |
| 113 def MockList(self, parent, items_to_mock): | 137 def MockList(self, parent, items_to_mock): |
| 114 for item in items_to_mock: | 138 for item in items_to_mock: |
| 115 # Skip over items not present because of OS-specific implementation, | 139 # Skip over items not present because of OS-specific implementation, |
| 116 # implemented only in later python version, etc. | 140 # implemented only in later python version, etc. |
| 117 if hasattr(parent, item): | 141 if hasattr(parent, item): |
| 118 try: | 142 try: |
| 119 self.mox.StubOutWithMock(parent, item) | 143 self.mox.StubOutWithMock(parent, item) |
| 120 except TypeError: | 144 except TypeError: |
| 121 raise TypeError('Couldn\'t mock %s in %s' % (item, parent.__name__)) | 145 raise TypeError('Couldn\'t mock %s in %s' % (item, parent.__name__)) |
| 122 | 146 |
| 123 def UnMock(self, object, name): | 147 def UnMock(self, object, name): |
| 124 """Restore an object inside a test.""" | 148 """Restore an object inside a test.""" |
| 125 for (parent, old_child, child_name) in self.mox.stubs.cache: | 149 for (parent, old_child, child_name) in self.mox.stubs.cache: |
| 126 if parent == object and child_name == name: | 150 if parent == object and child_name == name: |
| 127 setattr(parent, child_name, old_child) | 151 setattr(parent, child_name, old_child) |
| 128 break | 152 break |
| OLD | NEW |