Skip to content

Commit 934b2be

Browse files
sidhujusmndeveci
andauthored
fix: streaming stderr from subprocess started by lambda-builders (#563)
* display stderr if available * fix formatting and log to debug * have test assert with os.linesep * check decoded str for if statements --------- Co-authored-by: Mehmet Nuri Deveci <[email protected]>
1 parent ce114ae commit 934b2be

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

aws_lambda_builders/workflows/ruby_bundler/bundler.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
from os import linesep
67

78
LOG = logging.getLogger(__name__)
89

@@ -54,7 +55,7 @@ def run(self, args, cwd=None):
5455

5556
p = self.osutils.popen(invoke_bundler, stdout=self.osutils.pipe, stderr=self.osutils.pipe, cwd=cwd)
5657

57-
out, _ = p.communicate()
58+
out, err = p.communicate()
5859

5960
if p.returncode != 0:
6061
if p.returncode == GEMFILE_NOT_FOUND:
@@ -65,7 +66,19 @@ def run(self, args, cwd=None):
6566
if self.osutils.directory_exists(check_dir):
6667
self.osutils.remove_directory(check_dir)
6768
else:
68-
# Bundler has relevant information in stdout, not stderr.
69-
raise BundlerExecutionError(message=out.decode("utf8").strip())
69+
# Bundler can contain information in both stdout and stderr so we check and log both
70+
err_str = err.decode("utf8").strip()
71+
out_str = out.decode("utf8").strip()
72+
if out_str and err_str:
73+
message_out = f"{out_str}{linesep}{err_str}"
74+
LOG.debug(f"Bundler output: {out_str}")
75+
LOG.debug(f"Bundler error: {err_str}")
76+
elif out_str:
77+
message_out = out_str
78+
LOG.debug(f"Bundler output: {out_str}")
79+
else:
80+
message_out = err_str
81+
LOG.debug(f"Bundler error: {err_str}")
82+
raise BundlerExecutionError(message=message_out)
7083

7184
return out.decode("utf8").strip()

tests/unit/workflows/ruby_bundler/test_bundler.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest import TestCase
22
from unittest.mock import patch
3+
from os import linesep
34

45
from aws_lambda_builders.workflows.ruby_bundler.bundler import SubprocessBundler, BundlerExecutionError
56

@@ -73,13 +74,32 @@ def test_bundle_file_removed_if_generated(self):
7374
self.osutils.get_bundle_dir.assert_called_once()
7475
self.osutils.remove_directory.assert_called_once()
7576

76-
def test_raises_BundlerExecutionError_with_err_text_if_retcode_is_not_0(self):
77+
def test_raises_BundlerExecutionError_with_stdout_text_if_retcode_is_not_0(self):
7778
self.popen.returncode = 1
7879
self.popen.out = b"some error text\n\n"
80+
self.popen.err = b""
7981
with self.assertRaises(BundlerExecutionError) as raised:
8082
self.under_test.run(["install", "--without", "development", "test"])
8183
self.assertEqual(raised.exception.args[0], "Bundler Failed: some error text")
8284

85+
def test_raises_BundlerExecutionError_with_stderr_text_if_retcode_is_not_0(self):
86+
self.popen.returncode = 1
87+
self.popen.err = b"some error text\n\n"
88+
self.popen.out = b""
89+
with self.assertRaises(BundlerExecutionError) as raised:
90+
self.under_test.run(["install", "--without", "development", "test"])
91+
self.assertEqual(raised.exception.args[0], "Bundler Failed: some error text")
92+
93+
def test_raises_BundlerExecutionError_with_both_stderr_and_stdout_text_if_retcode_is_not_0(self):
94+
self.popen.returncode = 1
95+
self.popen.err = b"some error text from stderr\n\n"
96+
self.popen.out = b"some error text from stdout\n\n"
97+
with self.assertRaises(BundlerExecutionError) as raised:
98+
self.under_test.run(["install", "--without", "development", "test"])
99+
self.assertEqual(
100+
raised.exception.args[0], f"Bundler Failed: some error text from stdout{linesep}some error text from stderr"
101+
)
102+
83103
def test_raises_ValueError_if_args_not_a_list(self):
84104
with self.assertRaises(ValueError) as raised:
85105
self.under_test.run(("install", "--without", "development", "test"))

0 commit comments

Comments
 (0)