@@ -100,41 +100,39 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
100100return process
101101
102102try :
103- result ,error = process .communicate (input = input_prepared ,timeout = timeout )
103+ output ,error = process .communicate (input = input_prepared ,timeout = timeout )
104104except subprocess .TimeoutExpired :
105105process .kill ()
106106raise 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
111109assert 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-
122111if encoding :
123- result = result .decode (encoding )
112+ output = output .decode (encoding )
124113error = 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
127126RaiseError .UtilityExitedWithNonZeroCode (
128127cmd = cmd ,
129- exit_code = exit_status ,
130- msg_arg = error ,
128+ exit_code = process .returncode ,
131129error = error ,
132- out = result )
130+ out = output )
133131
134132if verbose :
135- return exit_status , result ,error
136- else :
137- return result
133+ return process . returncode , output ,error
134+
135+ return output
138136
139137# Environment setup
140138def environ (self ,var_name :str )-> str :
@@ -165,8 +163,30 @@ def find_executable(self, executable):
165163
166164def is_executable (self ,file ):
167165# 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 "
166+ command = ["test" ,"-x" ,file ]
167+
168+ exit_status ,output ,error = self .exec_command (cmd = command ,encoding = get_default_encoding (),ignore_errors = True ,verbose = True )
169+
170+ assert type (output )== str # noqa: E721
171+ assert type (error )== str # noqa: E721
172+
173+ if exit_status == 0 :
174+ return True
175+
176+ if exit_status == 1 :
177+ return False
178+
179+ errMsg = "Test operation returns an unknown result code: {0}. File name is [{1}]." .format (
180+ exit_status ,
181+ file )
182+
183+ RaiseError .UtilityExitedWithNonZeroCode (
184+ cmd = command ,
185+ exit_code = exit_status ,
186+ msg_arg = errMsg ,
187+ error = error ,
188+ out = output
189+ )
170190
171191def set_env (self ,var_name :str ,var_val :str ):
172192"""
@@ -251,15 +271,21 @@ def mkdtemp(self, prefix=None):
251271else :
252272command = ["mktemp" ,"-d" ]
253273
254- exit_status , result , error = self .exec_command (command ,verbose = True ,encoding = get_default_encoding (),ignore_errors = True )
274+ exec_exitcode , exec_output , exec_error = self .exec_command (command ,verbose = True ,encoding = get_default_encoding (),ignore_errors = True )
255275
256- assert type (result )== str # noqa: E721
257- assert type (error )== str # noqa: E721
276+ assert type (exec_exitcode )== int # noqa: E721
277+ assert type (exec_output )== str # noqa: E721
278+ assert type (exec_error )== str # noqa: E721
258279
259- if exit_status != 0 :
260- raise ExecUtilException ("Could not create temporary directory. Error code: {0}. Error message: {1}" .format (exit_status ,error ))
280+ if exec_exitcode != 0 :
281+ RaiseError .CommandExecutionError (
282+ cmd = command ,
283+ exit_code = exec_exitcode ,
284+ message = "Could not create temporary directory." ,
285+ error = exec_error ,
286+ out = exec_output )
261287
262- temp_dir = result .strip ()
288+ temp_dir = exec_output .strip ()
263289return temp_dir
264290
265291def mkstemp (self ,prefix = None ):
@@ -273,15 +299,21 @@ def mkstemp(self, prefix=None):
273299else :
274300command = ["mktemp" ]
275301
276- exit_status , result , error = self .exec_command (command ,verbose = True ,encoding = get_default_encoding (),ignore_errors = True )
302+ exec_exitcode , exec_output , exec_error = self .exec_command (command ,verbose = True ,encoding = get_default_encoding (),ignore_errors = True )
277303
278- assert type (result )== str # noqa: E721
279- assert type (error )== str # noqa: E721
304+ assert type (exec_exitcode )== int # noqa: E721
305+ assert type (exec_output )== str # noqa: E721
306+ assert type (exec_error )== str # noqa: E721
280307
281- if exit_status != 0 :
282- raise ExecUtilException ("Could not create temporary file. Error code: {0}. Error message: {1}" .format (exit_status ,error ))
308+ if exec_exitcode != 0 :
309+ RaiseError .CommandExecutionError (
310+ cmd = command ,
311+ exit_code = exec_exitcode ,
312+ message = "Could not create temporary file." ,
313+ error = exec_error ,
314+ out = exec_output )
283315
284- temp_file = result .strip ()
316+ temp_file = exec_output .strip ()
285317return temp_file
286318
287319def copytree (self ,src ,dst ):