@@ -100,41 +100,40 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
100
100
return process
101
101
102
102
try :
103
- result , error = process .communicate (input = input_prepared , timeout = timeout )
103
+ output , error = process .communicate (input = input_prepared , timeout = timeout )
104
104
except subprocess .TimeoutExpired :
105
105
process .kill ()
106
106
raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
107
107
108
- exit_status = process .returncode
109
-
110
- assert type (result ) == bytes # noqa: E721
108
+ assert type (output ) == bytes # noqa: E721
111
109
assert type (error ) == bytes # noqa: E721
112
110
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
-
122
111
if encoding :
123
- result = result .decode (encoding )
112
+ output = output .decode (encoding )
124
113
error = error .decode (encoding )
125
114
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
127
126
RaiseError .UtilityExitedWithNonZeroCode (
128
127
cmd = cmd ,
129
- exit_code = exit_status ,
128
+ exit_code = process . returncode ,
130
129
msg_arg = error ,
131
130
error = error ,
132
- out = result )
131
+ out = output )
133
132
134
133
if verbose :
135
- return exit_status , result , error
136
- else :
137
- return result
134
+ return process . returncode , output , error
135
+
136
+ return output
138
137
139
138
# Environment setup
140
139
def environ (self , var_name : str ) -> str :
@@ -165,8 +164,30 @@ def find_executable(self, executable):
165
164
166
165
def is_executable (self , file ):
167
166
# 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
+ )
170
191
171
192
def set_env (self , var_name : str , var_val : str ):
172
193
"""
@@ -251,15 +272,21 @@ def mkdtemp(self, prefix=None):
251
272
else :
252
273
command = ["mktemp" , "-d" ]
253
274
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 )
255
276
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
258
280
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 )
261
288
262
- temp_dir = result .strip ()
289
+ temp_dir = exec_output .strip ()
263
290
return temp_dir
264
291
265
292
def mkstemp (self , prefix = None ):
@@ -273,15 +300,21 @@ def mkstemp(self, prefix=None):
273
300
else :
274
301
command = ["mktemp" ]
275
302
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 )
277
304
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
280
308
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 )
283
316
284
- temp_file = result .strip ()
317
+ temp_file = exec_output .strip ()
285
318
return temp_file
286
319
287
320
def copytree (self , src , dst ):
0 commit comments