OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import os as real_os | |
5 import sys as real_sys | |
6 import subprocess as real_subprocess | |
7 | |
8 """Provides stubs for os, sys and subprocess for testing | 4 """Provides stubs for os, sys and subprocess for testing |
9 | 5 |
10 This test allows one to test code that itself uses os, sys, and subprocess. | 6 This test allows one to test code that itself uses os, sys, and subprocess. |
11 """ | 7 """ |
12 class SysModuleStub(object): | 8 |
9 import os | |
10 import subprocess | |
11 | |
12 class Override(object): | |
13 def __init__(self, base_module, **stubs): | |
14 self._base_module = base_module | |
15 self._stubs = stubs | |
16 self._originals = {} | |
17 | |
18 def __enter__(self): | |
19 for module_name, stub in self._stubs.iteritems(): | |
20 self._originals[module_name] = getattr(self._base_module, module_name) | |
21 setattr(self._base_module, module_name, stub) | |
22 return self | |
23 | |
24 def __exit__(self, *args): | |
25 for module_name, original in self._originals.iteritems(): | |
26 setattr(self._base_module, module_name, original) | |
27 | |
nduca
2012/09/20 04:18:30
should __del__ throw an exception if they didn't e
| |
28 # adb not even found | |
29 # android_browser_finder not returning | |
30 class ADBCommandsStub(object): | |
31 def __init__(self, module, device): | |
32 self._module = module | |
33 self._device = device | |
34 self.is_root_enabled = True | |
35 | |
36 def RunShellCommand(self, args): | |
37 if isinstance(args, basestring): | |
38 import shlex | |
39 args = shlex.split(args) | |
40 handler = self._module.shell_command_handlers[args[0]] | |
41 return handler(args) | |
42 | |
43 def IsRootEnabled(self): | |
44 return self.is_root_enabled | |
45 | |
46 class ADBCommandsModuleStub(object): | |
13 def __init__(self): | 47 def __init__(self): |
14 self.platform = '' | 48 self.attached_devices = [] |
49 self.shell_command_handlers = {} | |
50 | |
51 def ADBCommandsStubConstructor(device=None): | |
52 return ADBCommandsStub(self, device) | |
53 self.ADBCommands = ADBCommandsStubConstructor | |
54 | |
55 def IsAndroidSupported(self): | |
56 return True | |
57 | |
58 def GetAttachedDevices(self): | |
59 return self.attached_devices | |
60 | |
61 def HasForwarder(self, adb): | |
62 return True | |
15 | 63 |
16 class OSPathModuleStub(object): | 64 class OSPathModuleStub(object): |
17 def __init__(self, os_module_stub): | 65 def __init__(self, os_module_stub): |
18 self._os_module_stub = os_module_stub | 66 self._os_module_stub = os_module_stub |
19 | 67 |
20 def exists(self, path): | 68 def exists(self, path): |
21 return path in self._os_module_stub.files | 69 return path in self._os_module_stub.files |
22 | 70 |
23 def join(self, *args): | 71 def join(self, *args): |
24 if self._os_module_stub.sys.platform.startswith('win'): | 72 if self._os_module_stub.sys.platform.startswith('win'): |
25 tmp = real_os.path.join(*args) | 73 tmp = os.path.join(*args) |
26 return tmp.replace('/', '\\') | 74 return tmp.replace('/', '\\') |
27 else: | 75 else: |
28 return real_os.path.join(*args) | 76 return os.path.join(*args) |
29 | 77 |
30 def dirname(self, filename): | 78 def dirname(self, filename): |
31 return real_os.path.dirname(filename) | 79 return os.path.dirname(filename) |
32 | 80 |
33 class OSModuleStub(object): | 81 class OSModuleStub(object): |
34 def __init__(self, sys): | 82 def __init__(self, sys): |
35 self.sys = sys | 83 self.sys = sys |
36 self.path = OSPathModuleStub(self) | 84 self.path = OSPathModuleStub(self) |
37 self.files = [] | 85 self.files = [] |
38 self.display = ':0' | 86 self.display = ':0' |
39 self.local_app_data = None | 87 self.local_app_data = None |
88 self.devnull = os.devnull | |
40 | 89 |
41 def getenv(self, name): | 90 def getenv(self, name): |
42 if name == 'DISPLAY': | 91 if name == 'DISPLAY': |
43 return self.display | 92 return self.display |
44 if name == 'LOCALAPPDATA': | 93 if name == 'LOCALAPPDATA': |
45 return self.local_app_data | 94 return self.local_app_data |
46 raise Exception('Unsupported getenv') | 95 raise Exception('Unsupported getenv') |
47 | 96 |
48 class PopenStub(object): | 97 class PopenStub(object): |
49 def __init__(self, communicate_result): | 98 def __init__(self, communicate_result): |
50 self.communicate_result = communicate_result | 99 self.communicate_result = communicate_result |
51 | 100 |
52 def communicate(self): | 101 def communicate(self): |
53 return self.communicate_result | 102 return self.communicate_result |
54 | 103 |
55 class SubprocessModuleStub(object): | 104 class SubprocessModuleStub(object): |
56 def __init__(self): | 105 def __init__(self): |
57 self.Popen_hook = None | 106 self.Popen_hook = None |
58 self.Popen_result = None | 107 self.Popen_result = None |
59 import subprocess as real_subprocess | 108 self.PIPE = subprocess.PIPE |
60 self.PIPE = real_subprocess.PIPE | 109 self.call_hook = None |
61 | 110 |
62 def Popen(self, *args, **kwargs): | 111 def Popen(self, *args, **kwargs): |
63 assert self.Popen_hook or self.Popen_result | 112 assert self.Popen_hook or self.Popen_result |
64 if self.Popen_hook: | 113 if self.Popen_hook: |
65 return self.Popen_hook(*args, **kwargs) | 114 return self.Popen_hook(*args, **kwargs) |
66 else: | 115 else: |
67 return self.Popen_result | 116 return self.Popen_result |
117 | |
118 def call(self, *args, **kwargs): | |
119 assert self.call_hook | |
120 return self.call_hook(*args, **kwargs) | |
121 | |
122 class SysModuleStub(object): | |
123 def __init__(self): | |
124 self.platform = '' | |
OLD | NEW |