@@ -100,41 +100,40 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
100100 return process
101101
102102 try :
103- result , error = process .communicate (input = input_prepared , timeout = timeout )
103+ output , error = process .communicate (input = input_prepared , timeout = timeout )
104104 except subprocess .TimeoutExpired :
105105 process .kill ()
106106 raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
107107
108- exit_status = process .returncode
109-
110- assert type (result ) == bytes # noqa: E721
108+ assert type (output ) == bytes # noqa: E721
111109 assert type (error ) == bytes # noqa: E721
112110
113- if not error :
114- error_found = False
115- else :
116- error_found = exit_status != 0 or any (
117- marker in error for marker in [b'error' , b'Permission denied' , b'fatal' , b'No such file or directory' ]
118- )
119-
120- assert type (error_found ) == bool # noqa: E721
121-
122111 if encoding :
123- result = result .decode (encoding )
112+ output = output .decode (encoding )
124113 error = error .decode (encoding )
125114
126- if not ignore_errors and error_found and not expect_error :
115+ if expect_error :
116+ if process .returncode == 0 :
117+ raise InvalidOperationException ("We expected an execution error." )
118+ elif ignore_errors :
119+ pass
120+ elif process .returncode == 0 :
121+ pass
122+ else :
123+ assert not expect_error
124+ assert not ignore_errors
125+ assert process .returncode != 0
127126 RaiseError .UtilityExitedWithNonZeroCode (
128127 cmd = cmd ,
129- exit_code = exit_status ,
128+ exit_code = process . returncode ,
130129 msg_arg = error ,
131130 error = error ,
132- out = result )
131+ out = output )
133132
134133 if verbose :
135- return exit_status , result , error
136- else :
137- return result
134+ return process . returncode , output , error
135+
136+ return output
138137
139138 # Environment setup
140139 def environ (self , var_name : str ) -> str :
@@ -165,8 +164,30 @@ def find_executable(self, executable):
165164
166165 def is_executable (self , file ):
167166 # Check if the file is executable
168- is_exec = self .exec_command ("test -x {} && echo OK" .format (file ))
169- return is_exec == b"OK\n "
167+ command = ["test" , "-x" , file ]
168+
169+ exit_status , output , error = self .exec_command (cmd = command , encoding = get_default_encoding (), ignore_errors = True , verbose = True )
170+
171+ assert type (output ) == str # noqa: E721
172+ assert type (error ) == str # noqa: E721
173+
174+ if exit_status == 0 :
175+ return True
176+
177+ if exit_status == 1 :
178+ return False
179+
180+ errMsg = "Test operation returns an unknown result code: {0}. File name is [{1}]." .format (
181+ exit_status ,
182+ file )
183+
184+ RaiseError .CommandExecutionError (
185+ cmd = command ,
186+ exit_code = exit_status ,
187+ msg_arg = errMsg ,
188+ error = error ,
189+ out = output
190+ )
170191
171192 def set_env (self , var_name : str , var_val : str ):
172193 """
@@ -251,15 +272,21 @@ def mkdtemp(self, prefix=None):
251272 else :
252273 command = ["mktemp" , "-d" ]
253274
254- exit_status , result , error = self .exec_command (command , verbose = True , encoding = get_default_encoding (), ignore_errors = True )
275+ exec_exitcode , exec_output , exec_error = self .exec_command (command , verbose = True , encoding = get_default_encoding (), ignore_errors = True )
255276
256- assert type (result ) == str # noqa: E721
257- assert type (error ) == str # noqa: E721
277+ assert type (exec_exitcode ) == int # noqa: E721
278+ assert type (exec_output ) == str # noqa: E721
279+ assert type (exec_error ) == str # noqa: E721
258280
259- if exit_status != 0 :
260- raise ExecUtilException ("Could not create temporary directory. Error code: {0}. Error message: {1}" .format (exit_status , error ))
281+ if exec_exitcode != 0 :
282+ RaiseError .CommandExecutionError (
283+ cmd = command ,
284+ exit_code = exec_exitcode ,
285+ message = "Could not create temporary directory." ,
286+ error = exec_error ,
287+ out = exec_output )
261288
262- temp_dir = result .strip ()
289+ temp_dir = exec_output .strip ()
263290 return temp_dir
264291
265292 def mkstemp (self , prefix = None ):
@@ -273,15 +300,21 @@ def mkstemp(self, prefix=None):
273300 else :
274301 command = ["mktemp" ]
275302
276- exit_status , result , error = self .exec_command (command , verbose = True , encoding = get_default_encoding (), ignore_errors = True )
303+ exec_exitcode , exec_output , exec_error = self .exec_command (command , verbose = True , encoding = get_default_encoding (), ignore_errors = True )
277304
278- assert type (result ) == str # noqa: E721
279- assert type (error ) == str # noqa: E721
305+ assert type (exec_exitcode ) == int # noqa: E721
306+ assert type (exec_output ) == str # noqa: E721
307+ assert type (exec_error ) == str # noqa: E721
280308
281- if exit_status != 0 :
282- raise ExecUtilException ("Could not create temporary file. Error code: {0}. Error message: {1}" .format (exit_status , error ))
309+ if exec_exitcode != 0 :
310+ RaiseError .CommandExecutionError (
311+ cmd = command ,
312+ exit_code = exec_exitcode ,
313+ message = "Could not create temporary file." ,
314+ error = exec_error ,
315+ out = exec_output )
283316
284- temp_file = result .strip ()
317+ temp_file = exec_output .strip ()
285318 return temp_file
286319
287320 def copytree (self , src , dst ):
0 commit comments