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 """ |
| 8 import os as real_os |
| 9 import subprocess as real_subprocess |
| 10 |
12 class SysModuleStub(object): | 11 class SysModuleStub(object): |
13 def __init__(self): | 12 def __init__(self): |
14 self.platform = '' | 13 self.platform = '' |
15 | 14 |
16 class OSPathModuleStub(object): | 15 class OSPathModuleStub(object): |
17 def __init__(self, os_module_stub): | 16 def __init__(self, os_module_stub): |
18 self._os_module_stub = os_module_stub | 17 self._os_module_stub = os_module_stub |
19 | 18 |
20 def exists(self, path): | 19 def exists(self, path): |
21 return path in self._os_module_stub.files | 20 return path in self._os_module_stub.files |
22 | 21 |
23 def join(self, *args): | 22 def join(self, *args): |
24 if self._os_module_stub.sys.platform.startswith('win'): | 23 if self._os_module_stub.sys.platform.startswith('win'): |
25 tmp = real_os.path.join(*args) | 24 tmp = real_os.path.join(*args) |
26 return tmp.replace('/', '\\') | 25 return tmp.replace('/', '\\') |
27 else: | 26 else: |
28 return real_os.path.join(*args) | 27 return real_os.path.join(*args) |
29 | 28 |
30 def dirname(self, filename): | 29 def dirname(self, filename): # pylint: disable=R0201 |
31 return real_os.path.dirname(filename) | 30 return real_os.path.dirname(filename) |
32 | 31 |
33 class OSModuleStub(object): | 32 class OSModuleStub(object): |
34 def __init__(self, sys): | 33 def __init__(self, sys): |
35 self.sys = sys | 34 self.sys = sys |
36 self.path = OSPathModuleStub(self) | 35 self.path = OSPathModuleStub(self) |
37 self.files = [] | 36 self.files = [] |
38 self.display = ':0' | 37 self.display = ':0' |
39 self.local_app_data = None | 38 self.local_app_data = None |
40 | 39 |
41 def getenv(self, name): | 40 def getenv(self, name): |
42 if name == 'DISPLAY': | 41 if name == 'DISPLAY': |
43 return self.display | 42 return self.display |
44 if name == 'LOCALAPPDATA': | 43 if name == 'LOCALAPPDATA': |
45 return self.local_app_data | 44 return self.local_app_data |
46 raise Exception('Unsupported getenv') | 45 raise Exception('Unsupported getenv') |
47 | 46 |
48 class PopenStub(object): | 47 class PopenStub(object): |
49 def __init__(self, communicate_result): | 48 def __init__(self, communicate_result): |
50 self.communicate_result = communicate_result | 49 self.communicate_result = communicate_result |
51 | 50 |
52 def communicate(self): | 51 def communicate(self): |
53 return self.communicate_result | 52 return self.communicate_result |
54 | 53 |
55 class SubprocessModuleStub(object): | 54 class SubprocessModuleStub(object): |
56 def __init__(self): | 55 def __init__(self): |
57 self.Popen_hook = None | 56 self.Popen_hook = None |
58 self.Popen_result = None | 57 self.Popen_result = None |
59 import subprocess as real_subprocess | |
60 self.PIPE = real_subprocess.PIPE | 58 self.PIPE = real_subprocess.PIPE |
61 | 59 |
62 def Popen(self, *args, **kwargs): | 60 def Popen(self, *args, **kwargs): |
63 assert self.Popen_hook or self.Popen_result | 61 assert self.Popen_hook or self.Popen_result |
64 if self.Popen_hook: | 62 if self.Popen_hook: |
65 return self.Popen_hook(*args, **kwargs) | 63 return self.Popen_hook(*args, **kwargs) |
66 else: | 64 else: |
67 return self.Popen_result | 65 return self.Popen_result |
OLD | NEW |