From f573dccb6a46d568aaf334a8ba8139776f86cc1d Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:22:01 -0700 Subject: [PATCH] wip --- tests/testing_utils.py | 48 ++++++++++++------------------------------ 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/tests/testing_utils.py b/tests/testing_utils.py index 2709331dd8..6e38aba44a 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -8,7 +8,8 @@ from threading import Thread from typing import Callable, List, Optional, cast from collections import namedtuple -from subprocess import Popen, PIPE, TimeoutExpired, check_output +from subprocess import Popen, PIPE, TimeoutExpired, CalledProcessError +import subprocess from queue import Queue from samcli import __version__ as SAM_CLI_VERSION @@ -67,68 +68,47 @@ def method_to_stack_name(method_name): def run_command(command_list, cwd=None, env=None, timeout=TIMEOUT) -> CommandResult: LOG.info("Running command: %s", " ".join(command_list)) - import subprocess - try: process = subprocess.run(command_list, cwd=cwd, timeout=timeout, stdout=PIPE, stderr=PIPE) - except subprocess.CalledProcessError as e: + except CalledProcessError as e: LOG.error("Command %s failed: %s", command_list, e.returncode) LOG.error("Stdout: %s", e.stdout.decode("utf-8")) LOG.error("Stderr: %s", e.stderr.decode("utf-8")) raise except TimeoutExpired as e: - LOG.error("Command %s, TIMED OUT") - LOG.error("Stdout: %s", e.stdout.decode("utf-8")) - LOG.error("Stderr: %s", e.stderr.decode("utf-8")) + LOG.error("Command %s, TIMED OUT in %ss", command_list, timeout) + if e.stdout: + LOG.error("Stdout: %s", e.stdout.decode("utf-8")) + if e.stderr: + LOG.error("Stderr: %s", e.stderr.decode("utf-8")) raise LOG.info("Stdout: %s", process.stdout.decode("utf-8")) LOG.info("Stderr: %s", process.stderr.decode("utf-8")) return CommandResult(process, process.stdout, process.stderr) - # process_execute = Popen(command_list, cwd=cwd, env=env, stdout=PIPE, stderr=PIPE) - # try: - # stdout_data, stderr_data = process_execute.communicate(timeout=timeout) - # LOG.info(f"Stdout: {stdout_data.decode('utf-8')}") - # LOG.info(f"Stderr: {stderr_data.decode('utf-8')}") - # return CommandResult(process_execute, stdout_data, stderr_data) - # except TimeoutExpired: - # LOG.error(f"Command: {command_list}, TIMED OUT") - # LOG.error(f"Return Code: {process_execute.returncode}") - # process_execute.kill() - # raise def run_command_with_input(command_list, stdin_input, timeout=TIMEOUT, cwd=None) -> CommandResult: LOG.info("Running command: %s", " ".join(command_list)) LOG.info("With input: %s", stdin_input) - import subprocess try: process = subprocess.run(command_list, cwd=cwd, input=stdin_input, timeout=timeout, stdout=PIPE, stderr=PIPE) - except subprocess.CalledProcessError as e: + except CalledProcessError as e: LOG.error("Command %s failed: %s", command_list, e.returncode) LOG.error("Stdout: %s", e.stdout.decode("utf-8")) LOG.error("Stderr: %s", e.stderr.decode("utf-8")) raise except TimeoutExpired as e: - LOG.error("Command %s, TIMED OUT") - LOG.error("Stdout: %s", e.stdout.decode("utf-8")) - LOG.error("Stderr: %s", e.stderr.decode("utf-8")) + LOG.error("Command %s, TIMED OUT in %ss", command_list, timeout) + if e.stdout: + LOG.error("Stdout: %s", e.stdout.decode("utf-8")) + if e.stderr: + LOG.error("Stderr: %s", e.stderr.decode("utf-8")) raise LOG.info("Stdout: %s", process.stdout.decode("utf-8")) LOG.info("Stderr: %s", process.stderr.decode("utf-8")) return CommandResult(process, process.stdout, process.stderr) - # process_execute = Popen(command_list, cwd=cwd, stdout=PIPE, stderr=PIPE, stdin=PIPE) - # try: - # stdout_data, stderr_data = process_execute.communicate(stdin_input, timeout=timeout) - # LOG.info(f"Stdout: {stdout_data.decode('utf-8')}") - # LOG.info(f"Stderr: {stderr_data.decode('utf-8')}") - # return CommandResult(process_execute, stdout_data, stderr_data) - # except TimeoutExpired: - # LOG.error(f"Command: {command_list}, TIMED OUT") - # LOG.error(f"Return Code: {process_execute.returncode}") - # process_execute.kill() - # raise def run_command_with_inputs(command_list: List[str], inputs: List[str], timeout=TIMEOUT) -> CommandResult: