Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/common/system/executive_mock.py

Issue 2588863002: Make MockExecutive behave more like MockExecutive2. (Closed)
Patch Set: Reformat args lists for MockHost, MockSystemHost. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (C) 2011 Google Inc. All rights reserved. 1 # Copyright (C) 2011 Google Inc. All rights reserved.
2 # 2 #
3 # Redistribution and use in source and binary forms, with or without 3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are 4 # modification, are permitted provided that the following conditions are
5 # met: 5 # met:
6 # 6 #
7 # * Redistributions of source code must retain the above copyright 7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer. 8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above 9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer 10 # copyright notice, this list of conditions and the following disclaimer
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 # FIXME: This should be unified with MockExecutive2 (http://crbug.com/626115). 60 # FIXME: This should be unified with MockExecutive2 (http://crbug.com/626115).
61 class MockExecutive(object): 61 class MockExecutive(object):
62 PIPE = "MOCK PIPE" 62 PIPE = "MOCK PIPE"
63 STDOUT = "MOCK STDOUT" 63 STDOUT = "MOCK STDOUT"
64 64
65 @staticmethod 65 @staticmethod
66 def ignore_error(error): 66 def ignore_error(error):
67 pass 67 pass
68 68
69 def __init__(self, should_log=False, should_throw=False, 69 def __init__(self, should_log=False, should_throw=False,
70 should_throw_when_run=None, should_return_zero_when_run=None): 70 output="MOCK output of child process", stderr='',
71 exit_code=0, exception=None, run_command_fn=None):
71 self._should_log = should_log 72 self._should_log = should_log
72 self._should_throw = should_throw 73 self._should_throw = should_throw
73 self._should_throw_when_run = should_throw_when_run or set()
74 self._should_return_zero_when_run = should_return_zero_when_run or set()
75 # FIXME: Once executive wraps os.getpid() we can just use a static pid f or "this" process. 74 # FIXME: Once executive wraps os.getpid() we can just use a static pid f or "this" process.
76 self._running_pids = {'test-webkitpy': os.getpid()} 75 self._running_pids = {'test-webkitpy': os.getpid()}
76 self._output = output
77 self._stderr = stderr
78 self._exit_code = exit_code
79 self._exception = exception
80 self._run_command_fn = run_command_fn
81 self._proc = None
77 self.calls = [] 82 self.calls = []
78 self._output = "MOCK output of child process"
79 self._proc = None
80 83
81 def check_running_pid(self, pid): 84 def check_running_pid(self, pid):
82 return pid in self._running_pids.values() 85 return pid in self._running_pids.values()
83 86
84 def running_pids(self, process_name_filter): 87 def running_pids(self, process_name_filter):
85 running_pids = [] 88 running_pids = []
86 for process_name, process_pid in self._running_pids.iteritems(): 89 for process_name, process_pid in self._running_pids.iteritems():
87 if process_name_filter(process_name): 90 if process_name_filter(process_name):
88 running_pids.append(process_pid) 91 running_pids.append(process_pid)
89 92
90 _log.info("MOCK running_pids: %s", running_pids) 93 _log.info("MOCK running_pids: %s", running_pids)
91 return running_pids 94 return running_pids
92 95
93 def command_for_printing(self, args): 96 def command_for_printing(self, args):
94 string_args = map(unicode, args) 97 string_args = map(unicode, args)
95 return " ".join(string_args) 98 return " ".join(string_args)
96 99
100 # The argument list should match Executive.run_command, even if
101 # some arguments are not used. pylint: disable=unused-argument
97 def run_command(self, 102 def run_command(self,
98 args, 103 args,
99 cwd=None, 104 cwd=None,
100 input=None, 105 input=None, # pylint: disable=redefined-builtin
101 # pylint: disable=unused-argument 106 timeout_seconds=False,
102 timeout_seconds=None,
103 error_handler=None, 107 error_handler=None,
104 return_exit_code=False, 108 return_exit_code=False,
105 return_stderr=True, 109 return_stderr=True,
106 decode_output=False, 110 decode_output=False,
107 env=None, 111 env=None,
108 debug_logging=False): 112 debug_logging=False):
109
110 self.calls.append(args) 113 self.calls.append(args)
111 114
112 assert isinstance(args, list) or isinstance(args, tuple) 115 assert isinstance(args, list) or isinstance(args, tuple)
116
113 if self._should_log: 117 if self._should_log:
114 env_string = "" 118 env_string = ""
115 if env: 119 if env:
116 env_string = ", env=%s" % env 120 env_string = ", env=%s" % env
117 input_string = "" 121 input_string = ""
118 if input: 122 if input:
119 input_string = ", input=%s" % input 123 input_string = ", input=%s" % input
120 _log.info("MOCK run_command: %s, cwd=%s%s%s", args, cwd, env_string, input_string) 124 _log.info("MOCK run_command: %s, cwd=%s%s%s", args, cwd, env_string, input_string)
121 125
122 if self._should_throw_when_run.intersection(args): 126 if self._exception:
123 raise ScriptError("Exception for %s" % args, output="MOCK command ou tput") 127 raise self._exception # pylint: disable=raising-bad-type
124
125 if self._should_throw: 128 if self._should_throw:
126 raise ScriptError("MOCK ScriptError", output=self._output) 129 raise ScriptError("MOCK ScriptError", output=self._output)
127 130
128 if return_exit_code and self._should_return_zero_when_run.intersection(a rgs): 131 if self._run_command_fn:
129 return 0 132 return self._run_command_fn(args)
133
134 if return_exit_code:
135 return self._exit_code
136
137 if self._exit_code and error_handler:
138 script_error = ScriptError(script_args=args, exit_code=self._exit_co de, output=self._output)
139 error_handler(script_error)
140
141 if return_stderr:
142 return self._output + self._stderr
130 143
131 return self._output 144 return self._output
132 145
133 def cpu_count(self): 146 def cpu_count(self):
134 return 2 147 return 2
135 148
136 def kill_all(self, process_name): 149 def kill_all(self, process_name):
137 pass 150 pass
138 151
139 def kill_process(self, pid): 152 def kill_process(self, pid):
140 pass 153 pass
141 154
142 def popen(self, args, cwd=None, env=None, **kwargs): 155 def popen(self, args, cwd=None, env=None, **_):
143 assert all(isinstance(arg, basestring) for arg in args) 156 assert all(isinstance(arg, basestring) for arg in args)
144 self.calls.append(args) 157 self.calls.append(args)
145 if self._should_log: 158 if self._should_log:
146 cwd_string = "" 159 cwd_string = ""
147 if cwd: 160 if cwd:
148 cwd_string = ", cwd=%s" % cwd 161 cwd_string = ", cwd=%s" % cwd
149 env_string = "" 162 env_string = ""
150 if env: 163 if env:
151 env_string = ", env=%s" % env 164 env_string = ", env=%s" % env
152 _log.info("MOCK popen: %s%s%s", args, cwd_string, env_string) 165 _log.info("MOCK popen: %s%s%s", args, cwd_string, env_string)
153 if not self._proc: 166 if not self._proc:
154 self._proc = MockProcess(self._output) 167 self._proc = MockProcess(self._output)
155 return self._proc 168 return self._proc
156 169
157 def call(self, args, **kwargs): 170 def call(self, args, **_):
158 assert all(isinstance(arg, basestring) for arg in args) 171 assert all(isinstance(arg, basestring) for arg in args)
159 self.calls.append(args) 172 self.calls.append(args)
160 _log.info('Mock call: %s', args) 173 _log.info('Mock call: %s', args)
161 174
162 def run_in_parallel(self, commands): 175 def run_in_parallel(self, commands):
163 assert len(commands) 176 assert len(commands)
164 177
165 num_previous_calls = len(self.calls) 178 num_previous_calls = len(self.calls)
166 command_outputs = [] 179 command_outputs = []
167 for cmd_line, cwd in commands: 180 for cmd_line, cwd in commands:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if self._run_command_fn: 222 if self._run_command_fn:
210 return self._run_command_fn(args) 223 return self._run_command_fn(args)
211 if return_exit_code: 224 if return_exit_code:
212 return self._exit_code 225 return self._exit_code
213 if self._exit_code and error_handler: 226 if self._exit_code and error_handler:
214 script_error = ScriptError(script_args=args, exit_code=self._exit_co de, output=self._output) 227 script_error = ScriptError(script_args=args, exit_code=self._exit_co de, output=self._output)
215 error_handler(script_error) 228 error_handler(script_error)
216 if return_stderr: 229 if return_stderr:
217 return self._output + self._stderr 230 return self._output + self._stderr
218 return self._output 231 return self._output
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698