(
self,
args,
as_table=False,
as_lines=False,
as_json=False,
cwd=None,
wait=True,
expect_fail=None,
stdin=None,
log=False,
ignore_stderr=False,
env=None,
use_server_dir=True,
cmd_prefix: Optional[List[str]] = None,
)
| 276 | return None |
| 277 | |
| 278 | def command( |
| 279 | self, |
| 280 | args, |
| 281 | as_table=False, |
| 282 | as_lines=False, |
| 283 | as_json=False, |
| 284 | cwd=None, |
| 285 | wait=True, |
| 286 | expect_fail=None, |
| 287 | stdin=None, |
| 288 | log=False, |
| 289 | ignore_stderr=False, |
| 290 | env=None, |
| 291 | use_server_dir=True, |
| 292 | cmd_prefix: Optional[List[str]] = None, |
| 293 | ): |
| 294 | cmd_prefix = cmd_prefix if cmd_prefix is not None else [] |
| 295 | if isinstance(args, str): |
| 296 | args = [args] |
| 297 | else: |
| 298 | args = list(args) |
| 299 | |
| 300 | if isinstance(stdin, str): |
| 301 | stdin = stdin.encode() |
| 302 | |
| 303 | final_args = cmd_prefix + [get_hq_binary(self.debug)] |
| 304 | if use_server_dir: |
| 305 | final_args += ["--server-dir", self.server_dir] |
| 306 | final_args += args |
| 307 | cwd = cwd or self.work_path |
| 308 | environment = self.make_default_env(log=log) |
| 309 | if env is not None: |
| 310 | environment.update(env) |
| 311 | stderr = subprocess.DEVNULL if ignore_stderr else subprocess.STDOUT |
| 312 | |
| 313 | if not wait: |
| 314 | return subprocess.Popen(final_args, stderr=stderr, cwd=cwd, env=environment) |
| 315 | |
| 316 | else: |
| 317 | process = subprocess.Popen( |
| 318 | final_args, |
| 319 | stdout=subprocess.PIPE, |
| 320 | stderr=stderr, |
| 321 | cwd=cwd, |
| 322 | env=environment, |
| 323 | stdin=subprocess.PIPE if stdin is not None else subprocess.DEVNULL, |
| 324 | ) |
| 325 | stdout = process.communicate(stdin)[0].decode() |
| 326 | if process.returncode != 0: |
| 327 | if expect_fail: |
| 328 | if expect_fail not in stdout: |
| 329 | raise Exception(f"Command should failed with message '{expect_fail}' but got:\n{stdout}") |
| 330 | else: |
| 331 | return |
| 332 | print(f"Process output: {stdout}") |
| 333 | raise Exception(f"Process failed with exit-code {process.returncode}\n\n{stdout}") |
| 334 | if expect_fail is not None: |
| 335 | raise Exception("Command should failed") |
no test coverage detected